event works in group, not in function

  • 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.

    <img src="https://dl.dropboxusercontent.com/u/57755595/pics/functionArray.jpg" border="0" />

    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... <img src="smileys/smiley5.gif" border="0" align="middle" />

  • Oh yeah,

    MERRY CHRISTMAS!    <img src="smileys/smiley10.gif" border="0" align="middle" />

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

  • 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!

  • 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.

  • 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.

  • Does anyone have an idea why this happens?

    Should I file a bug?

    thanks!

  • Re: older bug report:

    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.

    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.

  • 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?

  • Here is what is happening as stated by Blackhornet:

    <img src="https://dl.dropboxusercontent.com/u/5426011/fixed/Creation_picking.GIF" border="0" />

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

    <img src="https://dl.dropboxusercontent.com/u/5426011/pics/picking_new.png" border="0" />

    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.

  • Solved!

    Thanks so much all of you!

    This is valuable info ;)

    Regards.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)