When I switch from room B to room A, is everything in room B destroyed?

K

krugen

Guest
When I switched from room b to room A, is everything in room B destroyed except for the global variables?
 

TheouAegis

Member
Actually, no.

Instances are removed, but not destroyed -- assuming they're not persistent. They will no longer occupy memory, but they will not run the Destroy Event. Use the Cleanup Event for that.

Any resources (e.g., sprites, surfaces, data structures, buffers, audio stuff, etc.) created through code will not be destroyed, as they exist outside the room.
 

Sabnock

Member
you need to learn about persistent instances (rooms can be persistent also)

persistent

Description
This variable can be read to find out if the instance is flagged as persistent or not, or it can used to set persistence to true (persistent) or false (not persistent) for the instance. A persistent instance is one that will be "carried over" from room to room, meaning (for example) that it only has to be created once at the start of the game and it will be present in all further rooms. Care should be taken with persistence as it is easy to lose track of persistent instances which can lead to problems later in the development of the game.

When you create an instance with persistence enabled in a room, this variable will be set to true and it will be assigned a layer or a depth (depending on which function you used to create the instance of the object). When the room is changed, and if the following room does not have a layer with the same name or depth as the one assigned, then a new layer will be created for the instance that is persisting across the rooms. If you gave a named layer when the instance was created, then the new layer will also be named the same as original layer, while if you assigned a depth to the instance then the new layer will be named "_layer_XXX", where "XXX" is a hex value used to give the layer a unique name. These layers will be removed from the room on Room End. Another thing to note about layers and persistent instances is that if you have assigned the instance a named layer on creation, and there is another layer in the following rooms with the same name, then the persisted instance will be assigned to the layer with the same name regardless of the depth of the layer. Finally, if a persisted instance moves to a room with a layer at the same depth as the instance was created on, it will not be assigned to this layer, but instead a new layer will be created at the same depth (following the naming convention explained previously).

A persistent object will still have its Game Start, Game End, Room Start and Room End events triggered, however if you restart the game (with, for example, the game_restart() function) all persistent objects will be removed and only exist when created again by the game. Also note that if you deactivate a persistent object, it will no longer pass from one room to another unless re-activated before the Room End event is triggered.
 

Sabnock

Member
you should see rooms as individual episodes in your game and set them up as such each time you enter one. carrying things over from one room to another can be confusing and game breaking so use judiciously.
 

Yal

šŸ§ *penguin noises*
GMC Elder
Instances are only destroyed if their Room End event (which runs just BEFORE the room ends) contains an instance_destroy(), otherwise they're just removed from existence without being destroyed... unless they're persistent, in which case they're moved over to the next room. They still get a Room Start and a Room End event during this transition, but no destroy or create events.
 

TheouAegis

Member
Don't use game_restart() ever. The function should literally be a full memory wipe, resetting everything, and I mean everything, to null/zero values, but that is not the case. Unless you are the type of person that keeps track of every single little thing ever created at any point during your game that may or may not still exist in the memory right up to the point where you call game_restart() and, then remember to destroy every single one of those created resources during the Game End event... using game_end() causes memory leaks. Maybe they have to fix this and recent versions of Studio 2, but in every other iteration GM, the issue exists.

Just a friendly disclaimer.
 
Top