GMS 2.3+ layer_create is called but layers don't exist

Mooner01

Member
Hello all

I am running GML in GMS2 with runtime version 2.3.0.401 and IDE version 2.3.0.529

I am trying to generate a room on startup that is filled with obj_grass objects. I know, I should use tiles, but that is a problem for later. The code creates the appropriate layers and then tries to place the grass in those layers, but when I call instance_create_layer, it errors out because the layer doesn't exist. Code is below. In the file structure it is contained in a script called 'startup.gml'.

GML:
outside_width_px = 1280
outside_height_px = 960
room_set_height(room, outside_height_px)
room_set_width(room, outside_width_px)
terrain = layer_create(100, "terrain")
layer_create(0, "vegetation")
layer_create(-100, "entities")
layer_create(-150, "player")
layers = layer_get_all() // for debug
current = room_get_name(room) // debug

hor_tiles = outside_width_px / tile_size // tile_size = 32, stored in a macro elsewhere
ver_tiles = outside_height_px / tile_size
for(i = 0; i < hor_tiles; i++) {
    for(j = 0; j < ver_tiles; j++) {
        instance_create_layer(i * tile_size, j * tile_size, "terrain", obj_grass)
    }
}
I am getting this error:
Code:
___________________________________________
############################################################################################
ERROR in
action number 1
of Create Event
for object <undefined>:

instance_create_layer :: specified layer "terrain" does not exist
at gml_GlobalScript_Startup (line 16) -               instance_create_layer(i * tile_size, j * tile_size, "terrain", obj_grass)
############################################################################################
gml_GlobalScript_Startup (line 16)
I've tried creating a new room, but that hasn't worked. When I run in debug mode, the layers variable evaluates to -1, which makes me think that the layers aren't being made. Any ideas?
 

Roldy

Member
What object instance is calling this code and when is it being called? The error states it is for the Create Event of an Unknown Object.

If this script is not wrapped in a function definition then it is being called globally (gml_GlobalScript_Startup). And you are going to have limited, or no, control over the state of the game when this is called.

Try calling it as a function from a specific object instance and event.

Review the section on functions :https://www.yoyogames.com/blog/549/gamemaker-studio-2-3-new-gml-features
 
I've tried creating a new room, but that hasn't worked. When I run in debug mode, the layers variable evaluates to -1, which makes me think that the layers aren't being made. Any ideas?
You can only use the name of the layer if the layer is created in the Room Editor.
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 what you want to be using is the layer id that is returned when you used layer_create() already in your event:
Code:
terrain = layer_create(100, "terrain")
So just change your instance_create_layer to use the variable terrain that you stored the layer id in:
Code:
instance_create_layer(i * tile_size, j * tile_size, terrain, obj_grass)
 

Mooner01

Member
It was running in the body of a script, without a function or an object call. I changed the script to:
GML:
function setup_outside() {
    outside_width_px = 1280
    outside_height_px = 960
    room_set_height(room, outside_height_px)
    room_set_width(room, outside_width_px)
    terrain = layer_create(100, "terrain")
    layer_create(0, "vegetation")
    layer_create(-100, "entities")
    layer_create(-150, "player")
    layers = layer_get_all()
    for(i = 0; i < array_length(layers); i++) {
        show_debug_message(layer_get_name(layers[i]))
    }
    current = room_get_name(room)
    
    hor_tiles = outside_width_px / tile_size
    ver_tiles = outside_height_px / tile_size
    for(i = 0; i < hor_tiles; i++) {
        for(j = 0; j < ver_tiles; j++) {
            instance_create_layer(i * tile_size, j * tile_size, "terrain", obj_grass)
        }
    }
}
and added an init object that calls the function on startup. It works now. Thanks for the help!
 

Biscotto

Member
The code seems to be correct, so the problem lies elsewhere like Roldy already told you.
I wanted to take this opportunity to make two small points:
- If you enter the layer name in instance_create_layer as a string, you don't need to assign the layer you create to a variable.

- For debugging, also remember to check which is the current target of the layer functions, and be sure that the target is the current room.
GML:
if (layer_get_target_room() != room) {
   layer_reset_target_room();
}
 

Mooner01

Member
You can only use the name of the layer if the layer is created in the Room Editor.
From the manual for instance_create_layer:

So what you want to be using is the layer id that is returned when you used layer_create() already in your event:
Code:
terrain = layer_create(100, "terrain")
So just change your instance_create_layer to use the variable terrain that you stored the layer id in:
Code:
instance_create_layer(i * tile_size, j * tile_size, terrain, obj_grass)
I tried that as well and it didn't explicitly error out, but the error was printed to the console and the objects weren't created. See the solution above.
 
Top