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

which runs first when creating instance and setting variable

Xer0botXer0

Senpai
GML:
obj_tree create event:
var inst = instance_create(x,y,obj_apple);
inst.quality = "great";

obj_apple create event:
quality = "";
nice = false;
if quality == "great"
{
nice = true;
}
As you can see an instance is created, a variable of the instance is then assigned a value the line after,
but from what I can see .. the variable quality is equal to nothing "" in the debugger..

So I assume as soon as an instance is created, the create event for that instance is run before the next line is run from the object that created it..
and so quality is only assigned a value after its create event has run.

Is this correct ?
 

Nidoking

Member
You are correct. For the purpose you appear to be intending, I either put the check for the value of quality in the Alarm 0 event and set alarm[0] to 1 in the Create event, or I put that check in User Event 0 and have the obj_tree call that event when it's finished initializing the variables.
 

Slyddar

Member
Similar to the great suggestions above, another option is you can create the instance, assign it's id as you have done to a variable (inst), change your quality value on the next line, as you have done, then just do a with(inst) and run the quality check then. I would only do this if the creation of the instance happens here only, as creating multiple instances in different locations would mean replicating code, so the alarm or user event is more efficient.
 

TailBit

Member
..or write it into an variable in the control object or a global one, and have it copy it over from that
GML:
obj_tree create event:
global.quality = "great";
var inst = instance_create(x,y,obj_apple);

obj_apple create event:
quality = global.quality;
nice = false;
if quality == "great"
{
nice = true;
}
 

TheouAegis

Member
..or write it into an variable in the control object or a global one, and have it copy it over from that
GML:
obj_tree create event:
global.quality = "great";
var inst = instance_create(x,y,obj_apple);

obj_apple create event:
quality = global.quality;
nice = false;
if quality == "great"
{
nice = true;
}
I am personally a proponent of the User Event method (I still use it), but this method is becoming my favorite for two reasons.
  1. It lets the apple do its own thing without doubling code.
  2. It's what I see in a lot of NES games.
I am kind of curious how it would work when paired with instance creation code (typically you wouldn't have both in these cases) - is the creation code performed and then the create event performed one instance at a time, or do all creation codes get run first before any create events? Bit other than that, I do like this method more nowadays.
 

TailBit

Member
When creating a instance, the create event for it is run before the code it was created in can continue.

So you could set global, create, set global, create without problem
 

TheouAegis

Member
When creating a instance, the create event for it is run before the code it was created in can continue.

So you could set global, create, set global, create without problem
I am referring to instance creation code run in the room. Does the instance creation code in the room run for all instances first?

By the way, what happened to your profile picture?
 
Top