• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Persistent instances

Raoul

Member
Hi all,
I hope someone can help clarify this doubt of mine.

I have a room A where I create an instance of a persistent object (directly from the GUI, drag and dropping the object in the room properties)

At some point the game switch to room B and the instance is carried over correctly.

Question: when I move back to room A, will it create a new instance of the persistent object?

Thanks in advance.

P.S.
I did a test with instance_count and it seems that id does not, as expected, but I am curious to understand why in theory another instance is not created.
 

samspade

Member
Hi all,
I hope someone can help clarify this doubt of mine.

I have a room A where I create an instance of a persistent object (directly from the GUI, drag and dropping the object in the room properties)

At some point the game switch to room B and the instance is carried over correctly.

Question: when I move back to room A, will it create a new instance of the persistent object?

Thanks in advance.

P.S.
I did a test with instance_count and it seems that id does not, as expected, but I am curious to understand why in theory another instance is not created.
Yes. There are a couple ways to handle this. One way is to put code in an object's create event that looks like this:

Code:
with (object_index) {
   if (id != other.id) {
       instance_destroy();
   }
}
This code is based off of the object index, so it will prevent more than one of a single object type and preserve the original. You can modify it to be more specific for example:

Code:
with (object_index) {
   if (name == other.name) && (id != other.id) {
       instance_destroy();
   }
}
This instance would destroy itself only if it has the same name of the other instance. Here name would be a variable you create and set yourself different in each instance either through the room creation code or an object variable. If you are creating everything in the room editor, I would recommend using an object variable.
 
H

Homunculus

Guest
I usually do this to ensure only one instance of a specific object exists at any time:

Code:
//create event
if(instance_number(object_index) > 1) {
    instance_destroy();
}
Seems cleaner to me than using a with and comparing the id, but in the end the result is probably the same.
 

kupo15

Member
This is why you have a dedicated starting room that you initialize your persistent objects in and make it impossible to access that room again. Its the initialization room
 

curato

Member
I would do something similar but I would get the instance_number and only do the create if there were none already. No point creating and destroying something you didn't need to start off with.
 

Raoul

Member
Thanks to all for your valuable replies. All described methods make sense to me, I will see which approach fits better to my project.

One consideration: as I wrote above, I did a test checking the number of instance of a given persistet object and I can confirm that no new instances are created when returning to the room where the object is placed as a persistent one.
My educated guess is that GM only generates the instance once if it is placed in the room editor rather than being called from another oject.
for this simple test I created a variable which increases by 1 at each step and printed the result on screen. The variable was not reset to 0 in the create event when the room was loaded again. I also verified that the id stays the same.
 
Top