GML [SOLVED] General Programming Question: Does the order matter if it all happens in one step?

Dr_Nomz

Member
Let's say I have an object that runs a series of things all at once, one of them being to destroy itself, like so:
GML:
  with(collide_close){
    scr_Enemy_Damage(argument0);
  }
  instance_create(collide_close.x,collide_close.y,obj_Hit_Detector_2);
  instance_destroy(self);
If I were to run the destroy code up top instead of last, will that destroy the instance BEFORE all the other code is ran after it, or will it be able to run all at once with no issues?
 

Mr Magnus

Viking King
From the manual

It is worth noting that when you destroy an instance, its destroy event is called immediately after the code or action that calls the destroy function. Also, although the destroy event is performed, the instance is not immediately removed from the game, and it will continue to perform the code contained in the current event. Only when the current event is over will it be removed from the game.
The instance_destroy() isn't applied until the end of the step event, so it doesn't matter where the line appears, anything else executing alongside it will finish before the object vanishes.
 

Nidoking

Member
I wouldn't recommend calling instance_destroy and then trying to use any of the information in the instance that was destroyed, though. In this example, it doesn't look like any of the other lines refer to variables of that instance, so you're probably fine. Still, unless there's a particular need to destroy the instance earlier, I'd just leave it until as late as possible. Why take the risk just because it technically shouldn't break things?
 

Dr_Nomz

Member
Ohhh okay, that makes sense. So it applies to any code as well, it doesn't matter the order too much?
 
Order matters. A lot.

If you do this:

Create Event:
Code:
a = b;
b = 5;
The game will crash, as b doesn't have a value when you try to assign a to it. The computer reads line by line (at least, in this case, there are some cases such as macros where this isn't true, but for the majority of programming it is true). So you should write each line in a way that the computer has access to what it needs before it needs it.

It's like reading a novel. If the author placed the lines randomly, the novel would make no sense (making no sense to a computer means a crash). But because the author ordered the lines in a specific manner, the content of the book becomes understandable.
 

matharoo

manualman
GameMaker Dev.
Ohhh okay, that makes sense. So it applies to any code as well, it doesn't matter the order too much?
In your particular case it can matter, if you're using dynamic resources. So if you have a DS list that you create in the Create event, and destroy in Clean Up, calling instance_destroy() will run Clean Up and destroy that list -- so any code after instance_destroy() will not be able to access that list.
 

NightFrost

Member
Because instance destruct is deferred until end of current event, it is also worth noting you may need to make some of your code not run. To conjure a simplistic example:
GML:
if(is_dead){
    instance_destroy();
}
kill_player();
In the above code, even if is_dead is true and the instance gets destroyed, it still calls kill_player function because all of the event code will run first. To make it run in proper manner you'd have to extend the conditional blocks:
GML:
if(is_dead){
    instance_destroy();
} else {
    kill_player();
}
 
Top