Cleanup β
Overview β
Cleanup, also known as "unloading" or "destroying" a room, is typically used when the loaded room contents are no longer needed.
For example, if you're doing chunking by dynamically loading and unloading parts of a level as the player approaches, or regenerating a procedural layout and need to clean up the previous one before loading a new one.
MERGED LAYERS
When ROOMLOADER_MERGE_LAYERS is enabled, layers reused from the host room are not tracked by Payload and will not be destroyed during .Cleanup().
NEWLY CREATED INSTANCES
- Instances you create on a Payload-tracked layer will be destroyed during .Cleanup(), since destroying a layer destroys everything on it. To avoid this, create such instances on untracked layers or use instance_create_depth() instead.
- Instances you create on an untracked layer won't be destroyed during .Cleanup(). Make sure to handle their cleanup manually.
.Cleanup() β
Destroys all created layers and their elements. After calling this method, the Payload instance should be dereferenced to be picked up by the Garbage Collector.
js
// When you load the room
payload = RoomLoader.Load(rmExample, x, y);
// When it's time to unload the room
payload.Cleanup();