• 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 (Solved) Can't get layer background ID

Hello. For some reason, I can't access the id of a background layer that I create. Three simple lines of code and I'm getting a message saying "-1" instead of the layer background id. I must be missing something obvious but I can't figure out what it is.

id1 = layer_create(500500)
layer_background_create(id1,back_hills)
show_message(string(layer_background_get_id(id1)))

I know I can store the layer background id into a global variable when I create it, but I'd rather not do that and I want to understand why this code is returning "-1" instead of the layer background id.
 

sp202

Member
This might have something to do with it, from the manual:
Code:
Note that this function is specifically designed for use with backgrounds that have been added in the IDE, as if you add a background to a layer through code using the function layer_background_create, then it will return the unique ID for the background element added.
 
J

J_Dev

Guest
Hello. For some reason, I can't access the id of a background layer that I create. Three simple lines of code and I'm getting a message saying "-1" instead of the layer background id. I must be missing something obvious but I can't figure out what it is.

id1 = layer_create(500500)
layer_background_create(id1,back_hills)
show_message(string(layer_background_get_id(id1)))

I know I can store the layer background id into a global variable when I create it, but I'd rather not do that and I want to understand why this code is returning "-1" instead of the layer background id.
First of all, id1 already contains the layer ID.

Second, the layer depth should be between -16000 and 16000, not 500500.

Third, the correct function to get (non background) layers is layer_get_id(). But you would need to supply a string (the layer name). Alternativly, if you know the depth, you can use layer_get_id_at_depth();
 
First of all, id1 already contains the layer ID.

Second, the layer depth should be between -16000 and 16000, not 500500.

Third, the correct function to get (non background) layers is layer_get_id(). But you would need to supply a string (the layer name). Alternativly, if you know the depth, you can use layer_get_id_at_depth();
Thank you for the input. You seem to think that I am trying to access the layer ID. I'm not. I'm trying to access the background element id of the layer so that I can use functions such as layer_background_visible. Perhaps sp202 is correct that this function only works for pre-built layers. But if that is the case, how do I find the background element ID of background layers that I created? Do I have to store them in variables after they are made, or is there another way?
 
J

J_Dev

Guest
Thank you for the input. You seem to think that I am trying to access the layer ID. I'm not. I'm trying to access the background element id of the layer so that I can use functions such as layer_background_visible. Perhaps sp202 is correct that this function only works for pre-built layers. But if that is the case, how do I find the background element ID of background layers that I created? Do I have to store them in variables after they are made, or is there another way?
Oh my bad. In that case why not just do this:

Code:
bg_element_id = layer_background_create(id1,back_hills)
Because he is right. The other function is only used for Backgrounds create dusing the IDE.

The reason it is returning -1 is because you are referencing the layer id not the background element with your debug message.
 
Top