event works in group, not in function

Discussion and feedback on Construct 2

Post » Tue Dec 24, 2013 5:21 pm

Hi,

I'm confused...
I have a sequence of conditions and actions that work without any problem in a group. But when duplicated for use in a function it doesn't.



The issue lies with the 'for each' loop that is highlighted in the screenshot.
-In the 'makeDecks' function that preceeds it, I create the classic_back objects.
When fetching the UID's it is as if no classic_back object exists although the debugger shows there are 52.
The card array stays empty (1,1,1), and when I trace the UID values to a textfield it stays empty. When I add a 'pick all' condition prior to the 'for each'...empty.
No return value whatsoever. No "0", no "-1".

When I execute the exact same sequence in a group, and activate/deactivate it as if I would call the function it works like a charm.

Any idea what am I overlooking here?
In what way is there a difference between a group or function?
Could this be a scope issue? Seems unlikely since I created many other functions that use 'cardArray' and other arrays and those work just fine.
Or does the 'for each' execute while the makeDecks() function is still running? Seems unlikely too.

I can use a group to circomvent the problem, but I'd prefer not to.
Stumped... Moongaze2013-12-25 00:41:52
B
8
S
2
Posts: 26
Reputation: 604

Post » Tue Dec 24, 2013 5:23 pm

Oh yeah,

MERRY CHRISTMAS!     Moongaze2013-12-24 17:24:49
B
8
S
2
Posts: 26
Reputation: 604

Post » Tue Dec 24, 2013 6:09 pm

Well you probably don't need the first for each loop. Adding a Destroy with no prior selection will just destroy everything.

Just as a test. You might want to wait a tick before calling makeDecks and going to the next for loop after creating the next Array. I remember having a problem with newly created arrays.

B
92
S
18
G
9
Posts: 2,455
Reputation: 15,113

Post » Wed Dec 25, 2013 1:14 am

Thanks for pointing out the unnecessary 'for each' jayderyu.

A wait of 1 second does not solve it. I've tried it after creating the array, and after calling makeDeck().
Setting the array size does not help either.
I'm really curious why this doesn't work. Is it my fault? Is it a bug? Is there something I don't grasp about how C2 works?
The event sequence is pretty basic though.
All that happens in makeDeck() is a loop in a loop creating/pinning/setting instance vars for sprite objects. That runs fine so I doubt that is what is interfering with the push to the cardArray. Unless the 'for each' runs in parallel with the makeDeck() function. (in another thread?)

The classic_back sprite and all the sprites I destroy in the function are part of a container, if that is of any concern.
I probably don't need to destroy all sprites in the container come to think of it. Just one would do the trick.
-EDIT--- I removed all the unnecessary destroys, does not solve the issue.----

If I have the time I will try to reproduce this in a test project.
In the meanwhile any comments or suggestions are welcome!Moongaze2013-12-25 01:47:46
B
8
S
2
Posts: 26
Reputation: 604

Post » Wed Dec 25, 2013 1:21 am

I'm not sure what it's supposed to be doing exactly, but it looks like you're destroying all the objects in the first for each loop then trying to run a for each on the objects that have been destroyed. Try having a every tick event after the second for each that destroys the classic_bck object instead.
Moderator
B
95
S
34
G
33
Posts: 3,007
Reputation: 27,876

Post » Wed Dec 25, 2013 1:41 am

Its basically a start/restart game event sequence.
I destroy all objects and the relevant array, then recreate them (makeDeck() and cardArray create) then start the for each that is supposed to fetch all UIDs and push them into the array.
The 'for each classic_back' simply does not run, although the classic_back objects that makeDeck() creates are definately there and are shown in the inspector/debugger.

As stated above, the exact same event sequence works fine when used in a event group and triggered by activate/deactivate group.Moongaze2013-12-25 01:43:29
B
8
S
2
Posts: 26
Reputation: 604

Post » Fri Dec 27, 2013 1:18 pm

Does anyone have an idea why this happens?
Should I file a bug?

thanks!
B
8
S
2
Posts: 26
Reputation: 604

Post » Fri Dec 27, 2013 2:28 pm

Re: older bug report: [QUOTE=Ashley] Closing as by design. I'm afraid that it's a limitation of the engine that objects created in subevents are not fully created until the next top-level event. That means newly created instances cannot be used in immediately following sibling events.[/QUOTE]
I'm guessing this is your issue. In a group, everything is shifted over one level to the left so your 'For each' loop is a top-level event, but in a Function, it is not. There's no need to make the post-Create events siblings, make them sub-events of the Create and it should all work fine. As sub-events they are still in the scope of the Create.

One of the few counter-intuitive issues in C2.blackhornet2013-12-27 14:29:41
ImageImageImage
B
71
S
22
G
258
Posts: 3,791
Reputation: 143,198

Post » Fri Dec 27, 2013 4:32 pm

After creating the array, you could wait 0 seconds, because I believe some object properties do not become available the moment they are created. Waiting 1 second wouldn't make it. I am not sure, but could you give a try?
B
18
S
4
G
1
Posts: 332
Reputation: 3,149

Post » Fri Dec 27, 2013 6:46 pm

Here is what is happening as stated by Blackhornet:


And here is one possible way to get your function to work:

The first mirrors your issue. The for-each will only affect existing sprites. The second waits until the end of the tick to do the actions when the new objects are pickable.
B
94
S
33
G
118
Posts: 5,393
Reputation: 75,847

Next

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 5 guests