game-makergmlgame-maker-studio-1.4

When colliding with another object, the variable referenced is undefined, even though it is defined? (GMS1.4)


I am using GameMaker Studio 1.4. I have a fireball object, and when it collides with the enemy object, it is supposed to remove 1 (one) from the enemy's life variable.

Here is the code:

Fireball Code

Step Event

if (place_meeting(x,y,obj_enemy)) { // if collision with enemy
    with (other) {
        other.life-=1; // remove 1 from life
        self.start_decay=true; // remove fireball
    }
}

Enemy Code

Create

life=1;
isDie=false;

Step Event

if (life<=0) {
    isDie=true; // I use a variable because there are other conditions that can also satisfy this
}


[...] // other unnecessary code


if (isDie) {
     instance_destroy(); // Destroy self
}

Error Log(s)

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of  Step Event0
for object obj_fireball:

Variable <unknown_object>.<unknown variable>(100017, -2147483648) not set before reading it.
 at gml_Object_obj_fireball_StepNormalEvent_1 (line 3) -         other.life-=1;
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_fireball_StepNormalEvent_1 (line 3) ('other.life-=1;')


Solution

  • One thing I notice is that you're using an other inside a with(other), which sounds a bit unneccesary.

    Assuming that the other.life -= 1 is meant for the enemy, and the self.start_decay=true is for the fireball, then you can remove the with(other) line (and brackets), keeping the code like this:

    if (place_meeting(x,y,obj_enemy)) { // if collision with enemy
        other.life-=1; // remove 1 from life
        self.start_decay=true; // remove fireball
    }
    

    If you use with(other), then everything inside that with(other) will be targetted towards the 'other' object it's colliding with, in this case, your obj_enemy.
    Calling an other inside a with(other) will possibly be targetted back towards the fireball, which didn't defined the life variable. And that's where your error came from.