MV SAN_ResidualSprites help with running code

For context, my goal is to apply an afterimage to ALL events on the map using SAN_ResidualSprites.
This is tied to an event that will run javascript code with a parallel trigger
Now, how I'm trying to do this is by using a for loop like so:

for (i = 0; i <= $gameMap.events().length; i++){
$gameMap.event(i).residual().setValid(true);
$gameMap.event(i).residual().setColorTone([0, 0, 0, 0]);
$gameMap.event(i).residual().setOpacity(50);
$gameMap.event(i).residual().setDuration(30);
$gameMap.event(i).residual().setPeriod(5);
}

What I expected this to do is, upon every loop it would:
Set i to 0 to (however many events are on the map, let's say 30 for simplicity).

count from 1 to 30 (which is i++) and for each number it counted it would run the rest of the code, then loop back to 'for'.
the issue is that it just doesn't do that, it's instead undefined? i assume it RUNS the gameMap.event code but as soon as it reaches i, it must count off all the numbers, then get confused or something.
1764864486320.png


Upon fiddling with thing i did find out putting "28, 29, 30" in place of "i" also did not work so I assume listing off multiple events do not work.
I also found out putting "events()" or "events().length" in place of "event(i)" does not work, i assume an oversight on the developers end.
Using the for loop accompanied by $gameMessage.add(i) works perfectly fine.
1764865277059.png

1764865445952.png


So, yeah i don't know if there's any listing methods I need to do or if there are any different loop methods i can use I'm just pretty confused.
if you have any questions feel free to ask and any help is appreciated.

Link to the plugin code here
Hmmm you start at 0, you can't go up to the length. It has to be < length. Event 30 is [29], right?
You need to start with i = 1 instead of 0 since $gameMap.event(0) is undefined and causes an error for the residual() Method. Using this for loop may result in another error if it gets to an event that does not exist (like if you accidentally delete event 15 but never fill it back into the map).

If you can, use a for-of loop instead to iterate through all events on the map without needing to know the ID (in case there is a missing gap for IDs):
JavaScript:
for (const event of $gameMap.events()) {
    event.residual()...
    ...
}

Edit: here is your code with the for-of loop:
JavaScript:
for (const event of $gameMap.events()) {
    const res = event.residual();
    res.setValid(true);
    res.setColorTone([0, 0, 0, 0]);
    res.setOpacity(50);
    res.setDuration(30);
    res.setPeriod(5);
}
Oh yeah good point, I thought they were checking $gameMap.events()[i], where you can check the 0.

EDIT: And which doesn't have those empty events.
You need to start with i = 1 instead of 0 since $gameMap.event(0) is undefined and causes an error for the residual() Method. Using this for loop may result in another error if it gets to an event that does not exist (like if you accidentally delete event 15 but never fill it back into the map).

If you can, use a for-of loop instead to iterate through all events on the map without needing to know the ID (in case there is a missing gap for IDs):
JavaScript:
for (const event of $gameMap.events()) {
    event.residual()...
    ...
}

Edit: here is your code with the for-of loop:
JavaScript:
for (const event of $gameMap.events()) {
    const res = event.residual();
    res.setValid(true);
    res.setColorTone([0, 0, 0, 0]);
    res.setOpacity(50);
    res.setDuration(30);
    res.setPeriod(5);
}
Absolutely fantastic, thank you so much! Usually java stuff always starts at 0 so I was considering that instead of RPGmaker rules. Also i never actually considered how deleting an even would effect it, so that was good thinking ahead for me
1765031476604.png
1765031506894.png
Absolutely fantastic, thank you so much! Usually java stuff always starts at 0 so I was considering that instead of RPGmaker rules. Also i never actually considered how deleting an even would effect it, so that was good thinking ahead for me
View attachment 354412View attachment 354413
Glad it is working!

All arrays start with 0, except that the way RM sets up some of its databases make it 1 (by placing a null for 0, etc.).

RpgLord's code would work great as well since $gameMap.events() is based on events on the screen and not the id, meaning that when it iterates through the array from that method, there will be no gaps, allowing you to use i = 0; i < array.length ; i++ ..., etc.

What you used was the .event() method which requires the id as a parameter to get the specific event. The .events() method returns an array which allows you to get the index with events()[id] events[indexNumber].
The .events() method returns an array which allows you to get the index with events()[id]
That's misleading. The index is not the same as the ID if you're using events(). For example, you might find that $gameMap.events()[2].id == 12.

Note that erased events (erased with the Erase Event command) will still be in that list – erasing an event only sets an "erased" flag on the event. Events deleted from the editor won't be, of course.
That's misleading. The index is not the same as the ID if you're using events(). For example, you might find that $dataMap.events()[2].id == 12.

Note that erased events (erased with the Erase Event command) will still be in that list – erasing an event only sets an "erased" flag on the event. Events deleted from the editor won't be, of course.
How is that misleading? I'm pretty sure I said what you said, right?

The index allows you to get an event. I never specified id.

Ah, my mistake, I wrote id at the end. I meant index, not id.Method. (edit: corrected above, thanks Solar_Flare!)

Also, there is no $dataMap.events() method.
How is that misleading? I'm pretty sure I said what you said, right?

The index allows you to get an event. I never specified id.

Ah, my mistake, I wrote id at the end. I meant index, not id.Method. (edit: corrected above, thanks Solar_Flare!)

Also, there is no $dataMap.events() method.
$gameMap, whoops.

Original thread: https://forums.rpgmakerweb.com/threads/san_residualsprites-help-with-running-code.181294/