Objects spawned in function don't exist after call?

Get help using Construct 2

Post » Mon Sep 22, 2014 3:38 pm

Hi,

I have a function that creates a number of objects. I want to reference these objects as a group (i.e. pick them all together) after they have been created, in the same tick. C2 seems to have an issue with this however, as for some reason, the objects don't seem to exist right after being spawned.

Image

In the example code above (and the attached capx) you can see that it's a pretty straightforward function call which spawns a number of objects. After the call, their animation frames should be changed. However, they don't - the only instance which gets a frame change is one already in the layout before the function is called.

I'm not looking for a specific workaround for the above example, what I'm looking for is a way to pick all the objects that were spawned by the function in the same tick, and in the same event branch. for example, this works as intended:

Image

But it's not a solution for me because (in my actual implementation) these events happen within another function:

Image

For clarification: I'm not looking for a workaround to get this specific example working - I'm looking for a way to fix this within the constrants set (picking all the objects in the same tick, in the same event branch.)

I'm pretty sure the problem has to do with an issue I remember reading about, something about objects not being created until the next top-level event or something.

Is there any way to get around this?

Thanks!
You do not have the required permissions to view the files attached to this post.
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,699

Post » Mon Sep 22, 2014 3:46 pm

I believe you need to put in a 0.1 wait after the function call since the Function is happening at the same time as the For Each Sprite.

I don't think those objects exist yet at the time it's executing.
B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865

Post » Mon Sep 22, 2014 3:55 pm

There is no 'fix'. This is by design. It's been discussed many times. You have you work around it.
ImageImageImage
B
71
S
22
G
249
Posts: 3,761
Reputation: 138,573

Post » Mon Sep 22, 2014 4:11 pm

Why would you not change the sprite in its creation event?
That's been the touted standard since day one, just like avoid using unnecessary for eaches.
Image ImageImage
B
171
S
50
G
180
Posts: 8,395
Reputation: 113,984

Post » Mon Sep 22, 2014 4:20 pm

@newt it's a bit difficult to explain why I can't change it in the creation event - basically it needs data unknown until all of the objects have been spawned.

OK so it's definitely just an engine limitation. I tried to search the forums but couldn't find anything on it - Can anyone link me? I'd need more details (like what defines the next top-level event) before I can find an effective workaround.

Thanks guys
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,699

Post » Mon Sep 22, 2014 4:42 pm

ImageImageImage
B
71
S
22
G
249
Posts: 3,761
Reputation: 138,573

Post » Mon Sep 22, 2014 4:55 pm

Thanks @blackhornet, I will look into it.
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,699

Post » Mon Sep 22, 2014 5:03 pm

Shouldn't events in functions count as top-level events? In that post Ashley mentioned that exceptions were made for triggers and groups...
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,699

Post » Mon Sep 22, 2014 11:31 pm

I can't find any doc on how events in functions work opposed to normal top-to-bottom events that are checked once per tick. but from my experience they seem to run parallel (or at least out of sequence) to normal events since they branch off. Which is probably why it's a good practice as @newt said to change the sprite inside the function instead of after.

When I add a 0.001 Wait after your function call I get all green check boxes. Now that might be messy code, but it exposes the issue. It seems the For Each Sprite event (setting animation frame) is happening at the same time as the Create event For-Loop and its NOT happening top down. So how could those objects exist? I'm talking about the order of events inside 1 tick.

I had a similar thing happen where I use RexRainbow's CSV plug-in. I called a function which modifies the CSV(comma delimited file) in memory. And my very next event after the function call references that change that happens inside the function but I kept having problems as if the function didn't work. It turned out to be order of events issue. The only way I could work around it was to "wait" for the function to be done.
Last edited by jobel on Tue Sep 23, 2014 12:13 am, edited 1 time in total.
B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865

Post » Mon Sep 22, 2014 11:46 pm

Also "Wait 0 seconds" postpones the following actions until the end of the event sheet. So depending on what you are doing that might muck things up.

For me it worked because it was just database stuff and displaying information.
B
100
S
33
G
16
Posts: 1,204
Reputation: 16,865

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 31 guests