Strange FPS drops on activating an event

Hello everybody, i made a test event for my game, and there are strange fps drops (about 10 frames) on the first run of the event. The game normally runs at a stable 60fps, but drops to 47-53 fps.
Second run of the event doesn't show the drop, but when i go into the main menu and restart the game, the drop happens again.
The event looks like this:
1680466951932.png


This is even on a new project with no plugins, taskmanager doesn't tell me a bottleneck, 20% GPU, 5% CPU, 2/3 of the RAM free, SSD has nothing to do, the only thing i can come up with is the file size of the character sets, which is 3MB each (making a project with 1920x1080 and 120x120 tile size with galvs diagonal movement plugins), but this happens even with a preloader plugin. Any reason why this is happening? And maybe a way to get around it would be nice 🙂
I don't know why but it's the same for me if I "change the graphic" for the first time, I think it has to do with the first load of graphics to the memory.
The thing is, i can confirm that the graphics are already in the cache (using console.log for the image cache), but the drop still happens, so it doesn't seem to load it from drive, maybe it happens because it is then first delivered to the graphics card, but that should not give such a huge drop, i would not even expect a drop from system memory to graphics memory for around 10-15MB at all
GPU doesn't really matter in the case of RM except for a few small things, but it's rarely worth considering as a factor when it comes to RPG Maker.
If you are using a pre-loader, be mindful. I believe RM uses aggressive RAM culling in order to make the engine optimized for lower-end devices, therefore any allocated cache it may have might be filled by your pre-loader, and therefore something needs to be culled from the cache first in order to make room for the objects. Pre-loading a bunch of assets may fill that cache faster and has the potential to cause stutters.

You can fake a pre-loader by having all the of the pictures be shown at the start of the event, but set them to 0 opacity. The game will run through all of the pictures in a few frames which should buffer them into the RAM, and then when you call the actual pictures to be shown in the event, it should smooth out the performance.
It's worth nothing this buffering technique has its own flaws. Again, due to what I believe might be aggressive culling within the engine itself, it does just cull unused items from RAM after a fairly short amount of time. So, say for example, your event is a really long event. If you run the image buffer at the start of the event, but you don't actually need to use those images until near the end of the event, there is a chance the engine may have already dumped the buffer and will need to reload it again.
It definitely sounds like an image loading issue.

What preloader are you using? I had to troubleshoot one once, and discovered the way it passed the sprite name to the loading function was not the same as the default scripts, so it was actually loading the image under a slightly different name, and when the time came to use the image, it thought it wasn't loaded, and went ahead and loaded it a second time under the real name.
I'm using Visustella CoreEngine and have put in all 4 of the images in the plugin.
I'm using a console command in a script on another test event to make sure the
images are loaded, and they seem to be... Well i cannot be a 100% sure that the
names are exactly the same, i'll check.

And i also see that there are only about 15 items in the cache, since i go directly to the test map from game start... I'm using RPGMaker MZ version 1.6.1 if that has anything to do with it.

I'm aware of the shortcomings of the image cache deleting everything now and then, looked into the code of the ImageManager.

But i found a solution already, but i don't know if that has any weird side-effects. There is an option in the pixi.js, in RENDER_OPTIONS there is clearBeforeRender which is turned on by default. When i turn it off, there is no frame drop on the preloaded game, and the frame drop on the non-preloaded game is cut in half. It seems to have no visible downside, but i'll have to test if it interferes with anything... In the pixi documentation it says it's only needed if the scene is transparent somewhere and the previous frame could shine through, but since everything is covered by parallax or tiles, i think it is safe to turn off.

@TheAM-Dol:
Do you know what is considered a lower-end device and if the engine even checks if it is on one? It doesn't seem to, and using RAM culling when the whole process is using only 350MB seems a bit over the top, because then lower-end would mean the system has about 1-2 gig of RAM, and i don't think RMMZ would run on such a system very good anyway. Given that rpgmakerweb and steam state it requires 8GB of RAM, this doesn't make sense (couldn't find separate requirements for a deployed project)

Update:
Ok, the pixi.js thing doesn't seem to be consistent, i sometimes have framedrops directly after game start and sometimes i don't... It gets even stranger, when i stay on the map for like a minute and do nothing, the drops happen again, but console.log(ImageManager._cache) still tells me the images are loaded, i have that as a script command inside the event for testing. console.log(ImageManager.isReady()) also tells me everything is loaded.

