Setting variable based on how many events are on a few of the in-game maps.

Hello all,
I am using Visustella's Furniture System found here: http://www.yanfly.moe/wiki/Furniture_System_VisuStella_MZ

Ultimately, what I want to use alongside it is a system where the player is rewarded at the end of every in-game day based on how many furniture items they have in both their home and in their yard outside of their home (so two different maps). Visustella's Furniture System works by using event templates with their Events and Movement Core plugin and allows the player to place furniture items that are events on the map. My idea would be to have the end-of-day common event at the end of every in-game day calculate how many furniture items they have both inside their home and outside their home and calculate a score based on that. I'm sure there is a script or something that I could run to get that information, but I haven't been able to find it. A more advanced feature I'd love too would be to give different points based on WHAT furniture is placed (larger/more expensive furniture items grant more points, etc.) but I can't fathom how that would even work, but if anyone has any ideas on that, I would tremendously appreciate that as well!

The furniture system plugin has it to where you can trigger a common event whenever a furniture item is placed if that's at all relevant or helpful.
Hello all,
I am using Visustella's Furniture System found here: http://www.yanfly.moe/wiki/Furniture_System_VisuStella_MZ

Ultimately, what I want to use alongside it is a system where the player is rewarded at the end of every in-game day based on how many furniture items they have in both their home and in their yard outside of their home (so two different maps). Visustella's Furniture System works by using event templates with their Events and Movement Core plugin and allows the player to place furniture items that are events on the map. My idea would be to have the end-of-day common event at the end of every in-game day calculate how many furniture items they have both inside their home and outside their home and calculate a score based on that. I'm sure there is a script or something that I could run to get that information, but I haven't been able to find it. A more advanced feature I'd love too would be to give different points based on WHAT furniture is placed (larger/more expensive furniture items grant more points, etc.) but I can't fathom how that would even work, but if anyone has any ideas on that, I would tremendously appreciate that as well!

The furniture system plugin has it to where you can trigger a common event whenever a furniture item is placed if that's at all relevant or helpful.
In that case, why not just have it call a common event that adds 1 to a variable?
That was my initial thought to, but then I couldn't figure out how to keep the player from adding and removing the same furniture over and over to continuously add 1 to the variable to cheese it. If there was a way to remove 1 from the variable when the player removes and stores a furniture item, that would be perfect, but the plugin doesn't seem to run a common event when the player removes an item and puts it back into their inventory. So the only solution I could think of was one that counts the events on the map.
What you could do is make a small plugin that aliases the command for retrieving furniture, removes 1 from your variable and then calls the original.

