Script Calls for animation

Hello. I just have a question about using a script call to animate images on screen vs manually using the show picture command. The script call I use is convenient, but seems to cause lag. Based on my testing it seems to cause more lag than manually animating with show pictures commands. The call is :

Script: $game.Screen.showPicture(id, 'name_' + $gameVariables.value(variableid), x, y, orientation, x zoom, y zoom, blend)

The call is in a loop with a wait. Even with less frames than the manually animated version the scripted one lags after a few seconds. Is it something I'm doing inefficiently? There are a few of these running at once, but they all use different variables and parallel processes, but even one of these causes slowdown every few seconds.


event.png
your first problem is that you have a triple loop - three loops inside other loops.

because parallel processes loop automatically, making your outer loop a very bad idea, because it breaks the reason for the parallel and makes the wait in it useless.
Like Andar says, you can remove the outer Loop command. Not sure if it'll cause frame drops, but it is unnecessary. Active Parallel events will trigger on any frame that they are not already running.

The "lag" you're seeing might be image loading times. Loading/decompressing a PNG from disk into RAM typically takes more than 1 frame. The load delay only applies to images not already in memory; by default MZ clears its image cache on player transfer. The core scripts look ahead and load images for Show Text & Show Picture commands when an event starts (Game_Interpreter#loadImages). This is not feasible for script calls, so each frame of your animation is getting loaded on demand.

I'm not sure if it'd help, but you could try manually loading all associated images at the start of your event, e.g. something like this in a Script command?
JavaScript:
for (let n = 0; n < 113; ++n)
  ImageManager.loadPicture("beams-white_" + n);
your first problem is that you have a triple loop - three loops inside other loops.

because parallel processes loop automatically, making your outer loop a very bad idea, because it breaks the reason for the parallel and makes the wait in it useless.
Thanks for the reply. That outer loop is fairly new and only a recent attempt at trying to diagnose the lag, it should've been removed. I added an extra 1 frame wait outside that loop thinking that it could be hanging whenever the loop was broken. By that same fashion, would be it be a good idea to make it without any loops?
Like Andar says, you can remove the outer Loop command. Not sure if it'll cause frame drops, but it is unnecessary. Active Parallel events will trigger on any frame that they are not already running.

The "lag" you're seeing might be image loading times. Loading/decompressing a PNG from disk into RAM typically takes more than 1 frame. The load delay only applies to images not already in memory; by default MZ clears its image cache on player transfer. The core scripts look ahead and load images for Show Text & Show Picture commands when an event starts (Game_Interpreter#loadImages). This is not feasible for script calls, so each frame of your animation is getting loaded on demand.

I'm not sure if it'd help, but you could try manually loading all associated images at the start of your event, e.g. something like this in a Script command?
JavaScript:
for (let n = 0; n < 113; ++n)
  ImageManager.loadPicture("beams-white_" + n);
I think this makes sense and is why it seems more noticeable with the script call vs show picture event commands. Thanks for the suggestion. I had Galvs prechache plug-in but either didn't notice a difference or didn't precache enough events. I'll try your script call when I get home from work, thanks for your help!
I had Galvs prechache plug-in but either didn't notice a difference or didn't precache enough events.
or you cached too many events.

preloading is tricky, because the cache is of limited size, and the garbage collector clears the files not used longest whenever the cache gets to its limits.

preload too many files and the first ones will be removed before they are needed.
preload too few and the engine still has loading delays.
override the garbage collector to force files to stay in memory (something that a few such scripts give the option to do), and you basically have a memory leak slowing everything down.
or you cached too many events.

preloading is tricky, because the cache is of limited size, and the garbage collector clears the files not used longest whenever the cache gets to its limits.

preload too many files and the first ones will be removed before they are needed.
preload too few and the engine still has loading delays.
override the garbage collector to force files to stay in memory (something that a few such scripts give the option to do), and you basically have a memory leak slowing everything down.
Hmm. Galvs plug-in precaches the images before the title screen and is supposed to keep them in memory afterwards. But I guess when there's dozens of frames being called via multiple parallel processes, and specifically via script call which as you said causes the game to load the image on demand, the cache would have to turnover often. Maybe with the script call you provided running simultaneously in another parallel process and controlled by another variable that is slightly ahead of the frame of each animation, it can preload the next image or two right before they are needed.
Are you using RPG Maker MV or MZ? You've posted this in MZ Support, but on closer inspection of your screenshot and your comments I'm guessing you're actually using MV. 😅

As far as I'm aware, MZ's image cache is only limited by the game's RAM allowance. MV implemented a size limit, but MZ just clears itself periodically instead (e.g. every Player Transfer). The exact behaviour of a plugin depends entirely on how that plugin is designed.

If you are actually using MV, then consider using a plugin like Community_Basic (included by default in a new project) to increase the image cache size. The default size is 10 MPx, which is very small for non-mobile games. You could try 100 MPx to see if that helps. Remember to save your project to apply Plugin Manager changes before testing~

Original thread: https://forums.rpgmakerweb.com/threads/script-calls-for-animation.155050/