Update 2:
After further testing, i give up. Even when turning every plugin off, leaving the clearBeforeRender off and clearing the cache on every event call with ImageManager.clear() (leaving the whole map blank except the character image), the drop seems to show only about every 10th time, but 9 out of 10 times i do not have a drop 😛 , so loading the images doesn't seem to be the problem , but i cannot either have it to show everytime to corner it or get it to never show, which is what i want. turning clearBeforeRender on fires it consistently on the first activation, but wildly inconsistently on subsequent ones (between dropping one every 3 times to dropping once every 10 times), so turning it off seems to make it more consistent, less frequent and smaller, but it still happens every 5 to 10 seconds when running the event every second with a loop
MZ culls the image cache on map transfer. It's MV that culls on a memory limit.

When an event starts, both MZ and MV preload images from (for example) Show Picture commands present in the event. [Edit: ah, MZ doesn't check for Change Actor Images commands in Game_Interpreter#loadImages! Maybe that's it?] That load time should be effectively zero if all images are seen to be present in memory, otherwise what you're seeing is the typical result: a minor delay on the first time an event starts.

Note that your images might be 3 MB when encoded as PNG, but once loaded and decompressed they will occupy significantly more RAM. For example, I believe MZ uses a 32 bpp RGBA format, under which a 1920 x 1080 px image occupies 1920 x 1080 x 4 B...a bit under 8 MB. I don't think that should have any notable impact here, but it's worth considering.

Something to try - turn off all plugins and save your project to apply Plugin Manager changes. Add a Parallel event to that map that looks like this:

◆Comment:Request relevant images from disk. : :0 opacity so player doesn't see them. ◆Change Actor Images:DummyActor, Actor2(0), test1(0), Actor2_1 ◆Change Actor Images:DummyActor, Actor3(0), test2(0), Actor3_5 ◆Comment:etc. ◆Comment:Do once per map visit. ◆Erase Event
This should run once, when the player enters the map. It's not a general solution because it won't process again on load. However, if it eliminates the delay at the start of your test event then I think that pretty much confirms the preloader plugin is not working as intended. In that case you could be on the lookout for:
  • A bad configuration (Plugin Manager),
  • A conflict with another plugin (including an outdated plugin from the same library),
  • A mismatch in core script versions (e.g. running on v1.5 but plugin designed for v1.6), or
  • A bug in the plugin itself.
(Also, in case it's relevant: if you log an object to the console, its properties will be evaluated at the time you expand the object in the console, not at the time the parent object was logged.)
Tried what you suggested, but the initial drop in frames is still there, new game on an empty map with only 2 events, mine and the parallel one, no plugins or common events, still does so on start, but the intermediate drops seem to be gone.

Setup of the parallel event is:
1680523789055.png


Good to know that i cannot rely on the readout of the console for objects, but did print out the URLs cached with a for loop, and the images were there. And i guess the images are much larger than 8MB when unpacked, since i'm using a full character set, but i'm not using it for a tilesize of 48x48, my tilesize is 120x120 and my char size is double the tile height, so the images are 5120x1920, which would make it around 40MB per picture when unpacked. Disabling clearBeforeRender of pixi still seems to cut the drop about in half. Tried adding another preloader plugin to that setup, but still had about a 6 frame drop on the first event start. Storage doesn't seem to affect it because i loaded it from a sata SSD with max rates of around 600MB/sec and an NVMe with around 2GB/sec (i know it's not sequential read that matters, but random access, but the NVMe is a lot faster on that too), and the drop of frames stayed the same.

I know the drop is not that much, but when played on other computers i guess it will be way higher on systems that still meet minimal specs
I noticed that my suggestion will only actually "preload" the last set of images (in your case: Actor1, test1, and Actor1_1), because MZ, unlike MV, only scans ahead for Show Text and Show Picture commands. The sprite itself is responsible for loading its new image (if it has changed), but the spriteset updates before the event interpreter each frame, so it won't see the new image assignments until next frame. 😴

You can try replacing all the Change Actor Images commands in the Parallel event with this Script command:

for (const face of ["Actor1","Actor2","Actor3","Evil","Monster"]) ImageManager.loadFace(face); for (const char of ["test1","test2","test3","test4","test5"]) ImageManager.loadCharacter(char); for (const svA of ["Actor1_1","Actor1_5","Actor1_7","Actor2_1","Actor3_5"]) ImageManager.loadSvActor(svA); this.setWaitMode("image");
I.e. directly load all those images. The last line tells that event to not process any more event commands until the loading is done, so you can put a Show Text or something afterwards to check how long it takes. It's possible there is some external interference slowing things down, e.g. OS security wondering why this Chromium browser (playtest environment) wants to read a bunch of local files?

