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

GML [solved] Specified layer does not exist

D

Deleted member 45063

Guest
Hello GMC.

I just created a new project in which I use different control objects, each one responsible for managing a different module of the game. Since instances are organized in layers I wanted all of my control objects to be in a single "control" layer. In order to do this in the first room I execute a piece of code that iterates through all rooms and adds the layer if it isn't present there already. However when I do this the creation of the first control object in that same room fails with the following error message:

Code:
___________________________________________
############################################################################################
FATAL FATAL ERROR in Room Creation Code for room rm_init

instance_create_layer :: specified layer "control" does not exist
 at gml_Room_rm_init_Create (line 15) - instance_create_layer(0, 0, LAYER_NAME, ctrl_dummy);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Room_rm_init_Create (line 15)
In order to reproduce this just create a new GML project with a blank object called ctrl_dummy and a room callled rm_init with the following room creation code:

Code:
#macro LAYER_NAME "control"
#macro LAYER_DEPTH -10000

var current_room = 0;
while (room_exists(current_room)) {
   layer_set_target_room(current_room);
   if (!layer_exists(LAYER_NAME)) {
       show_debug_message("Create layer for " + room_get_name(current_room));
       layer_create(LAYER_DEPTH, LAYER_NAME);
   }
   current_room++;
}
layer_reset_target_room();

instance_create_layer(0, 0, LAYER_NAME, ctrl_dummy);
I haven't used GameMaker in some years so is there something that I am just missing entirely or should the layer be recognized?

Thanks in advance for your support!

IDE Version 2.2.4.474, Runtime 2.2.4.374, Windows 10
 
If you're going to use the name of the layer, then that layer must exist in the room editor already. If you are creating the layer on room creation, then you need to use the id that is returned by the layer_create function (I don't think you can use the name).
From the manual for instance_create_layer:
The layer can be identified using the layer ID value (as returned by the function layer_create()) or by the name of the layer (as a string, for example "instance_layer") as defined in the room editor.
So I think you are going to have to store the id from the layer_create function and then use that instead of the name of the layer.
 

rIKmAN

Member
Using the name of the layer shouldn't be the problem - you can test this by reducing your code to the following which works fine:
Code:
#macro LAYER_NAME "control"
#macro LAYER_DEPTH -10000

layer_create(LAYER_DEPTH, LAYER_NAME);
instance_create_layer(100, 100, LAYER_NAME, ctrl_dummy);
From a quick play with the code you posted it seems to be an issue with using layer_set_target_room() to set the room you are already in as the target.

If you move the code to create the layer and the instance of ctrl_dummy above the code for doing your loop through the rest of the rooms and start the loop at 1 then it works fine and the console reports that the layers were created all the other rooms.

Code:
#macro LAYER_NAME "control"
#macro LAYER_DEPTH -10000

layer_create(LAYER_DEPTH, LAYER_NAME);
instance_create_layer(100, 100, LAYER_NAME, ctrl_dummy);

var current_room = 1;
while (room_exists(current_room))
{
   layer_set_target_room(current_room);
        if (!layer_exists(LAYER_NAME))
        {
            show_debug_message("Create layer for " + room_get_name(current_room));
            layer_create(LAYER_DEPTH, LAYER_NAME);
        }
    layer_reset_target_room();
 
    current_room++;
}
Gives me the output:
Code:
Create layer for room1
Create layer for room2
Create layer for room3
...where I added 3 extra rooms for a total of 4 (room0, room1, room2, room3)

You can't create instances in rooms set as targets (see the manual page for layer_set_target_room) and I don't have time to test if the layers have been created in them but that's the next thing I'd check.
 
Last edited:
D

Deleted member 45063

Guest
If you're going to use the name of the layer, then that layer must exist in the room editor already. If you are creating the layer on room creation, then you need to use the id that is returned by the layer_create function (I don't think you can use the name).
From the manual for instance_create_layer:

So I think you are going to have to store the id from the layer_create function and then use that instead of the name of the layer.
Thanks for your reply @BaBiA Game Studio but from the documentation of layer_create using the name should work as usual:
You can also supply an optional "name" argument, which will enable you to give the layer a specific name (as a string), and instead of using the layer ID to access the layer, you can use this name string instead, although it should be noted that using name strings will have a greater impact on performance than using just the ID value.
Using the name of the layer shouldn't be the problem - you can test this by reducing your code to the following which works fine:
Code:
#macro LAYER_NAME "control"
#macro LAYER_DEPTH -10000

layer_create(LAYER_DEPTH, LAYER_NAME);
instance_create_layer(100, 100, LAYER_NAME, ctrl_dummy);
From a quick play with the code you posted it seems to be an issue with using layer_set_target_room() to set the room you are already in as the target.

If you move the code to create the layer and the instance of ctrl_dummy above the code for doing your loop through the rest of the rooms and start the loop at 1 then it works fine and the console reports that the layers were created all the other rooms.

Code:
#macro LAYER_NAME "control"
#macro LAYER_DEPTH -10000

layer_create(LAYER_DEPTH, LAYER_NAME);
instance_create_layer(100, 100, LAYER_NAME, ctrl_dummy);

var current_room = 1;
while (room_exists(current_room))
{
   layer_set_target_room(current_room);
        if (!layer_exists(LAYER_NAME))
        {
            show_debug_message("Create layer for " + room_get_name(current_room));
            layer_create(LAYER_DEPTH, LAYER_NAME);
        }
    layer_reset_target_room();
 
    current_room++;
}
Gives me the output:
Code:
Create layer for room1
Create layer for room2
Create layer for room3
...where I added 3 extra rooms for a total of 4 (room0, room1, room2, room3)

You can't create instances in rooms set as targets (see the manual page for layer_set_target_room) and I don't have time to test if the layers have been created in them but that's the next thing I'd check.
Thanks @rIKmAN indeed the layer_set_target_room was the culprit, changing the code to the following works as expected!
Code:
#macro LAYER_NAME "control"
#macro LAYER_DEPTH -10000

var current_room = 0;
while (room_exists(current_room)) {
    if (current_room != room) { // just added this safeguard
        layer_set_target_room(current_room);
    }
    if (!layer_exists(LAYER_NAME)) {
        show_debug_message("Create layer for " + room_get_name(current_room));
        layer_create(LAYER_DEPTH, LAYER_NAME);
    }
    current_room++;
}
layer_reset_target_room();

instance_create_layer(0, 0, LAYER_NAME, ctrl_dummy);
 
Top