expected behavior?

  • Ashley I was wondering if this is expected behavior? or if i should file a bug for it, so basically inside a trigger i am create some spritefont instances inside that same trigger after the spritefont instances have been created i set some properties, then i call a function. inside that function i am just trying to log all the instances of the sprite font to the console. those 2 arrays always come back empty when using getAllInstances, if i use getFirstInstance that reference comes back null, if i add a wait 0 seconds before calling the function those objects are then logged as expected.

    I did a bit more testing and moved the java script block inside the trigger, and got the same result it seems like created instances are only available in the java script api on the next tick after they are created

    so i got a bit more curious, and i tried to execute the javascript on the on created trigger for the sprite font, and that also came up with the same results... empty array

    when creating instances in events and trying to modify them through the javascript api are there any other ways to access the created instance in the same tick? or is wait 0 the recommended approach. would it be possible to attach hooks on the life cycle of an instance, like we have hookson the lifecycle of layout?

    runtime.object.Instance.oncreated = () => { ... }
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • This is a known existing limitation in Construct. Newly created instances aren't actually fully created until the end of the top-level event. Inside the same event that creates them, they are picked, so you can use them in events, but not available on the main instance lists. This is currently by design due to the high complexity and bugs arising from modifying data structures that are actively in-use while running events.

  • Okay thanks for the explanation, based on that I will move the creation logic to the script to have access to the instances thanks

  • You can pick the newly created objects by their UID - so it is possible to pass the UID into a function, or you could set a global variable to the UID and pick it that way. Wait 0 can also be used to defer actions until the end of the current tick - which is after the top level event that created it, so the new objects are then fully available.

  • Hmmm let me try that out... And see if the instance gets picked

  • yep that worked, forcing the function to pick it, and changing the getAllInstances to getPickedInstances worked thanks

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