1
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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) {
    texture tex = loadedTextures.find(filename);

    if (tex != null) // texture is already loaded
        return tex;  // return it

    // texture isn't loaded, load it
    tex = new texture();
    if (!tex.load(filename)) {
        log("Failed to load texture: " + filename);
        return default_texture;
    }

    loadedTextures[filename] = tex;
    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"};

    foreach(string f in preloadFiles)
        getTexture(f);
}
\$\endgroup\$
2
  • \$\begingroup\$ You mean they load all their archives into RAM? \$\endgroup\$
    – Tom
    Commented Oct 13, 2013 at 10:34
  • \$\begingroup\$ Not necessarily. Their archives consist of an index/dictionary as well as the actual data. I could imagine them using memory mapping to access the files and read parts of them as necessary. They don't open/read/decompress/close the files for every single file being accessed. If you want, grab a tool such as Process Monitor from sysinternals.com and monitor the game accessing your hard disk. \$\endgroup\$
    – Mario
    Commented Oct 13, 2013 at 13:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .