On Create firing before On Start Layout

Bugs will be moved here once resolved.

Post » Sun May 05, 2013 2:14 am

Since Construct 2 still requires the layout to have (as I workaround I can create an extra Loading Layout to handle this but anyway...) at least one instance of every object, when working on small single layout projects I usually start with:

On start of layout: ChildObj.Destroy()

I also have an On Create Master Obj : Spawn Child Obj
From what I can tell though, the On Create is firing *before* On Layout Start, causing an object to be spawned then summarily deleted. Is this order of events intentional?

Link to .capx file (required!):
Order of Events Capx

Steps to reproduce:
0. Create Layout -> Add ChildObj and ParentObj to Layout
1. Create ChildObj
2. Create ParentObj
3. Add On Create ParentObj : Spawn ChildObj
4. Add On Start Layout : Destroy ChildObj

Observed result:
Both instances of ChildObj are destroyed.

Expected result:
Only the existing ChildObj should have been destroyed, then "On Create" should have been run.

Browsers affected:
Chrome: yes
Firefox: yes
Internet Explorer: yes

Operating system & service pack:
Windows 7

Construct 2 version:
Release 126
B
30
S
6
G
8
Posts: 232
Reputation: 6,119

Post » Sun May 05, 2013 10:31 pm

I think if you switch the order of the events around, so that the "Start of layout - Destroy ChildObj" happens before "On created - Spawn ChildObj", then it will work.
If you had lots of ChildObjs on the layout and did a "On start of layout - Destroy ChildObj", it would pick all of them and destroy all of them, which is essentially whats going on if you spawn one in one event, then destroy without specifying which ChildObj to destroy in another event. :P
B
45
S
19
G
10
Posts: 562
Reputation: 9,543

Post » Mon May 06, 2013 4:21 am

As was demonstrated in the capx example, the start of Layout was already the first event in the event sheet. *Even* if it wasn't the first event linearly in the sheet itself, think about it. On Start of Layout is defined as the condition that occurs when the layout begins, I would think the layout would get initialized before objects were created on said layout.

The whole point of doing Destroy ChildObj in the "On Start of layout" is to get rid of the troublesome fact that we are required to include one instance of every object on a layout, something which I recall the makers of Construct having stated in the past that they would eventually like to rectify.
B
30
S
6
G
8
Posts: 232
Reputation: 6,119

Post » Mon May 06, 2013 4:55 am

There was a change to the "firing order" of events in R123 , that could potentially break projects, or mess you up if your used to doing things a certain way...

unsure if this will help your situation...

this is the "breaking change" log from that release:

Breaking change: 'On created' now fires just before 'On start of layout' for every object already on the layout. Previously objects placed in the layout view did not trigger 'On created', but this made it annoying to initialise objects (e.g. you'd have to pin an object both on start of layout and in 'On created'). This will probably break some projects, but it really is more convenient this way round, and hopefully all you need to do is remove the stuff you do in 'On start of layout' that is a duplicate of what happens in 'On created'.
As long as I can move left, right and fire, I'm Happy...
B
42
S
15
G
11
Posts: 655
Reputation: 12,260

Post » Mon May 06, 2013 6:57 pm

Bingo! That's it! Was not aware of this change. I'll just make it a rule to always have a "StorageLayout" where at least one instance of every object is already initialized.

Thanks Pixel perfick.
B
30
S
6
G
8
Posts: 232
Reputation: 6,119

Post » Mon May 06, 2013 10:07 pm

Closing, this is by design. Objects have to be created before 'start of layout', otherwise you could not access any objects in 'start of layout' (none would exist), therefore objects are created and fire 'On created' just before 'start of layout'.

There's nothing wrong with destroying objects on start of layout, even though they were just created. (You can't destroy an object that hasn't been created, anyway.)
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580


Return to Closed bugs

Who is online

Users browsing this forum: No registered users and 0 guests