MartinK12
Member
I know how creation/execution order works in GMS, at least I think so
I’m looking for how to create my own solutions to sorting - the problem is with creation code of instances placed in the room and dependent on each other variables - for example obj_tree need to setup in its create event its color bases on obj_wall below him but in room editor first we place obj_tree, then obj_wall so obj_tree variables are setup before obj_wall variables.
I don't have problems with sorting instanced I created through code during gameplay but those that I need to place in room editor - just requires more testing Also I'm looking for the optimal solution - performance and to work with.
SOLUTION 1 - DEFAULT GMS - instance variables are initialized in the order based on instance creation order list
pros: this is easy for very small projects
cons: manual labor moving instances on this list up and down so it's worst solution ever
SOLUTION 2 - STEP EVENT - put variables dependent on other objects variables to step event and place all instances on specific layers so obj_wall instances with their variables are always initialized first (have higher depth layer, ie. 100) and obj_tree instances with their variables after that (have lower depth layer, ie -100).
pros: it's working like a charm
cons: code runs once but if check still take unnecessary processing power every step for possible several hundreds instances, but it’s easiest and best one I got so far.
Example code:
SOLUTION 3 - COLLISION CHECK WORKS - I'm surprise that this solution works but looks like when game starts all instances are already placed in room so we can check all their ids through collisions with each other in create event?! But their variables are not initialized yet! So we ignore their variables and in create event we can check for instances id and we can set our variables based on instance id and object_index.
pros: this code runs only once in create event
cons: uses strings to compare, suitable only for some situations (we must know all variable values that object can have), also I’m surprise that it works, will it always work?
Example code:
What do you think about above solutions, do you see any possible bugs with them, can you explain why solution 3 works, and what other solutions are there for initializing variables in create event that depend on other objects variables?
Thank You
I’m looking for how to create my own solutions to sorting - the problem is with creation code of instances placed in the room and dependent on each other variables - for example obj_tree need to setup in its create event its color bases on obj_wall below him but in room editor first we place obj_tree, then obj_wall so obj_tree variables are setup before obj_wall variables.
I don't have problems with sorting instanced I created through code during gameplay but those that I need to place in room editor - just requires more testing Also I'm looking for the optimal solution - performance and to work with.
SOLUTION 1 - DEFAULT GMS - instance variables are initialized in the order based on instance creation order list
pros: this is easy for very small projects
cons: manual labor moving instances on this list up and down so it's worst solution ever
SOLUTION 2 - STEP EVENT - put variables dependent on other objects variables to step event and place all instances on specific layers so obj_wall instances with their variables are always initialized first (have higher depth layer, ie. 100) and obj_tree instances with their variables after that (have lower depth layer, ie -100).
pros: it's working like a charm
cons: code runs once but if check still take unnecessary processing power every step for possible several hundreds instances, but it’s easiest and best one I got so far.
Example code:
GML:
//code in obj_tree - create:
set_variables = true;
//example code in obj_tree step event:
if (set_variables) {
set_variables = false; //so every other game frame this is false but still takes processing power
wall_below_us = instance_place(x, y+1, obj_wall);
if (wall_below_us) {
my_color = wall_below_us.my_color;
} else {
my_color = c_white; //default color
}
}
pros: this code runs only once in create event
cons: uses strings to compare, suitable only for some situations (we must know all variable values that object can have), also I’m surprise that it works, will it always work?
Example code:
GML:
//example code in obj_tree - create:
wall_below_us = instance_place(x, y + 1, obj_wall);
if (wall_bellow_us) {
obj_name = object_get_name(wall_below_us.object_index);
if (obj_name = “obj_grass”) {
my_color = c_green; //just switch for all possible object names
} else {
my_color = c_white; //default color
}
}
Thank You
Last edited: