Fast resource management

So I'm working on one thing that I can't figure it out at this moment. Lets say that I have my all game data compressed into zip archive. There's a scripting system in the game that lets you to do some stuff. For example spawn npcs, equip specific items, etc.

Where's the problem? When I'm loading my world that is separated into chunks it's quite easy to load needed assets and other data without any notice. But lets say I write a script that triggers an explosion after a button press. In that moment game needs to load explosion textures, etc. Without separate threads, on slower system, the game would definitely freeze for some time. If I would use separate threads after all - on the slower system it may cause missing textures problem or something like that.

So it's quite easy to load up ahead some resources while loading world chunks, but how to foresee what resources should be loaded for objects that will be seen after script execution?

I don't know if it's possible to understand what I want to do, but I can give another example. In Bethesda's games like Fallout 3 or TES you can use spawn command that shows up constructed npc with all the textures right away. Any ideas how they load up textures and models so quick?

Don't load (heavy) ressources directly. Instead cache them and return the already loaded ressource if possible. Since you haven't specified any language or library, here's some pseudo code:

Dictionary<string, texture> loadedTextures;

texture getTexture(string filename) {

return tex;  // return it

tex = new texture();
log("Failed to load texture: " + filename);
return default_texture;
}

return tex;
}


When done with everything, you'll just have to go through your map/dictionary and clear all members before tearing down your render context etc. for resource cleanup.

This can also be extended with stuff like object lifetime/reference couting, etc.

As for loading stuff so quick: I assume archives with those files are just hold in memory and loading some texture isn't that slow anymore, even on some older hardware.

Also using the code above it's pretty simple to create a short "preloader" that could even be fed by a text file:

void preload() {
// This list could also come from a text file
string[] preloadFiles = {"first file", "second file", "third file"};