Script call optimisation

Hi Guys,

I am using Yanfly's SaveEventLocations script and I noticed that the reset function does not reset the self switches of the events. I compiled a script call manually, I asked a question but after some going back and forth it seems that it worked out as I want.

Two questions:
1) It seems that I can drop the ; signs after each of the variable definitions and nothing breaks. How come?
2) Is it an okay script call in general? Do you anticipate issues?

Code:
var mapId = $gameVariables.value(3);
var events = $gameMap.events();
for (var i = 1; i <= events.length; ++i)
{var key = [mapId, i, "A"]
$gameSelfSwitches.setValue(key, false);}
1) You can, but you shouldn't, because the only reason js allows it is because js interpreters have functikns that try to fill in missing ; if they notice it. And that can break the program sometimes.
2. It is an okay script, which will run just fine (if you want to keep the last event with self switch A on. If you want the last event to turn off too, then you have a bug in your for loop, because you stard with i = 1, but you didn't increase the i < by 1 as well), but unnecessarily complicated.
Here it's a bit simplified:
Code:
var events = $gameMap.events();
for (var i in events)
{var key = [$gameVariables.value(3), i + 1, "A"];
$gameSelfSwitches.setValue(key, false);}

Or if you want to completely minify the code:
Code:
for (var i in $gameMap.events()) $gameSelfSwitches.setValue([$gameVariables.value(3), i+1, "A"], false);
Both did not work. I guess it is somehow related to the i in $gameMap.events() check but I cannot know. I always feel as if you are making stuff up on the go with these scripts. 😀
Although I used a bit of your ideas to cut a line or two:
Code:
var events = $gameMap.events();
for (var i = 1; i <= events.length; ++i)
{var key = [$gameVariables.value(3), i, "A"]
$gameSelfSwitches.setValue(key, false);}
I also checked it in a small map and it seems to be working for all 3 events there.
Thank you for the input nonetheless.
Yeah "var i in $gameMap.events()" doesn't work because that's looping through the event itself, but you need the event ID#. That's why incrementing i from 1 to events.length works, because it is an integer (and event ID is an integer).

Another 'one-line' option is:
Code:
$gameMap.events().forEach(e=>$gameSelfSwitches.setValue([$gameMap.mapId(), e.eventId(), 'A']))
But honestly, if you have a working script call there is no reason to change it, the most important thing is that it works. You can study this for educational purposes though.

Edit:
For MV v1.5.2 and under, the equivalent would be:
Code:
$gameMap.events().forEach(function(e){$gameSelfSwitches.setValue([$gameMap.mapId(), e.eventId(), 'A'])})
I like synthesizing algorithms. And also, would not it work faster when it has less operations? Thank you for this one line option. I will check it as well. Thank you both for your time.
Thank you for clarifying it @Aloe Guvner. I knew I was wrong, but had no option to fix it, because I was going to work 😀
@kartacha less operations isn't always faster, because some operations are more expensive than others.
In this case, turning a self-switch off is simply deleting a key from an object, I doubt you would see any performance difference between different strategies even if you had 1000 events on the map.
That's right. Some things are more expensive than others.
However, there's another thing... Performance gain/loss vs. ease or readability.

For example, if you have four lines of code and shrink it to 1, the performance gain from that is negligible due to the power of our cpus.
However, these 4 lines of code are easier to read than the 1 line of code.
So if you feel like writing the longer way, feel free to do so, especially if the performance gain is small.
There are, however, cases where performance gets a huge slap in the face.

One of them is if else. If else, compared to switches, are very inefficient in Javascript and if you stack up some of them, you'll feel it.
Another huge problem in JS are function calls. Unfortunately in the mv engine there's quite a bunch of them, because every sprite in mv has an update function fired in 60 fps. Oftentimes quite unnecessarily as well.
Thank you all for your guideance! I will stick to my "descriptive" style when I am learning to code myself. And if I feel some performance issues I will make sure to touch base with you again.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/script-call-optimisation.97503/