Instance creation code priority

Y

Yobokkie

Guest
Greetings to all, I'm messing around with game maker and learning the ropes. But I've hit a bit of a snag that google seems unable to help me with.
Basically I have a controller object that runs various things in a room and I'm trying to get it to play the background music based on a variable set in the instance creation code. So in my creation code for the controller object I have: global.bgmusic = bgm_1;

And then on the create event of the object I have a script that says audio_play_sound(global.bgmusic, 1, true);
But the game keeps giving an error to say the variable doesn't exists. I'm sure it's something noob I'm doing wrong, but I can't find any answers and from what I can see the instance creation code should run before the create event, so why is it giving the error to say that the variable doesn't exist?

If this isn't the right way to go about playing music for different rooms, perhaps someone could point me in the right direction? I'd prefer to have attached to an object for the sake of persistence, and not restarting every time the room is changed.

Thanks for your help, I've learnt a lot just by lurking here and reading other people responses.
 

CloseRange

Member
You had the right idea however the truth is the creation event runes BEFORE the creation code (if youre talking about the creation code that you set in room editor?)

anyway what i would do is go to create event and make a variable called:
Code:
init = false;
then in the step event do:
Code:
if !init {
      audio_play_sound(global.bgmusic, 1, true);
     init = true;
}
that will make it so it wont try to check for the variable before its created.


PS the reasion gm does the event before the editor creation code is so that you can, for example, make an object and in the create event say "type = 0" then in the room editor make like 1 or 2 objects and int he creation code say "type = 1"

This makes it so the editor creation code can overwrite any variables inside of the creation event

Hope this helped! if not dont be afraid to tell me and i'll do my best to figure out another solution!:D
-CloseRange
 

Tornado

Member
You must assure that your controller object is the first object created in the room if other object(s) will be using the variables set in the controller. If you are adding your object manually in the room editor then you have to make sure that create event of contoller object runs before create event of other objects, otherwise you get that error because then the global variable really doesn't exist yet.
In the room editor you can show and change the order if the instances which are put into room via room editor.
You will see there a list of objects. Just move your controller to be the first object in the list (i think there are up and down arrows there for changing the position in the list)
 
Y

Yobokkie

Guest
Thanks very much for the responses. I tried to find the order of priority as I thought this might be the case, but there was no where that implicitly stated if the create event came before the creation code of the instance. I will use your suggestion CloseRange and implement it so the audio is set to play in a step event rather, with a little flag check to make sure it's not starting the audio up every time.
Thanks again!
 

GMWolf

aka fel666
Another approach is to, rather than have some code for each room, use a global ds_map or array to map each room to a sound.
 

Tornado

Member
What I don't understand is why you always make difference between create code and create event?
For me this is the same. When create event of the object is fired, then that object's create code is executed.
Therefore I don't really understand your posting and what the problem really is.

EDIT: Maybe with "create code" you mean "room create code" ?
 
Last edited:

Tornado

Member
I do the same "lurking", reading many post in order to learn things and how they are solved which I will need later for sure :)
 
I tried to find the order of priority as I thought this might be the case, but there was no where that implicitly stated if the create event came before the creation code of the instance.
The "Instance Creation Code" (that you set in the room editor) run's after the instance's "Create Event", so that you can alter the starting variables for specific instances in a room if you wish.

Just for future information, from the GameMaker documentation on Events:

Event Order

It should also be noted that the exact order that the events are going to occur in each step cannot be clearly stated, simply because it depends on the internal workings of GameMaker: Studio, which is subject to change as the software develops. However there are certain events that always run in the same order. The first set of events that will always happen the same way are those that occur when a room is first entered and the order of events for that is:


It is also worth noting that you can also set the order in which specific instances are created within the room editor itself. See the section on Instance Order in the Rooms - The Settings Tab for further details.

Other than those specific events, the only known order that will always occur in the same way no matter what belongs to the three step events and the two draw events. These will always remain consistent, so if you have code that relies on specific timing during each step of your game, you should use:


  • Begin Step Event
  • Step Event - The step event is executed just before instances are put in their new positions.
  • End Step Event
All of the sub events for drawing are also always dealt with in the same order as follows (except for the Resize event, which is triggered differently):


  • Pre Draw Event
  • Draw Begin Event
  • Draw Event
  • Draw End Event
  • Post Draw Event
  • Draw GUI Begin Event
  • Draw GUI Event
  • Draw GUI End Event
 
O

OnceADrog

Guest
Right.

So the reason my code was borked is I put room-related variables (room_scale) in the room creation code?
And it is stepping parent objects before the room exists, and before there are any children instances to step.

And even if I check if the room instance exists and assign a placeholder value, it still borks.

Not only that but it blames it on another object, not the room (which was not in the line referenced and confused me for ages!).
 

RangerX

Member
The order in which the creation events are read is this:
- Object's Create Event
- Instance's Creation Code
- Room's Creation Code

Order of events is super important to grasp. It can save you alot of useless headaches.


What I don't understand is why you always make difference between create code and create event?
For me this is the same.
As hopefully you can understand with this thread, the create event of an object and the creation code you write in the instance are 2 seperate events.
Create code of the object is read and then, the creation code in the instance is read.

Creation Code is useful when you want to set something locally. Per example, let's say I have multiple instances of "object_door" in the room by I want to identify them precisely.
In that case its super useful. Per example I set a variable to identify the door and then my door button object cycles through the doors to find the right id.
 

Yal

šŸ§ *penguin noises*
GMC Elder
Another useful example: NPCs with a single line of dialogue. In their Create event, they set my_message to "". Obviously those aren't gonna be very interesting to interact with, so we use creation code to set their sprite and message.


Also worth pointing out: the Game Start event is run after the Room Start event, if the room you're in is the first room entered since the game started. Basically the Game Start event is super-iffy and I'd recommend using the Create event in an object in a special setup room instead (it sets up global data and then immediately goes to the next room), it's super easy to mess stuff up if you reorder rooms with objects with Game Start events in them to test something in a later level.
 
Top