Foreach count of newly created objects

Get help using Construct 2

Post » Wed Oct 03, 2012 8:21 pm

Hi all

I have a problem in my game trying to do a foreach loop on some created objects. I'm not 100% sure it's the same problem, but I'm wondering if it's related too this breaking change.

Screenshot from R104. Also tested in R105.


Weirdly I had some similar code in my game that worked, but broke in R105. From the test above it should never have worked in the first place! Was there any changes related to the R100 breaking change that was made more strict in R105? Is it just luck that I had code similar to above that worked in R104 and broke in R105?

Cheers.
B
24
S
4
G
1
Posts: 244
Reputation: 3,462

Post » Wed Oct 10, 2012 12:33 am

Anyone have any ideas? This will be the one and only bump I'll do on this.
B
24
S
4
G
1
Posts: 244
Reputation: 3,462

Post » Wed Oct 10, 2012 12:55 am

This fix in r104 should mean this works properly:

[quote]Breaking change: Object's 'Count' expressions are now immediately updated inside loops and subevents when new instances are spawned or created, contrary to the fact these instances do not really exist until the next top-level event. This may help fix projects broken by the last breaking change which depend on object counts in subevents, but unfortunately may again break projects which already adapted to the new behavior. Sorry - in the long run it's for the best that we make these changes.[/quote]

So it's strange that it doesn't. I don't believe r105 changed how it worked. Can you post a .capx?
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Wed Oct 10, 2012 1:56 am

B
24
S
4
G
1
Posts: 244
Reputation: 3,462

Post » Wed Oct 17, 2012 12:50 am

So did this turn out to be a bug or am I doing something wrong?
B
24
S
4
G
1
Posts: 244
Reputation: 3,462

Post » Thu Oct 18, 2012 4:52 am

Last bump
B
24
S
4
G
1
Posts: 244
Reputation: 3,462

Post » Thu Oct 18, 2012 10:31 am

i have some similar problems too, especially with families
B
41
S
11
G
4
Posts: 428
Reputation: 7,356

Post » Thu Oct 18, 2012 12:12 pm

Yeah it's because of the change you linked to in your first post. The objects created in event 2 can't be picked until the next top level event. So in event 3 there is only 1 sprite - the initial one. Though sprite.count would still return the correct number (60).
B
54
S
28
G
18
Posts: 1,520
Reputation: 24,990

Post » Thu Oct 18, 2012 2:58 pm

See now when I heard the term top level event, I figured it meant the top level of the event that spawnd the objects - in this case line 2 would be the top level event for the object creation. Is the keyboard trigger the actual top event? How would you get around this then if you were creating objects inside a while loop?

The issue I'm running into in my game is I have a while loop that, depending on conditions, spawns 20-30 objects. I then assign an instance variable to them and use them in a foreach statement further down. If I need to return all the way to the top level event, I don't see how I would use a while loop unless I split it into two or three while loops. Is this just the expected way it should work?
B
24
S
4
G
1
Posts: 244
Reputation: 3,462

Post » Thu Oct 18, 2012 4:34 pm

A top level event is one that's not a sub-event of anything. There's another thread here where R0J0hound links to a picture from CC that explains it.

It depends what exactly you're trying to do. You could either do whatever you're doing in the for each loop inside the while loop instead, immediately after the sprite is spawned. Or inside an 'on created' trigger. Or you could set a variable true when the sprites are spawned and have a top level event 'var = true/ for each sprite' -> do stuff.
B
54
S
28
G
18
Posts: 1,520
Reputation: 24,990

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 11 guests