Windows How would I change the instance of another object in code?

A

ArcaPlaysYT

Guest
So I want one object to check if an object exists. If that object doesn’t exist, then it changes that object to another object.
This may seem confusing, I’ll try to put a code together later
 

samspade

Member
Do you mean object or an instance of an object?

The first part is easy if you mean object. You can use instance_exists with the object id. You can also use instance_exists to see if an instance of an object exists but you would need the instance_id to do that.

But I don't understand the second part. How can you change a non existent thing to something else?

Here are two resources if the differences between objects and instances don't make sense:
 
A

ArcaPlaysYT

Guest
Do you mean object or an instance of an object?

The first part is easy if you mean object. You can use instance_exists with the object id. You can also use instance_exists to see if an instance of an object exists but you would need the instance_id to do that.

But I don't understand the second part. How can you change a non existent thing to something else?

Here are two resources if the differences between objects and instances don't make sense:
im going to have a random number generated be the number of seconds. Say if Object 1 has a character in it, and it "moves" to another "room" (Object 2).
So something like
GML:
if alarm[0] == 0 {
//change instance of Object2NoCharacter to Object2WithCharacter
}
im not sure how to do that

Edit: Im thinking the "with" construction will work
 
Last edited by a moderator:

Zhanghua

Member
You can't change the instance directly.
But you can destroy the current instance and instance_create_xxx an other.
 
You can't change the instance directly.
Wrong. You can change an instance from being one object type to another.

To change an instance into a different object instance you would use instance_change
instance_change
So if in your example the alarm is on the object that you want to change, then you would just do something like:
GML:
instance_change(Object2WithCharacter, false);
I used false when calling instance_change as I wouldn't want the destroy/cleanup or create code to be run for the two object instances (the one being change from, and the one being changed into).
 

Zhanghua

Member
Wrong. You can change an instance from being one object type to another.

To change an instance into a different object instance you would use instance_change
instance_change
So if in your example the alarm is on the object that you want to change, then you would just do something like:
GML:
instance_change(Object2WithCharacter, false);
I used false when calling instance_change as I wouldn't want the destroy/cleanup or create code to be run for the two object instances (the one being change from, and the one being changed into).
TKS, Got that.
I haven't use this function before.
 

Let's Clone

Member
Wrong. You can change an instance from being one object type to another.

To change an instance into a different object instance you would use instance_change
instance_change
So if in your example the alarm is on the object that you want to change, then you would just do something like:
GML:
instance_change(Object2WithCharacter, false);
I used false when calling instance_change as I wouldn't want the destroy/cleanup or create code to be run for the two object instances (the one being change from, and the one being changed into).
I wonder if, in the back-end, this does the same thing as destroying the current and creating a new instance of the intended type.
Either way, I had no clue that this functionality existed. Thanks
 
I wonder if, in the back-end, this does the same thing as destroying the current and creating a new instance of the intended type.
Probably, however if you just did an instance_destroy/instance_create_* (depth or layer) then the destroy/cleanup and create events would all be triggered. At least using instance_change you have total control over whether these should be run or not through the second parameter of the instance_change function.
 

Nidoking

Member
Also, instance_change preserves the values of the instance variables through the transition; at least, the ones that aren't reset during create and destroy events, if you execute those.
 
Top