SOLVED With / other script scope?

Neptune

Member
Does this check out? Can you use other from within a script within a 'with'?
1611174291681.png
Kinda brain fried, thanks for any info!
 

kburkhart84

Firehammer Games
Yes, other has two places it works in...in with(){} statements, it changes instance variables from the other instance that has the with(){} statement, and in collision events, it accesses variables in the other object that is being collided with.
 

chamaeleon

Member
I would think so, although I haven't done it. It's just a variable that will have some value in the proper context, and calling a script will in itself not change values of variables.
 

kburkhart84

Firehammer Games
Just remember though, if you call that script that uses the other variable, and you are not in a with(){} statement or collision event, you will get either some kind of error, or possibly get wrong behavior, but more likely the error.
 

chamaeleon

Member
Just remember though, if you call that script that uses the other variable, and you are not in a with(){} statement or collision event, you will get either some kind of error, or possibly get wrong behavior, but more likely the error.
My experience with using other outside the scope of with() or collisions, is that it behaves as the current instance.
Create event
GML:
a = 1;
b = 1;
Step event
GML:
with (other) {
    a++;
}
other.b++;
GML:
draw_text(10, 10, string(a));
draw_text(10, 30, string(b));
draw_text(10, 50, string(id));
draw_text(10, 70, string(other.id));
Draws incrementing numbers for the instance and the same instance id for both id and other.id. I am not advocating relying on this, it's just a behavior I've observed.
 

kburkhart84

Firehammer Games
My experience with using other outside the scope of with() or collisions, is that it behaves as the current instance.
I'm guessing they have some kind of failsafe in there then. They should really shoot out an error instead. I would count that as wrong/undefined behavior as I mentioned. Neat to know though.
 

Neptune

Member
Sooo in the script, I know Im able to access instance variables of the with(obj). However 'other' wont work from there?
 

kburkhart84

Firehammer Games
Sooo in the script, I know Im able to access instance variables of the with(obj). However 'other' wont work from there?
Nobody said that. All I said was that you needed to be sure that the script is being called from within a with statement(){}. Then, the otherkeyword will be applicable. If you are referring to calling a script, and then that script having the with(){} statement(which then uses other[/], there is no reason that won't work either. Scripts/Functions generally work as if you simply copy/pasted the code in(there is even a pre-compiler flag to force this literal thing to happen when using the YYC).
 

chamaeleon

Member
I'm guessing they have some kind of failsafe in there then. They should really shoot out an error instead. I would count that as wrong/undefined behavior as I mentioned. Neat to know though.
I would have felt more comfortable with it having the value of noone outside with or collisions if not aborting with an error. But I'm not losing sleep over the current behavior.
 

kburkhart84

Firehammer Games
I would have felt more comfortable with it having the value of noone outside with or collisions if not aborting with an error. But I'm not losing sleep over the current behavior.
noone would have been acceptable, I like the actual error better so it's more clear what's going on instead of undefined stuff, but yeah, I never do it so it doesn't really affect me.
 

TheouAegis

Member
Sooo in the script, I know Im able to access instance variables of the with(obj). However 'other' wont work from there?
Given object A and object B, these two sets of code should function identically.

A calls
Code:
script();
Script:
Code:
with B {
    if ++x > room_width {
        x = 0;
        other.y += 16;
    }
}

B calls the script from A
Code:
with B
    script();
Script:
Code:
if ++x > room_width {
    x = 0;
    other.y += 16;
}
 
Top