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

GameMaker instance deactivate object

M

Mikael.K

Guest
Maybe i'm effing stupid or something but i can't get instance_deactivate_object to work.
I have an object called obj_spawn. I want it to spawn an enemy after it's animation has played, then deactivate itself.

This is the only code i have in it.

Animation End:
instance_create_layer(x,y,"characters",obj_enemy)
instance_deactivate_object(self);

It keeps spawning endless enemies. I even tried putting the code in the Create Event. Still won't deactivate.
Tried "instance_deactivate_object(object id)" in the Create Event of another object. Still won't deactivate.

I have used instance_deactivate_object before, never had a problem. What am i doing wrong?
 
S

Sinaz20

Guest
Use id instead of self.

I don't fully understand the intent of self, but I believe it is meant specifically for managing scope when you can't guarantee the rest of the program respects a the uniqueness of a variable name.

Like, I think, the more correct use here, if you were managing scope would be self.id. Because the instance id is what you want here.
 

TheouAegis

Member
Use id instead of self.

I don't fully understand the intent of self, but I believe it is meant specifically for managing scope when you can't guarantee the rest of the program respects a the uniqueness of a variable name.

Like, I think, the more correct use here, if you were managing scope would be self.id. Because the instance id is what you want here.
It's for use with var and globalvar (but not the built-in globals like health and score). Possibly for use with variable_get_name or whatever that function is/was. Consider:

Code:
timer = 8;
globalvar timer;
timer = 8;
timer-=1;
self.timer-=2;
The global variable timer would be 7 and the instance variable timer would be 6.

In terms of var use, self overrides the dominance of local variables.

Code:
var cat; cat = 1;
self.cat = 2;
This legitimately creates two variables with the same name, one local and one instance variable. Without self there would be just the local variable.

Of course if you just didn't use globalvar to begin with or didn't give your variables the same names as instance variables...

Another reason globalvar is bad (for the same user-fault reason), consider the following:

Code:
cat = 1;
var cat; cat = 2;
globalvar cat; cat = 4;
In Studio this probably isn't as big of an issue since you can declare and initialize variables in one statement, but in this code the local variable cat is set to 4, the 8nstance variable (self.cat) is set to 1, and the global variable is set to 0 because it was never actually set properly. This is alleviated with global, or by changing the order (which is not a fix at all), but having to prefix global to a global variable kinda negates the usefulness of globalvar.

Code:
cat = 1;
var cat; cat = 2;
globalvar cat; global.cat = 4;
And there you have it, the only "good" reason for using self: for reinforcing lack of originality in variable names. lol
 
Last edited:
S

Sinaz20

Guest
While we're on the subject, can you explain how self evaluates under the hood? Self seems to just evaluate to -1... how does the language make sense of this behind the scenes?
 
Top