Is this a bug? Can't pick objects created in the same tick

Discussion and feedback on Construct 2

Post » Wed Feb 25, 2015 4:07 pm

Hi everyone. I just wanted to make sure this was a bug (and not a known one) before I reported it. It seems pretty fundamental and like it would have been reported by now, but I couldn't find it with any of the search terms I tried.

Basically, if you create an object you cannot perform any actions on it outside the event that created it (and it's subevents) until the next tick.

This capx is a simple example.

After creating the box the action to set it's scale to half size works since it's in the same event.

The next 'every tick' takes place immediately afterward and sets the size back to normal and changes the angle, but it applies to every box except the one created in that tick.

If you toggle on the 'Wait 0 seconds' action, it works as expected since it is no longer the same tick as the creation.

Image
Last edited by lucid on Wed Feb 25, 2015 4:16 pm, edited 1 time in total.
Spriter Dev
B
94
S
21
G
12
Posts: 3,250
Reputation: 16,710

Post » Wed Feb 25, 2015 4:16 pm

I'm not sure, but i think this has something to do with the order Construct uses to perform actions each tick.

I think Construct creates objects at the final part of a tick. So the scaling action will be runing earlier then the creating action.
B
13
S
3
Posts: 43
Reputation: 857

Post » Wed Feb 25, 2015 4:20 pm

@lucid
I think this is expected behavior.

I usually have my creation methods wrapped in a custom function called "Spawn (ObjectName, X, Y)" which returns the UID of the new object. If you explicitly pick by UID after the creation, then this problem shouldn't happen.

It's best to just have a master Creation function to handle spawning of all objects. Having it also lets you "spawn by name" so you can easily switch Enemy.Projectile = "HomingMissile" or Enemy.Projectile = "RegularBullet", assuming these are Object Type names. Keeps your code clean.

EDIT: Screenshot
Image
Last edited by cacotigon on Thu Feb 26, 2015 1:34 am, edited 1 time in total.
B
31
S
7
G
8
Posts: 232
Reputation: 6,194

Post » Wed Feb 25, 2015 4:35 pm

@lucid

you might try shift right the event 3.

The SOL would affect remain actions and sub-events. But it would not affect remain events in the same level or higher.
B
99
S
22
G
197
Posts: 4,216
Reputation: 113,797

Post » Wed Feb 25, 2015 4:57 pm

Thanks for the responses everyone. I had never run into this before now.

The situation was basically

Code: Select all
On Function "create everything"
    --loop
          --create several objects and set their initial private variables according to an algorithm
    --every tick
          --call function "update everything"

On Function "update everything"  (used outside the "create everything" function as well)
    --several conditions here checking objects' private variables
          --perform actions on each object based on some algorithm involving each object's private variables


So using the 'wait 0 seconds' before the "update everything" in the first function was the cleanest workaround.
Spriter Dev
B
94
S
21
G
12
Posts: 3,250
Reputation: 16,710

Post » Wed Feb 25, 2015 5:04 pm

Yeah, after creation, outside of the same event or sub-events you have to wait till the next toplevel event to be able to pick the new object.

More details I've compiled in the past. CC did it too.
viewtopic.php?f=147&t=116098&p=837060&hilit=toplevel#p837060
viewtopic.php?f=162&t=60211&p=517685&hilit=toplevel#p517685
B
84
S
27
G
70
Posts: 4,933
Reputation: 49,558

Post » Wed Feb 25, 2015 5:18 pm

Thanks @R0j0hound, that was very useful.

I didn't realize that you could still affect the newly created objects from other top-level events, or that the function triggers didn't count as top-level events. Good to know.
Spriter Dev
B
94
S
21
G
12
Posts: 3,250
Reputation: 16,710

Post » Wed Feb 25, 2015 5:20 pm

Thanks for the responses everyone. I had never run into this before now.

Think its a problem everyone runs into at one point or another :D

However I would suggest that you design your game to not use wait 0. It can cause problems.
In your case you could just move the actions from the second "every tick" to the first one and it would work without wait 0.

A rule of thumb is as Rojo pointed out as well. When you create an object, you have to initialize it in the same event, or a sub event to where it was created.

Code: Select all
All in same event
Top level: Create object
                   Set object color (Works)
                   -----> Sub level
                             Set object size (Works)

Top level: Set object position (Wont work)


function triggers didn't count as top-level events.

Just saw this :D They will work, you just have to make them in the sub level or same level as the create, as shown in the code example.
B
43
S
11
G
2
Posts: 1,175
Reputation: 6,744

Post » Wed Feb 25, 2015 7:15 pm

Check to:
--call function "update everything"
B
8
S
1
Posts: 110
Reputation: 767


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 3 guests