GMS 2 ¿Creation Code from Room? Can you give me one example please?

GGJuanPabon

Member
Each room has something called, Creation Code. What is it specifically used for? What is your function?

The manual says:
Creation Code
If you click on this button you will open a code editor. This editor allows you to input functions and code that will be run at the start of the room, after the create event of all instances but before their room start event.

I would like if it is possible, and if you have time, could you give me some examples of how I could use it.
 

Nidoking

Member
I use it to set the music for each room. But really, it's like everything else in Game Maker - it's not a matter of figuring out how to use it, it's a matter of having a use for it. If the room is the right place for the code that does that thing, then this is where you put it.
 

Slyddar

Member
could you give me some examples of how I could use it.
I've used it to set the players starting position for each room, but I have a parent room to all my levels so the parent is the only one that holds the information, and I use a switch(room) statement to allocate the correct coordinates. This is all set in a script that runs, so no need to hunt for the creation code button to edit it.
It's especially useful for me as my players room_start code positions the player in their start location for each room via global x and y positions, but those variables need to be set for the newly entered room before the player runs their room start code. By placing it in the creation code of the parent room, it is run before the object room_start code, so I can be sure those variables are set correctly when needed.

Also to set a variable called global.room_type, which I set to "menu" or "game", so I can then show gui elements if it's a game room, or do other things if a menu room.

Like it was mentioned above, it's usefulness comes down to what you are trying to do in your game.
 
Last edited:

TailBit

Member
I have a variable that holds the info for the crop for all the fields ..

When I enter the room then I have the creation code fetch the correct field grid and put it into a variable so I can more easily access it:
GML:
global.field = global.all_fields[0];
This is set to noone again in the room end event:
GML:
global.field = noone;
And in the room start event I recreate all the crop on the field if the variable isn't noone:
GML:
if(global.field!=noone){
    for(var i=0;i<ds_grid_width(global.field);i++)
    for(var j=0;j<ds_grid_heigth(global.field);j++){
        if(global.field[# i,j] != noone){
            instance_create_layer(i*16,j*16,"field",global.field[# i,j])
        }
    }
}
ofc .. just 2-3 rooms are fields, so it isn't something that is triggered that often, still, in some cases then the room creation code does come in handy.
 
Last edited:

Neptune

Member
It's purely for using the room editor.
Imagine you have an object with 6 different states/forms it could assume. You could drag an instance into the room, and say state = flower; or state = tree; in the creation code. The same kinds of things you might do when creating an instance with code.

It is handy for this kind of thing...
Kind of a lame example, but that's the gist of it!

To be blunt, all of the above uses are not necessary uses of creation code. I think it's mostly for making small tweaks at the instance level. Anything else regarding the room or global settings could be better handled in a persistent control object somewhere.
 
Last edited:

Yal

🐧 *penguin noises*
GMC Elder
Creation code is basically a script that's run when the room starts, and you can also give instances creation code. At this point it's mostly a remnant from the days before you could set variables per-instance from the GUI, but it's useful if you want to do small changes on a per-room / per-instance basis without having to make a custom script for every case (e.g. play the level's background music, give an NPC unique lines, etc).
 
Honestly, I think it's good that GM gives you access to it (because there's always some random situation wherein some random person can find a need to use such a thing) but in regards to actual use, unless you have a very specific need for that kind of functionality, just ignore the hell out of it. Always remember that everything you need to do can be done in the Step Event and Draw Event with enough planning. Everything else is just flavour (and maybe -faster- depending on the event and the thing that is being done). The events are there to let you do specific things. It's like asking "What would I use the 'Key Pressed W' Event for?" It's for when you want something to happen when the key "W" is pressed. If you don't need that, don't use it. It's not required, it's just a possibility.
 

GGJuanPabon

Member
Thank you all for your experiences, and thanks to GM for answering. It is good to feel that you know something technically, in addition to people's experience.

Honestly, I think it's good that GM gives you access to it (because there's always some random situation wherein some random person can find a need to use such a thing) but in regards to actual use, unless you have a very specific need for that kind of functionality, just ignore the hell out of it.
It`s true.
 
Top