Search code examples
gmlgame-maker

Access object members from Instance ID


I'm getting the Instance ID of an object from collision_line()

Now that I have this instance, I want to get it's image_angle, but I get an 'unknown variable' message when I try that.

What should I do?


Solution

  • what is the value of this collision_line()? The collision_line() function returns an instance id - however when nothing is found it returns noone (-4).. So you'll have to test for that first:

    var inst, imgangle;
    inst = collision_line(...);
    if (inst != noone) {
        imgangle = inst.image_angle;
        //etc etc
    }
    

    or alternativelly (more cleanly in GM), we can "abuse" the with statement. With executes all following code from the perspective of the given instance id (or for all instances of a certain object when given the object id).
    However the value noone will automatically prevent any execution.

    var inst, imgangle;
    inst = collision_line(...);
    with (inst) {
        imgangle = image_angle; 
        //note that we do no longer have to put "inst." before getting a variable
        //etc etc
    }