MV Javascript issue - trying to use map ID to adjust relevant variables

Hi, so I'm using the following code to trigger +1 to the relevant variable when placing an item depending on what map you're on. It kind of works, except the first time you place the item it jumps the variable value from 0 to 6, and then weirdly it works perfectly after that, adding 1 each time. Can't work out why it's jumping to 6 first! Any help much appreciated!! <3


if ($gameMap.mapId() === 210) {

$gameVariables.setValue(102, $gameVariables.value(102) + 1);

} else if ($gameMap.mapId() === 122) {

$gameVariables.setValue(212, $gameVariables.value(212) + 1);

}
I'm an idiot, I worked it out, I'm sorry I don't know how to delete this TT_TT
You can't delete threads, but feel free to post your solution for any other people who had a brain fart.
I wonder why you don't do (if its a scriptcall:
$gameVariables._data[102] += 1 to make it easier.


if its a conditional branch with the gameMap ID, than control variables
102 += 1 would be easier than constantly set them.
I wonder why you don't do (if its a scriptcall:
$gameVariables._data[102] += 1 to make it easier.
That's not good practice because game variables don't start initialized.

If the OP hasn't used that variable before, that will be undefined += 1 = NaN.

Using the getter function, $gameVariables.value() and the setter function $gameVariables.setValue() is proper practice unless you have a niche situation where they specifically need to be worked around.
You can't delete threads, but feel free to post your solution for any other people who had a brain fart.
Haha sure! So I have a plugin running for the placeable objects, and ages ago I set up a script command within this (so I'd forgotten it was in there) to also add to the variable when the object is placed! so it was running the command multiple times haha! oopsyyyyy >.<

Original thread: https://forums.rpgmakerweb.com/threads/javascript-issue-trying-to-use-map-id-to-adjust-relevant-variables.182972/