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:
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:
After exiting the level and checking the log, there was no entry.
In the same level, I added
This time, the log showed:
This, in itself, contradicts both the manual and the information we have held for years.
The agent provided the following explanation:
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:
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.
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.