You can find out what the command is called pretty easily by typing PluginManager._commands["VisuMZ_2_FurnitureSystem into the console and then seeing what comes up in the intellisense list.
If the furniture items are events you could put notetags in their event properties giving the point values then a script to loop through the events, get that meta tag property for each one and add them up.
Ah, sounds like I need to learn scripting and that would make my life a whole lot easier! :LZSsmile:
Ah, sounds like I need to learn scripting and that would make my life a whole lot easier! :LZSsmile:
This but unironically.

Something like
JavaScript:
var fPieces = 0;
var fPoints = 0;
$gameMap.events().forEach(event => {if (event.meta.furniturePoints  && !event._erased){
fPieces += 1;
fPoints = fPoints + event.meta.furniturePoints;
}
$gameVariables.setValue(99, fPieces);
$gameVariables.setValue(100, fPoints);
});
Untested, but that should make it so you can put the notetag <furniturePoints:x> in the event notebox, then it'll add them up and store the total in variable 100. Or you can use whatever label you want if furniturePoints is too long, just update the reference in the script.

Actually, I modified it to just count the furniture too, or more accurately any event that has that furniturePoints tag and store it in variable 99.
Thanks so much for that! I couldn't get that to work when I tried it but I'm sure it's a me problem and not with the code. I put that script in a common event to run parallel with the GameStart switch that turns on when loading the game with a Wait 10 at the end, put <furniturePoints:70> in one of the event notetags as a test, placed that furniture item and the variables 99 and 100 remained at zero.

I don't expect you to try to troubleshoot my problems because it's not your responsibility, but I tremendously appreciate you giving me an idea on how to implement it if I can take some time to learn some javascript myself.
Thanks so much for that! I couldn't get that to work when I tried it but I'm sure it's a me problem and not with the code. I put that script in a common event to run parallel with the GameStart switch that turns on when loading the game with a Wait 10 at the end, put <furniturePoints:70> in one of the event notetags as a test, placed that furniture item and the variables 99 and 100 remained at zero.

I don't expect you to try to troubleshoot my problems because it's not your responsibility, but I tremendously appreciate you giving me an idea on how to implement it if I can take some time to learn some javascript myself.
It's possible that the furniture items are stored separately from the map events. I don't have the plugin to check.
It's possible that the furniture items are stored separately from the map events. I don't have the plugin to check.
You could test it by putting the tag on an existing event on the map just to see if the script works at all.

Also could you press F8 and see if there are any errors in the console log?
Could be. It uses the Events & Movements Core where you essentially create event templates and store them in a random "event map" where you set up the event templates in the plugin parameters. So I would add a bed event item in that event map with all the other furniture events, set it up as a template, and then add the notetag in the furniture items in the item database that link that item to a specific item template. Then when the player goes to use that item in their item menu, it places a copy of that item template on the map the player is on and allows them to place it wherever on that map. I added the notetags to the item templates so perhaps when it called a copy of the item template, it doesn't copy of the notetag? But I would think it would still at least calculate the notetag points in the templates on the designated furniture event map it pulls from..
1707179937497.png

Might need to use a null-coalescing operator to short-circuit when event or meta aren't present.

Or actually, wait. Does it not need to be event.event().meta to get the underlying json instead of the Game_Event instance?
You could test it by putting the tag on an existing event on the map just to see if the script works at all.

Also could you press F8 and see if there are any errors in the console log?
Ah good idea! I gave that a shot and still remained at zero. Here is the console log.

FurnitureScriptConsoleLog.png
Yeah, you need to change it to

JavaScript:
let fPieces = 0;
let fPoints = 0;

for (const event of $gameMap.events()) {
  if (event.event().meta.furniturePoints  && !event._erased) {
    fPieces += 1;
    fPoints += parseInt(event.event().meta.furniturePoints);
}

$gameVariables.setValue(99, fPieces);
$gameVariables.setValue(100, fPoints);
});

