0 Favourites

Objects spawned in function don't exist after call?

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

    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:

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

    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!

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

  • There is no 'fix'. This is by design. It's been discussed many times. You have you work around it.

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

  • 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

  • From the FAQ:

  • Thanks blackhornet, I will look into it.

  • Shouldn't events in functions count as top-level events? In that post Ashley mentioned that exceptions were made for triggers and groups...

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

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

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • from the manual: The action then triggers the corresponding On function event (e.g. On function "CreateEnemy"), running the event's actions and any sub-events, before returning to the original Call function action and continuing from where it was.

    so maybe it is a scope issue?? okay, now I'm confused to why Wait fixes it. Unless wait causes the proceeding events to happen after the tick? Wait 0 works and I assumed proceeding events were pushed to the end of the sheet, yet still in the same tick.

    @blackhornet do you know why "wait" fixes it?

  • I'm not the person to ask "why" C2 does what it does

  • A function is run as if it was instead placed where the function call was. Minus any picking. So it doesn't count as a toplevel event.

    "Wait 0" is a fix since it waits to run the following actions and sub events till the end of the event sheet where created objects are pickable as normal.

    As for what is happening consider

    start of layout

    ---- create sprite

    Before the event you can think of the state of the engine like this:

    sprites=[0,1,2] // A list of all the sprite instances

    picked=[] // A list of picked sprites

    new=[] // A list of new sprites

    Inside the event "picked" gets a list of all the sprite instances

    sprites=[0,1,2]

    picked=[0,1,2]

    new=[]

    The "create" action adds the the new object to "new" and picks only that.

    sprites=[0,1,2]

    picked=[3]

    new=[3]

    Then upon leaving the event the "new" list is moved to the "sprites" list:

    sprites=[0,1,2,3]

    picked=[]

    new=[]

    So the main point is the "new" list is only merged with the "sprites" list between toplevel events.

  • Wow R0J0hound , that is a great explanation. I finally understand how this thing exactly work! Thanks! A post that deserve a bookmark!

  • sorry blackhornet you seemed to know since you said this has come up before.

    thanks R0J0hound - a nice visual explanation! So does Wait 0 sort of make the Function act like a top level event?

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