Also: I think most systems nowadays support texture sizes up to 4096 px (square), or 2048 px if you're aiming for mobile and/or really want to play it safe. 5120 px is pretty big...I don't know what handling, if any, PIXI has for splitting/allocating oversized images to internal texture units, but you may want to downsize things just in case. For example, you can split each character onto their own sheet: 8 $actorX.png images rather than 1 actors1to8.png sheet. 😮‍💨

Otherwise I'm out of ideas~

PS - you can rely on the console, just don't misunderstand what it's reporting. In JavaScript objects are passed by reference (i.e. pointer), not by value: the console works on the same basis.
Ok, tried this code with giving out an accurate time at start and end, and it says it's less than 1 millisecond:
1680581155604.png

1680581163336.png

Strange thing is the drop is still there, even with this script, so it doesn't seem to be the loading that is causing it. Tried the same with an empty MV project, and the same happens there, but the frame drop is about 25 frames on the first activation, 0 on each consecutive activation, same computer, same drive.

Texture size isn't a concern with me, i plan on releasing on PC, and the oldest PC i found (a notebook which isn't even capable of running MZ due to old drivers) already has support for 4096x4096 textures with an integrated GPU, and that computer is 12 years old. Every iGPU or dedicated graphics card i could find that could run 1920x1080 fast enough has at least support for 8192x8192, since that seems to be the minimum requirement for DX10 on windows. Characters seem to be handled differently anyway, it seems only the part that is needed is drawn onto the texture, so individual characters can be as large as 2048x2048

I made tests with much larger images on MV (resolution of 3840x2160 with 512x1024 per sprite for a total of 16384x4096) and they load just fine, but didn't try that with such a short load order.

I can get around that with putting everything i would need into 1 spritesheet and not changing face and SvActor ( which would put it down to 3 changes within 1 spritesheet and a last change with the face and sv image), but i would need a spritesheet for every movement direction (or pack all into one, which will still make it a smaller as one of the current ones), but it will take a much larger script to do so, since the event is meant to make the character invisible (skill with common event later on), and i plan on letting him stay in the direction he is facing when doing so. That also means it has to be allowed on every or almost every map the game has...

Since i use the plugins of galv, splitting the character sheet isn't possible, since there is only one character on the sheet, with idle, walking, running and diagonal movement

Update:
Ok, seems to be CPU sensitive, if i give my CPU 100% workload, loading time of the images is at about 1.5ms, but the drop increases to 40, but 2nd activation still shows 0 drop
i tried different versions of pixi, and pixi 5.3.12 seems to yield the best results (already had that installed 😛 ), with 5.2.4 being the worst and dropping even on 2nd and consecutive activations by almost 20 frames, and 6.0.4 being slightly behind 5.3.12, each with 100% CPU load
Largely irrelevant if it hasn't removed the delay, but note that you've only measured the time necessary to send the image fetch requests. Images are loaded asynchronously! This is why I carefully stated that the interpreter wait mode prevents the interpreter from processing any more event commands. That also explains why your measurements are primarily CPU-dependent (possibly just from compiling the script call).

Regarding texture size: OK, that's fair.

Bear in mind that the engine is written for a specific operating environment. If you've made changes like manually updating PIXI, that's definitely something you should have stated up-front, along with any edits to the core scripts, HTML, or runtime (NWJS).
Ok fair, pixi is the only change made, but with the standard pixi it is way more noticable. The second event gets fired manually about 5-10 seconds after the first event put out the reading, so even if the reading is wrong, it should have loaded by then

I tried deleting everything, reinstalling from Steam and making a completely new project, so everything is standard, but still have the drops.

Update:
Ok, found out that if i combine the two suggested ways (script and after that change actor image) with a wait time of 1 frame inbetween the actor changes, the framedrop is in the first event (which i may be able to hide while changing maps), but the second event is working fine, but as soon as i set the actor to invisible while doing so, the second event has the drop again. Seems like the images are decoded when being displayed, not when being loaded, and with a size of 40mb, it just takes longer than the 16 milliseconds per frame. Tested further and fadeout/fadein which is done on map change does not shift the frame drop from the first to the second event, so i have found a way which works for me.

Even wrote a small plugin to confirm it isn't unpacking on load, which provides a script command that lets you load every png file in a given directory with ImageManager.loadBitmap, and the memory consumption rose by about the filesize within the directory loaded.

Original thread: https://forums.rpgmakerweb.com/threads/strange-fps-drops-on-activating-an-event.156351/