MV/MZ Asset Loading / Async Best Practices

Heya! :hwink:


Mind sharing some best practices to do async in MV/MZ? Especially on loading assets.

  1. Any methods you usually use outside bitmap.addLoadListener?
  2. Is it ok to use web dev pattern such as async/await or chaining then ? lol
  3. What to do for loading critical visual assets? Gameplay critical assets, for example. Any patterns for triggering the load screen when not all assets are ready yet? Honestly I don't know how the engine triggers the loading screen, mind pointing me to the code?

Thank you! :hhappy:
you can for Scene_Base update to force "Graphics.updateLoading()" to load it
on the map if you need to test the loading screen.

but personally, I don't use event load listeners or async for the matter.
I just use my own simple preloader on boot if they require images.

different than Galv ImageCache which can load image.

I have no idea how to use async and await stuff.
It's probably fine to use async/await in MZ; I believe MV is too old to support it. Chaining then on promises should work fine as well.

But I would say you should usually only use either of these if RPGMaker itself doesn't provide what you need. To be more specific, if you're loading a JSON data file or an audiovisual asset, you should usually use the functions in DataManager, ImageManager, AudioManager, or (for movies) Graphics. Though, it's not like you absolutely have to.

You can definitely think of your game as a "web page" and use XHR to load files. I'm not 100% if this works when you do a desktop build; if you're aiming for desktop I'd recommend using NWJS file routines instead (you can always use Utils.isNwJs() to distinguish between nwjs and true web deployment).
@ShadowDragon Thanks for pointing me out to the loading code!
Regarding your preloading method, I assume you load all images from your own plugins inside scene_boot? Any performance implications? I guess preloading some MBs wouldn't hurt even if the images won't always be used by the players straight away :hhappy:

@Solar_Flare Thank you for the guidance! :hhappy: I guess the best practice is to "resort to the built-in methods, and only use custom async if you need to" then.

May I have your opinion on these cases?
  1. Minigame plugins
  2. UI plugins
Should we load the assets on plugin initialization? Or on-demand when each scene is launched?
I assume for minigame plugins, it's better to do on-demand loading since not all player will do the minigame straight away.
But the problem with on-demand loading is we need to wait until all bitmap loading are successful. Which to me feels kinda hacky to wait on all bitmap.addloadlistener.

For UI plugins, I think it's best to load during plugin initialization or on Scene_Boot, right?



Sorry for all the questions, folks! You guys are awesome.
You haven't said what you're loading, so I'll assume it's exclusively images.

It's hard to say just from a single word, but if it's a minigame, I suppose it's likely to be a custom scene. In that case, I'd load the images when the minigame's scene is initialized.

If it's not a custom scene (perhaps it's done with events and Show Picture commands), I suspect the default event preloading would be sufficient. RPGMaker automatically begins loading all images an event uses when that event starts (at least, images used by all default event commands; if a plugin command uses an image, it's up to the plugin to handle this, which probably many don't). If you use a lot of large pictures though, maybe there could be an issue.

For UI, I'll assume you're talking about things like buttons or window skins. If they're used a lot (eg a secondary window skin for "popup" type windows), they should probably be loaded in Scene_Boot. If they're not used much, they should probably be loaded in the initialization function of the scene that uses them. This is how the default engine loads ButtonSet, for example.

If you have more specific details on what exactly you're loading, perhaps I can make other suggestions on how or where to load them.
Thank you for the explanation! Those are very helpful. :hhappy::hhappy:
And yes you are correct, I am talking about images.

Let me add a more concrete scenario.
Let's say I am creating a plugin that add a custom scene (minigames, crafting, etc.), then users could add their own images to display as UI/decorations via plugin parameters (struct). What would be the best way to load these image assets? In which inside the plugin I'll convert them to Sprite, so users could apply sprite manipulations (positions, rotation, etc.)

I've tried reading the struct -> convert to array/object of image filepath + positioning data -> for each image filepath, create bitmap, and then if bitmap is successfully loaded, create the sprite -> add to scene.
But sometimes it is not guaranteed that all images are being loaded 🙁

Thank you very much for the help!
First, to enable Exclude Unused Files to work, be sure to declare the parameter as @type file and add @require 1. I hear a lot of people choose not to use Exclude Unused Files because it's easy to end up with things that just can't work with it, but I still think a plugin author should always do their best not to break it. It just takes a few extra lines and you can get most things working with Exclude Unused Files, after all.

When it comes to loading, it really sounds to me that loading in scene initialization is the most logical here. The images are specifically for your custom scene, so they should be loaded when your scene loads. In addition, in case they don't manage to finish loading by the time your scene has finished setting up, you should be sure to refresh the scene once they do finish loading. This applies to anything drawn in windows, at least; I think it may not be necessary if you're creating them as sprites.

Actually, do you even have to wait for the bitmap to be loaded to create the sprite? I think that may not be necessary – you can just assign the loading bitmap to a sprite and it should just work, as far as I know. Of course that could mean that a sprite has no bitmap if it fails to load altogether, but that doesn't seem like too much of a problem, honestly.
A strong way to add preloading that also works perfectly with RPG Maker's loading circle is to use Scene.create and Scene.isReady.

Here's some example:

JavaScript:
const _SceneMap_create = Scene_Map.prototype.create;
Scene_Map.prototype.create = function() {
    _SceneMap_create.call(this);

    // load images or anything else...
}

const _SceneMap_isReady = Scene_Map.prototype.isReady;
Scene_Map.prototype.isReady = function() {
    return (
        _SceneMap_isReady.call(this) &&
        ImageManager.isReady() // <-- or anything else
    );
}

This way, everything is loaded, and you don't have to worry about addLoadListener methods anymore.
It's probably fine to use async/await in MZ; I believe MV is too old to support it.
this works fine in MV if you update NWJS => above 0.48.x as that is
the starting NWJS for MZ.

on a short test with async from stack overload (using console log).
it works fine without problems though.

if they use older nwjs like default (0.29) than they may not be able,
same for using let and const if its default => throw errors.
this works fine in MV if you update NWJS => above 0.48.x as that is
the starting NWJS for MZ.
Sort of - it's fine if it's just for your game, but if you're making a plugin for others to use, you really shouldn't do this.

I'm not sure what you're trying to say about let and const, but they work normally in MV. I can't say for sure if they worked in older versions of MV, but they certainly work in the last version.
Thank you for all the inputs and knowledge, @Solar_Flare, @Aerosys, @ShadowDragon. I learned a lot. You all are awesome. :hwink:
[ISPOILER]I love this community so much haha.[/ISPOILER]
@ShadowDragon Thanks for pointing me out to the loading code!
Regarding your preloading method, I assume you load all images from your own plugins inside scene_boot? Any performance implications? I guess preloading some MBs wouldn't hurt even if the images won't always be used by the players straight away :hhappy:
if you check my MV base, to line 1206, you see how I did the preloading,
but I have no performance issue as it can skip them if they aren't imported.

if it is, than it still runs fine.

there are ways if you could create mini functions and call them on the map
when they are truly needed, but menu based, I just call them, no issues.

I will clean that section up for a better approach that it may load upon entering,
so it can even run better and not load directly when they aren't needed,
and clean it when not required, so the performance should increase.

Original thread: https://forums.rpgmakerweb.com/threads/asset-loading-async-best-practices.181402/