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

Declaring variables within WITH(OBJ)

oldnoob

Member
Hello there,

I think this is how it works, but I would very much appreciate confirmation if possible.

If I have several instances of an object, say a ball, on screen and I address them all by...

with(obj_Ball){

}

If I declare any variable within those curly brackets, do they become property of the ball instances? i.e.

with(obj_Ball){
BallColour=choose(1,2,3);
}

So to target or retrieve those variables I'd have to do something like...

balloon=InstanceID.BallColour
 

Amon

Member
Wouldn't it be better to just have BallColour in your obj_Ball object and any instance created will have BallColour which you could manipulate within a with statement.
 

TsukaYuriko

☄️
Forum Staff
Moderator
with switches the scope.

Without any modifiers (var / global.), variable declarations will be instance-scoped.


Combine these two bits of information and you'll see that, yes, declaring a variable under instance scope while the scope is that of an instance of obj_Ball will indeed declare a variable under the scope of that instance. :)


I'd like to add that you should probably not be letting instances declare variables for instances of other objects, though. Assigning values is fine, but declarations are best off in the Create event of whatever owns the variable, for the simple reason that you'll be sure that it WILL be declared under all circumstances and can see what will be declared at a glance by looking at the Create event.
 
Top