Performance issues with custom script

I have a custom script I'm writing (mainly to learn RGSS3) in which one aspect is that when it rains, an event spawns from a different, template map to the current map around the player. The event runs for ~25 frames with the 'stepping' graphic of a splash afterwards the event deletes itself. The script works perfectly fine except that after about 1-2 minutes, the frame rate gradually drops to unplayable levels. Here is a link to a demo showing what I'm talking about:

https://www.dropbox.com/s/blm0jsbti80umet/Code%20Test.rar?dl=0

and here is a pastebin link to the code:

http://pastebin.com/4KLG0uSm

I also welcome any advice and tips on how to code better too. 😉

Any help I could get with this would be greatly appreciated!
My initial guess would be that maybe you're not removing them properly - memory leak.
Well, the event that is spawning it is set to run this code after 25 frames:

e = $game_map.events.delete(@event_id)e.erase unless e.nil?And $game_map.events.keys verifies that the entry, at least, is gone.
What does $game_map.events.delete return? Why not just put Wait 25, Erase Event on the event itself instead of trying to control it via script?


Or better yet - don't use events at all for the splashes.
Erase Event doesn't actually get rid of the event. It just basically, turns it off, from what I've been informed. Verified this by calling $game_map.events.keys before and after manually 'Erase Event'. The problem is that there are multiple splashes at a time that need to spawn in and out of existence over an undetermined amount of time. Obviously, this would lead to a build up of events eventually causing performance issues. If you check out the demo, you'll see what I'm talking about.

If I could get away with not using events as the splashes, then I'm all for that but I'm not really sure how else to do it. With playing animations, you can only play the animations on a given event. This would require me to have multiple events on every map that it could possibly rain on in order to show the splash animation. Unless you have a different suggestion, spawning events is the best idea I can come up with.

Also, a curious thing I noticed was that if I call a different scene (main menu, debug menu, etc) and go back, the frame rate resets back to 60 and the slow degradation process starts over again.
You need to do a little more research 😉


Erase Event DOES get rid of the event, but not permanently. If you place an event in the editor, it belongs to the map, and it will be loaded every time the map is loaded. That means if you erase it, it WILL be erased, for as long as you're on the map. When you leave the map and return, the event will be loaded again. However, you're not talking about those kind of events here. You are not saving the map settings, so the events you're spawning only belong to the map until you leave it. When the map loads, your spawned events will not be a part of it, and they will not be loaded. So yes, you CAN use Erase Event to permanently get rid of spawned events. And by doing it the way the events are MEANT to be removed, you'll be sure you're getting rid of the sprite and not consuming more and more memory.


As far as animations goes ... you're making a script, but you're limiting yourself to what can be done with events. There's nothing stopping you from creating a new class that also uses animations, like Sprite_Character, but base the position on a coordinate instead of a Game_Character.


There are a few weather / particle scripts around. Find them and see how they handle creating/erasing large numbers of sprites.
Hah, like I said I'm still learning. 😉

I'll try to use the Erase Event command and see if that works better. The only problem I foresee that having is I base the number of splash events on the screen at once with a formula based on the event IDs in an array. And with Erase Event, the event ID of the event you're erasing actually retains its key. But if it works better for performance I could rework that.

And yes, I could create a custom class for animations but I have little to no experience with scripting them. Now would probably be a good time to learn.

And lastly, I've looked at a couple of different weather scripts but they're too obfuscated for my beginner Ruby-fu.

*Edit - That's a no-go also. I changed the code to this:

Spoiler
  def precipAnim    if @splashesTimer >= 60      while @splashCounter < 5        xVariance = rand(8)        yVariance = rand(6)        dirVarianceCalc = rand(2)        if dirVarianceCalc == 1          dirVariance = 1        else          dirVariance = -1        end        splashXpos = $game_player.x + (xVariance * dirVariance)        splashYpos = $game_player.y + (yVariance * dirVariance)        if $game_map.any_direction_passable?(splashXpos, splashYpos) == true          spawn_event(2, 1, 1, splashXpos, splashYpos)          @splashCounter += 1        end      end      @splashCounter = 0      @splashesTimer = 0    else      @splashesTimer += 1    end        end
This test spawned 5 splash animations at the same time on the map every 60 frames. Each event waits 25 frames and then calls the 'Erase Event' command and it still degrades the performance drastically only after a couple minutes. I can try doing this by animations, but I'd REALLY like to figure out what's causing the performance degradation as its had me stumped for quite awhile now.

*Edit 2 - All of this led me to Spriteset_Weather. Maybe it'd be easier to just alias update_sprite_rain and set a counter there.

*Edit 3 - Thanks to Balrogic, it looks like the problem was that the sprite array wasn't being cleared. Everything else in the code seems to work fine. All I had to did was change the local sprite array in the spawining script to a global array and have the event set a class variable to its value in said array. In the process of unspawning the event, I simply added a line to clear that value out of the sprite array and voila, no more performance degradation. One other problem I noticed was that the first frame of the events SOMETIMES stayed behind on the map until the haltAnim value was set to true. Not really too sure why but I simply made the first frame of the splash animation completely blank just so they won't show up on the map for the extra second or so. Anyway, its working fine now!

Original thread: https://forums.rpgmakerweb.com/threads/performance-issues-with-custom-script.38514/