SOLVED Simple question about changing variables in multiple objects at once

Zahk

Member
Sorry for a basic question. Say I write some code that says something like:

GML:
if (instance_exists(obj_thing)) {

    with (other) {
    
        switch_activated = true;
    
    }

}
If there are multiple instances of obj_thing, will GMS change the variable for all of them?
 

chamaeleon

Member
Sorry for a basic question. Say I write some code that says something like:

GML:
if (instance_exists(obj_thing)) {

    with (other) {
   
        switch_activated = true;
   
    }

}
If there are multiple instances of obj_thing, will GMS change the variable for all of them?
Since "other" will always refer to a single instance, the answer is no.
 
D

Deleted member 45063

Guest
I would recommend to read up on the documentation of other and it's different meanings. Now, looking at that piece of code my initial reaction is to say that it will either crash, not change the variables of anything or of a single instance. If you want to change the variable in all the instances of obj_thing then you can just do (you don't need the if):
GML:
with (obj_thing) {
  switch_activated = true;
}
If you just want to change the variable for a specific instance of the object then you need to use the instance id instead of the object index. You will either have it already (might be other depending on the context) or will need to query for it (for example with instance_find or another search function).
 

chamaeleon

Member
@Zahk Make sure you understand that there's no connection between calling instance_exists() and "other" having some particular value. "other" is used/useful when you are in a collision event, and inside a with() statement (your current use was not inside the with statement code block..)
 
Top