unloading models from stages

O Ilusionista

Captain 100K
I wanted to clarify some information regarding the unloading of models, so I asked an agent for help. According to the manual, you can include a second argument after the name of an entity being spawned in a level; this argument is supposedly intended to force the model to unload once the level ends:

spawn {name} {bi}

{name} is the name of an entity defined in a .txt file that has been loaded via MODELS.txt.
{bi} determines whether the model remains in memory after the current level ends.
0 = The model stays in memory.
1 = The model is unloaded when the current level ends.
{name} will be spawned (created) in the current level. Its location and attributes are determined by the subsequent fields.

However, according to the agent—specifically regarding version V3.0 6391—this information is incorrect. I tried using a different model and asked the question again, but the result remained the same. The agent suggested the following test:

spawn afake 1
coords (...)

After exiting the level and checking the log, there was no entry.

In the same level, I added "load afake 1" and repeated the process.
This time, the log showed:
"Unload 'afake' ............Done."

This, in itself, contradicts both the manual and the information we have held for years.

The agent provided the following explanation:

engine/openbor.c:
case CMD_LEVEL_SPAWN (lines 15109–15135): If the model is already loaded, it is used as-is, without changing its flag. If it is not loaded, the engine calls load_cached_model(GET_ARG(1), filename, 0) with a fixed value of 0. The second argument—GET_INT_ARG(2)—is never read.

In other words, the value provided after the entity name is never actually read, which means the model remains in memory. The only way to unload a model is by using "load model 1" during a stage. Here is the explanation:

"load X 1": The `free_model()` function (lines 5295–5350) releases animations, defenses, scripts, colormaps, and other components of the model. It does not touch the sprites; sprites are stored in a global list, separate from the models.
When the model is loaded again, `loadsprite()` (lines 4254–4290) first checks for an already-loaded sprite with the same filename and reuses it, avoiding a redundant read from the disk. Therefore, unloading with "1" only frees the memory occupied by the model and its scripts—exactly as the manual states.
"load X 2" (bit 2): The `cache_model_sprites(temp, 0)` function deletes the sprites. However, these sprites are shared—based on their filename—across all models that use the same .gif or .png file.
"load X 3": Deletes both the sprites and the model itself.

Why I believe options 2 and 3 cause bugs (this is a deduction based on the code, not something I have tested): Deleting the sprites also removes the images used by any other model that happens to share those same files. Furthermore, with option 2, the model remains in memory; when it is spawned in the next level, the engine reuses it without reloading the sprites. This aligns with the behavior you have already observed.

I haven't tested option 2, but option 3 definitely causes bugs when used for entities that have scripts—it triggers a silent crash in the engine.

I must admit I’m confused about the correct way to unload models that we no longer need—for instance, enemies that won’t be used again after a certain stage.
 
@O Ilusionista,

There are a few separate mechanisms involved here, and it helps to distinguish them.

For build 6391, your finding about spawn afake 1 is correct. The level parser does not read that second argument. The separate load afake 1 command does, including when the model is already loaded. The manual’s description of that particular spawn argument is indeed wrong. Thing is, I don’t recall ever describing an unload argument for spawn. My explanation of the unload flags was for load {name} {unload}. I don't have any idea why the legacy manual lists an extra argument for spawn. If I ever repeated that claim myself, I was sorely mistaken.

Beyond that discrepancy, though, removing an entity, unloading a model, and releasing its cached assets are separate operations.

Entities use a pool of reusable storage. Let’s say you have 132 entities in play, and the 131st is killed - technical removal, rather than merely entering its death animation. That entity’s life in the game is over. Its allocated storage, however, remains available for reuse.

When another entity needs to spawn, the engine finds an available slot, resets it, assigns the new entity’s values, and puts it into play. During normal spawning, the pool only needs to grow when its existing capacity is exhausted. This has very good reasons behind it. Reusing allocated storage avoids repeatedly asking the memory allocator to create and destroy the same basic structures. That reduces allocation overhead and memory churn during gameplay. It is one of those small engine optimizations most creators never need to think about.

Models have a separate lifecycle. The engine retains a registration containing the model’s name and the information needed to load it. The loaded model template can be freed while that registration remains. OpenBOR can therefore still know what a model is and where to find it after unloading its template.

Sprites also have their own cache. Consequently, unloading the model template does not automatically mean its sprite data has been released. That is why the unload options distinguish between the model and its cached resources.

For the stage load command in the build you are discussing, 1 requests model-template cleanup when the stage ends. The separate 2 flag requests cached-resource cleanup, and 3 combines both. Despite the resource-cleanup function’s name, it also processes certain sound samples.

C:
temp = getFirstModel();
do
{
    if(!temp)
    {
        break;
    }
    if((temp->unload & 2))
    {
        cache_model_sprites(temp, 0);
    }
    if((temp->unload & 1))
    {
        free_model(temp);
        temp = getCurrentModel();
    }
    else
    {
        temp = getNextModel();
    }
}

So load afake 1 does request unloading of that model’s template after the stage. It does not request removal of its cached sprites, and the amount of memory recovered depends on which resources are being released.

The crash with 3 is a separate issue. Shared resources are a reasonable thing to investigate, but the explanation quoted from the agent is still a proposed cause. Establishing why your particular module crashes requires tracing or reproducing that failure.

HTH,
DC
 
Back
Top Bottom