(for of is generally less expensive than forEach, but you don't actually have to change that if you want to use AquaEcho's version. The important part is changing "event.meta" to "event.event().meta" and using parseInt so it doesn't end up being a string)
You two are geniuses, thank you! So it's now getting the variable to go from zero to a number; however, the funny thing is if I add an item with 70 furniture points and then add an item with 50 furniture points, instead of changing the variable to 120, it changes it to 07050!

Additionally, it changes based on which map the player is on so if they move to another map, it goes back to zero. The ideal scenario would be it keeps track of three or four specific maps regardless of which the player is on. So far, map 25 and 26 I would want to calculate (probably some others as I continue to build the game)
You two are geniuses, thank you! So it's now getting the variable to go from zero to a number; however, the funny thing is if I add an item with 70 furniture points and then add an item with 50 furniture points, instead of changing the variable to 120, it changes it to 07050!

Additionally, it changes based on which map the player is on so if they move to another map, it goes back to zero. The ideal scenario would be it keeps track of three or four specific maps regardless of which the player is on. So far, map 25 and 26 I would want to calculate (probably some others as I continue to build the game)
Did you add "parseInt" to it like I suggested? It does that in the original because meta values are returned as strings.

If you want it to track multiple maps, we'll need to turn the tracker variable into an array rather than a flat value. That's easy enough to do.
Oh no I missed that part. I just edited the code from AquaEcho that I already was testing and changed the "event.meta" to "event.event().meta".

I tried copying the one you posted first and got this messsage in the console:

SyntaxError: Unexpected token ')'
at Game_Interpreter.command355 (rmmz_objects.js:11298)
at Game_Interpreter.<computed>.<computed> [as command355] (VisuMZ_0_CoreEngine.js:9231)
at Game_Interpreter.executeCommand (eval at onScriptLoad (FOSSIL.js:7999), <anonymous>:195:26)
at Game_Interpreter.executeCommand (VisuMZ_1_EventsMoveCore.js:8022)
at Game_Interpreter.update (rmmz_objects.js:9565)
at Game_CommonEvent.update (rmmz_objects.js:6909)
at Game_Map.updateEvents (rmmz_objects.js:6746)
at Game_Map.<computed>.<computed> [as updateEvents] (VisuMZ_1_MessageCore.js:2607)
at Game_Map.update (rmmz_objects.js:6702)
at Game_Map.<computed>.<computed> (VisuMZ_1_EventsMoveCore.js:8022)

So I just went back in and edited AquaEcho's with the adjustments you mentioned.

EDIT: Adding the "parseInt" fixed the calculations!! 😀 Now what would I adjust to change it to just track specific maps?
Oh no I missed that part. I just edited the code from AquaEcho that I already was testing and changed the "event.meta" to "event.event().meta".

I tried copying the one you posted first and got this messsage in the console:

SyntaxError: Unexpected token ')'
at Game_Interpreter.command355 (rmmz_objects.js:11298)
at Game_Interpreter.<computed>.<computed> [as command355] (VisuMZ_0_CoreEngine.js:9231)
at Game_Interpreter.executeCommand (eval at onScriptLoad (FOSSIL.js:7999), <anonymous>:195:26)
at Game_Interpreter.executeCommand (VisuMZ_1_EventsMoveCore.js:8022)
at Game_Interpreter.update (rmmz_objects.js:9565)
at Game_CommonEvent.update (rmmz_objects.js:6909)
at Game_Map.updateEvents (rmmz_objects.js:6746)
at Game_Map.<computed>.<computed> [as updateEvents] (VisuMZ_1_MessageCore.js:2607)
at Game_Map.update (rmmz_objects.js:6702)
at Game_Map.<computed>.<computed> (VisuMZ_1_EventsMoveCore.js:8022)

So I just went back in and edited AquaEcho's with the adjustments you mentioned.
Oh, whoops. I still had the residual closing bracket from her forEach and I missed a bracket. This will work:

JavaScript:
let fPieces = 0;
let fPoints = 0;

for (const event of $gameMap.events()) {
  if (event.event().meta.furniturePoints  && !event._erased) {
    fPieces += 1;
    fPoints += parseInt(event.event().meta.furniturePoints);
  }
}

$gameVariables.setValue(99, fPieces);
$gameVariables.setValue(100, fPoints);

Or you could just change the fPoints line to

JavaScript:
fPoints += parseInt(event.event().meta.furniturePoints);

in what you already have.
Beautiful! Yeah, I went in and copied that
fPoints += parseInt(event.event().meta.furniturePoints);
line and now it's adding the variable correctly!

What line would I change to specify certain maps and maintain that calculation regardless of what map the player is on?

Sidenote: It really fascinates me how you all can just bang this stuff out like it's easy or look at a few lines of code and identify what needs adjusting! Makes me really want to learn it myself, but I always get overwhelmed looking at it and not knowing where to even start to learn it.
Beautiful! Yeah, I went in and copied that
fPoints += parseInt(event.event().meta.furniturePoints);
line and now it's adding the variable correctly!

What line would I change to specify certain maps and maintain that calculation regardless of what map the player is on?

Sidenote: It really fascinates me how you all can just bang this stuff out like it's easy or look at a few lines of code and identify what needs adjusting! Makes me really want to learn it myself, but I always get overwhelmed looking at it and not knowing where to even start to learn it.
The thing to keep in mind there is that I've been doing this for literal decades (and I'm pretty sure AquaEcho is very experienced as well); there's absolutely no shame in not being capable of rattling off reams of code at the drop of a hat. It's probably not something a lot of normal people ever bother to learn how to do. XD I'm always happy to help you out if you want to start learning, though.

Anyway, in order to track it on multiple maps, you'd need to do something along the lines of:

JavaScript:
let fPieces = 0;
let fPoints = 0;
$gameVariables.setValue(99, $gameVariables.value(99) || {});
$gameVariables.setValue(100, $gameVariables.value(100) || {});

for (const event of $gameMap.events()) {
  if (event.event().meta.furniturePoints  && !event._erased) {
    fPieces += 1;
    fPoints += parseInt(event.event().meta.furniturePoints);
  }
}

$gameVariables.value(99)[$gameMap._mapId] = fPieces;
$gameVariables.value(100)[$gameMap._mapId] = fPoints;

This will store a key-value pair object in variables 99 and 100 instead of a flat value, in which the key is the map ID and the value is the number of pieces/total points for that map respectively.

Original thread: https://forums.rpgmakerweb.com/threads/setting-variable-based-on-how-many-events-are-on-a-few-of-the-in-game-maps.165492/