• 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!

GML with() doesn't recognise the other object that executes it?!

X

XirmiX

Guest
The "with" statement is supposed to take an instance/object and make changes to that instance/object as if it's written within that instance/object, but the object that executes the statement can be called within the statement through "other", right? Well, I have had this work well until now, where the object that executes the with statement is a networking one without any textures or anything and from within networking event. And I know that the issue is with the "other." parts and it getting an "unknown object" because if I change this code in the networking event:

Code:
var turret_type = buffer_read(buffer, buffer_u8); //turret type
var hull_type = buffer_read(buffer, buffer_u8); //hull type

tank_instance = instance_create(instance_spawn_current.x, instance_spawn_current.y, obj_hull);

with(tank_instance){
        buffer_hull_receive = other.hull_type;
        buffer_turret_receive = other.turret_type;
}
To this...

Code:
var turret_type = buffer_read(buffer, buffer_u8); //turret type
var hull_type = buffer_read(buffer, buffer_u8); //hull type

tank_instance = instance_create(instance_spawn_current.x, instance_spawn_current.y, obj_hull);

tank_instance.buffer_hull_receive = hull_type;

with(tank_instance){
        buffer_turret_receive = other.turret_type;
}
...the error for that line of code goes away and moves onto creating an error for the next line that is still in the with statement, which is the same type of error. The two types of code would do the exact same thing, but one gives an error, the other doesn't. Any ideas as to why this might be? Can with statements with "other." not be done from within networking events? And to confirm yes, the variables from the networking event of the connection object ARE set to something, I even have set the values to display in the console log. And yes, the variables have already been set for the object instance that is called within the "with" statement and yes, an instance is being selected here.

This is the error that occurs for when both lines are in the with() statement btw:
Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Async Event: Networking
for object obj_connection:

Variable <unknown_object>.<unknown variable>(100014, -2147483648) not set before reading it.
 at gml_Object_obj_connection_NetworkingEvent_1 (line 109) -                     buffer_hull_receive = other.hull_type;
############################################################################################
Here's the error that occurs after one of the lines is taken away from the with statement and set straight in the networking event of the collision object:
Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Async Event: Networking
for object obj_connection:

Variable <unknown_object>.<unknown variable>(100102, -2147483648) not set before reading it.
 at gml_Object_obj_connection_NetworkingEvent_1 (line 112) -                     buffer_turret_receive = other.turret_type;
############################################################################################
 

FrostyCat

Redemption Seeker
Rules on how to reference a variable from within a with block:
  • Local variable (i.e. declared with var): variable
  • Instance variable belonging to the calling instance: other.variable
  • Instance variable belonging to the called instance: variable
  • Standard global variable: global.variable
  • Deprecated globalvar-declared global variable (strongly discouraged): variable
  • Instance variable belonging to an unrelated subject (e.g. a controller) or a fixated subject (i.e. instance ID stored in a local variable): subject.variable
You declared your variable with var. That's a local variable, not an instance variable. Keep your paws off other.
Code:
var turret_type = buffer_read(buffer, buffer_u8); //turret type
var hull_type = buffer_read(buffer, buffer_u8); //hull type

tank_instance = instance_create(instance_spawn_current.x, instance_spawn_current.y, obj_hull);

with (tank_instance) {
       buffer_hull_receive = hull_type;
       buffer_turret_receive = turret_type;
}
 

CMAllen

Member
Yup. They're not instance variables, so other other identifier has no variable to which they refer. Local variables are in scope for every bit of code in the event that executes it. And if you wanted turret_type and hull_type to be instance variables, remove the var preface and keep the other.[variable] set up you're using now. Otherwise, those variables will go out of scope and cease to exist once that script or event finishes executing.

Note: if this is a script being run by an object, and that object does not have instance variables by those names already declared, it might throw an error trying to assign values to instance variables that don't exist.
 
Top