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

Run code in newly created room

Smig

Member
I'm getting the error:
Code:
specified layer "LayerStars" does not exist
On the following code:
Code:
var star = instance_create_layer(starX, starY, "LayerStars", oStar);
Which is part of a script (setupStarMap) that is being called from another room, like this:
Code:
room_goto(rStarMap);
setupStarMap();
If I run this code on the create event of an object that I drag and drop on the room, it works, so the layer name is correct.

I assume this is caused by that code still running on the previous room, despite it being after the room_goto instruction. Is that correct? Shouldn't it change rooms before the next line? If that's the case, is there some way to specify running it on that room? Otherwise I need a new object just to run that code.
 
D

Diamond Hunter Zero

Guest
I'm getting the error:
Code:
specified layer "LayerStars" does not exist
On the following code:
Code:
var star = instance_create_layer(starX, starY, "LayerStars", oStar);
Which is part of a script (setupStarMap) that is being called from another room, like this:
Code:
room_goto(rStarMap);
setupStarMap();
If I run this code on the create event of an object that I drag and drop on the room, it works, so the layer name is correct.

I assume this is caused by that code still running on the previous room, despite it being after the room_goto instruction. Is that correct? Shouldn't it change rooms before the next line? If that's the case, is there some way to specify running it on that room? Otherwise I need a new object just to run that code.
(I assume this code is being run in some kind of object and not a room creation event?)
When the room_goto() function is executed, Gamemaker will not move to the new room until every line of code still remaining in the event that room_goto() is inside of, has been run. This means that instance_create_layer(starX, starY, "LayerStars", oStar); will be run in the original room, where the layer doesn't exist, because setupStarMap(); is being run in the original room.

In order to run code in a newly moved-to room, you either need to write it in the new room's Creation Event or in a persistent object from the previous room. For your case, I would recommend temporarily making your calling object
persistent and have it run an alarm like this:

Inside the Original Room & Object
Code:
room_goto(rStarMap);
persistent = true;
alarm[0] = 1;
Inside the Alarm 0 Event of the calling object
Code:
var star = instance_create(starX, starY, "LayerStars", oStar);
instance_destroy();
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Top