• 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!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Windows Problem with instance_create_depth and instance_create_layer

M

MadTinkerer

Guest
In my game I have a bunch of instances instantiating instances. With the GMS1, things were pretty straightforward because there was just one instance_create() function. In the new GMS2, we have instance_create_layer() which is even more straightforward because you have the layer built-in variable. But...

So we start with the obj_Interface object, a "God object" that essentially tells everything else what to do. One of the things it does is call the CreateMainMenu() script. The CreateMainMenu script instantiates the Main Menu Buttons including the obj_OpenGame button. When clicked, an instance of obj_OpenGame instantiates the obj_Game object, which is the object that keeps track of everything that needs to be saved when the game is saved.

So, obj_Interface starts in the main room on the default Instances layer, then it calls CreateMainMenu which instantiates the obj_OpenGame button. Now, I meant to use instance_create_layer in CreateMainMenu, but I accidentally used instance_create_depth instead. So as far as I can tell, obj_OpenGame was instantiated with a depth equal to the layer of obj_Interface. This became a problem when I tried to instantiate obj_Game using the layer variable of obj_OpenGame. I'm still not completely sure what GMS2 tried to do, but obj_Game was not instantiating, and it drove me crazy for three days before I realized the problem wasn't even with obj_OpenGame itself, but CreateMainMenu() using the wrong instance_create function.

So I changed the erroneous instance_create_depth()s to instance_create_layer()s and everything now works as intended. I think there needs to be a warning if instance_create_layer is being called from an object that was instantiated with instance_create_depth, because, as far as I can tell, it will just not work, and GMS2 won't give an error message.
 

gnysek

Member
The safest option is to create custom script for creating on layer, which checks for layer to exists and if not it creates instance at some depth (like -1000), cause you will need it all the time.
 
Top