The principle in my idea is not to optimize the current design, but instead to alter the design.
I would instead map the events accordingly to their coordinates.
We could for example do something like this:
$game_map.map_events = {}
$game_map.map_events[[x,y]] = event
# or
$game_map.map_events[[x,y]] = [event1, event2] #so there can be more than 1 event on that tile
Naturally I would implement it in the Game_Map class; it is just to give the idea.
I am not saying that using a hash in that way is the most efficient way, but it is conceptually easy to understand.
Perhaps it would be better to encapsulate the map of events in a class, I am not sure.
Think about how fast collision detection becomes.
If a event moves to the left it is simple just to check what is in
$game_map.map_events[[x-1,y]]This is much much faster than the naive collision detection that is in the original scripts
Another bonus is which events to update.
Just check for events in the visible part of the map and a buffer (a tile or two outside the screen) in the hash-map. Update the found events.
You won't event process outside events.
An added bonus of this is that the size of the map is irrelevant when updating the events.
The cost is simple just more ram usages.
The speed is dependent on how many events are visible.
We can take this and go even more advanced. Why should events outside of the screen have sprites?
Sprites will slow down the game significantly even if they are outside the screen.
1 or 2 sprites make no difference, but what if you have a 500x500 map with loads of events?
500 sprites outside the screen... I think you get my point.
We can do some tricks to make the adding and removing of sprites faster.
We only have to consider which events move into the buffer and out of the buffer. This is why I wanted a buffer previously. Otherwise the player might notice the events blink in and out.
If the player moves to the right then we should consider the new 17 or 19 tiles that comes into the buffer and create the sprites for the events there, while removing the sprites for the tiles going out of the buffer.
Naturally we will also have to check when events move whether or not the go inside or outside of the buffer/visible area.
~ Zeriab