• 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 Trouble implementing "with"

knightshaft

Member
I'm getting unknown instance and unknown variable for "other" and "t". If I understand correctly (and this might be my issue) "other" is the object that this is written in so how is it unknown. And "t" is used in "SBcreator[t] = 1" just above it. Even if I change "other" to the actual name of this object, "t" is still unknown. What am I doing wrong?


GML:
if (global.step_counter > 1) {

inst = instance_create_layer(800,VertAlign,"FrontRoom",Button_obj);
SBcreator[t] = 1;

     with (inst)
    {
    location = other.t;
    }

}
 

TsukaYuriko

☄️
Forum Staff
Moderator
Where are you declaring t? You didn't show this part, so I can't be sure what scope it was declared under.

If it was declared under instance scope, it should be referenced by other.t.
If it was declared under local scope (var), it should be referenced by t - both inside and outside of a with statement - as it is local to the event or function it was declared in, not the instance.
 

knightshaft

Member
Where are you declaring t?
I would have bet my mortgage on it being instance scope as it has been in my game since before I had any concept of var!
So I've just lost my house! It was var, I have changed it to instance and the original coding (above) now works fine.

Thank you for making me check.
 

kburkhart84

Firehammer Games
That's fine, but if it was working fine before as a local var, you are better off getting in the habit of keeping it local instead of as an instance variable. Then, in the with() statement, just remove the other part and access it directly.
 

knightshaft

Member
Then, in the with() statement, just remove the other part and access it directly.
I'm not following. If you remove other it would be: location = t;
I can't see that working because location is in the new instance and t is in the object that called it
I assume I'm missing something.
 

TsukaYuriko

☄️
Forum Staff
Moderator
t is not a part of the calling instance if you declare it as a local variable via var. Then it would be a part of the function or event it was declared in.

As you know, as long as you're within the scope of something, you can refer to it by its name. So, since the scope is the current event, and the with statement is inside of that event, you refer to it by its name. ;)
 

kburkhart84

Firehammer Games
't' would be a variable that you declared on the original object using the var keyword. This is a local variable, which does not belong to the object, rather the code itself. So when you go into the inside of a with() statement, you are now in the scope of the other object..but you are still in the same code block, so the variable is accessible the same way. Instance variables stay with an instance, and depend on scope, but local variables don't have instance scope, rather code block scope.

If you are not using the t variable anywhere else except for in that code block(as in no other events), add the var to it, and get rid of the other. part and keep it as location = t and I bet it will work.[/icode]
 
Top