0 Favourites

Can't select dynamically created objects inside a Function

  • I created a For Loop inside a Function to dynamically generate three text box, each with a consecutive number.

    Right after that For Loop, I try to pick one of the text object based on its instance variable and set the text to 'A'. This renders unsuccessful as it seems as if the generated objects doesn't exist.

    drive.google.com/open

    However, if I place the code that selects the instance outside of the Function, it's able to see that the text objects exist and changes one of them to 'A'.

    drive.google.com/open

    Does anyone have an idea why picking instances won't work inside the function, but does when it's outside of it? THanks!

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Instances can only be manipulated in actions when they are picked by a condition, or in an event that they are created.

    Functions specifically break picking which can be useful, but if you need to re pick an object in a function you can pass the objects id, or uid in a function parameter.

    Just remember to use the param to do a comparison to re-pick the object in a subevent of the function.

  • ikazuchi

    The reason you can't pick the new instance inside the function is that C2 does not complete creating the new instances until the next top level event. (That is why you can outside the function - because it is in a different top level event.

    Inside the function you can continue to access the new instance in sub events, under the one that created it. So, you could add a sub event inside the for loop that says if numberDisplay.display_number=1 then set Text to "A"

    Since you try to pick the instance outside the loop, but before a top level event, C2 has not added the new instance into its internal indexes and can't find it.

  • Thanks newt and AllanR... it makes perfect sense now!

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