Disallowed operations in the same Tick

1 favourites
  • 5 posts
  • Hi,

    I seem to have issues if I try and create objects in a while loop, and then in the very next event try to select from them based on the randomised variables I added.

    The only way I can get it work is to apply this logic separately on the next tick, by placing it above the original event, the only issue is it was in a setup function, but now I have to take that logic out of the function. I have encountered this a few times, for example you cant seem to destroy objects in a while loop and assume they aren't there in the next iteration.

  • After you create an object, it's only picked for that top level event you created it in. If the engine moves onto the next top level event, your newly created object cannot be picked anymore until the start of the next tick. It's just a quirk in Construct you have to get used to.

    So you'll need to find a way to manipulate your newly created object within the same top level event before the engine moves onto the next top level event.

  • Fib, sorry that's incorrect. You can access the object within the same scope it was created, but not outside of it in the same event. The newly created object can be picked in the next top level event.

    A trick I use all the time is to create objects in "On start of layout", then have a second "On start of layout" to access the new objects and do anything I need with them.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • EDIT: blackhornet beat me to it - I should have refreshed :)

    Actually, the rules around creating objects and picking are slightly more confusing.

    The new object is picked in the event that created it (unless a loop is creating multiple objects - then the last one created will be the only one picked).

    during that event (or any functions you call from that event), you can only pick the new object by its UID (or "Pick Last Created"). So, if you call a function and want to use the new object, you must pass the UID in so that the function can pick it. You can't pick by instance variables, or any other way (x, or y values, etc.)

    After the top level event that created the object, you can then pick it by all the normal methods. You don't have to wait until the next tick - just the next top level event. That is when C3 finishes the process of creating new objects.

    What a lot of people will do is add a "Wait 0 Seconds" action after creating an object. This defers further actions until the end of the current tick, so by then the object is pickable with all the regular methods. I think that is where the "you must wait until the next tick" idea comes from. But I have seen people get themselves into lots of trouble by misusing Waits, so I think it is a better idea to set up your eventsheet to use some kind of flag, and access the new objects in a different top level event.

  • Thanks. While in a while loop I was moving the positions, checking for overlap until no overlap found. At the top of each while loop I was selecting sprites. Every 50 iterations I was destroying an object to make the overlap test work better. Then when re-picking at the top of while loop...destroyed objects were still there. I ended up simply marking them for disposal with a variable and killing them in the next event.

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