Create event event order

Just started using Construct 2? Post your questions here

Post » Mon Aug 24, 2015 10:59 pm

does the create event execute before the variables set after spawning the object?

or does it work in line order?

for example witch var would be set first in a code like this?

Code: Select all
on X pressed
spawn projectile at layer 1 point 0
projectile set instance var1 to 1

on projectile created
projectile set instance var2 to 1
B
14
S
6
G
1
Posts: 150
Reputation: 1,987

Post » Tue Aug 25, 2015 4:49 am

Both var1 and var2 will be set to 1 but C2 works in a cascading hierarchy. When a layout is started C2 reads down the list top to bottom going through each event so long as the conditions are meet. The important thing to understand about the coding is what events take priority over others.

So if you have something like a bomb we are detonating at random

On Layout Start -> Set var1 == 0
For EveryTick -> Set var1 == round(random(0,9))
On Function "Boom" -> Destroy SpriteBomb & Play Boom sound clip
If var 1 == 5 -> Then do function "Boom"

In this case c2 looks from top to bottom for conditions it first sees On Layout Start which has a green arrow, meaning it is done first. So var1 is set to 0, this is also a condition that triggers once and is done unless the layout is restarted.

next going down it sees EveryTIck, Every Tick is a way to say "Always do this till I say stop" so every moment the game is running it is setting var1 to a random number between 0 and 9, it is also being rounded to a whole number. This event will continue to do its own thing without the program really having to do anything

Next is a Function called "Boom" Since it has not had it triggered it skips it entirely

Next is a condition seeing if var1 is equal to 5. It will bounce back in forth between the Everytick and this condition till the condition is meet. once var1=5 then the function is called and it skips straight to it. Note "Boom" is specific so you can have as many functions in a event sheet as you want and know that one trigger can do more than one action provided that each on function matches the string that the function is called

Finally there is a exploding animation then the sprite is destroyed and then a sound fx is played in that order

Events only matter what order they come in if they have either the same trigger. otherwise most of your event sheet will be inactive but c2 runs that checklist constantly till a condition is meet

I hope this was helpfull

CrazyVulcan
"Were all mad here. I'm mad, your mad." "How do you know I'm mad?" Asked Alice. "You must be" Said the Cheshire Cat. "Or you wouldn't have come here"-Lewis Carroll: Adventures In Wonderland
B
7
S
3
Posts: 113
Reputation: 1,295

Post » Tue Aug 25, 2015 10:14 pm

so they would both be set at the same time?
I don't quite understand
B
14
S
6
G
1
Posts: 150
Reputation: 1,987

Post » Tue Aug 25, 2015 10:19 pm

You could test it yourself..
One will be set before the other, so if you put something like

on X pressed
spawn projectile at layer 1 point 0
if var2=0 then projectile set instance var1 to 1

on projectile created
if var1=0 then projectile set instance var2 to 1

So whichever event gets run will show you which is set first. You'd use the debugger to check the values.
B
48
S
23
G
66
Posts: 1,144
Reputation: 39,059

Post » Fri Aug 28, 2015 9:27 am

From my experiences, var2 would be set first.
I find the "OnCreated" event is like a parameterless constructor in programming languages.

I wanted to do something like what you did here and I had to use a seperate instance variable "JustCreated" with default value 1. I set up my object when it is 1 and immediately set it back to 0. It is not event based, but works fine.
B
10
S
2
G
1
Posts: 51
Reputation: 761

Post » Fri Aug 28, 2015 11:18 pm

thankyou I will try this when I get the chance
B
14
S
6
G
1
Posts: 150
Reputation: 1,987


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 2 guests

cron