Can't select dynamically created objects inside a Function

Discussion and feedback on Construct 2

Post » Thu Feb 16, 2017 12:00 am

Message: ikazuchi can only post plain text URLS until they have 500 rep. 2 URLS modified. Why?
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.

Image
https://drive.google.com/open?id=0B-5l35zB3rv3Nlc4bUdJMVd0NjQ

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

Image
https://drive.google.com/open?id=0B-5l35zB3rv3QnU2S01mUGg3YWM

Does anyone have an idea why picking instances won't work inside the function, but does when it's outside of it? THanks!
B
4
S
1
Posts: 6
Reputation: 312

Post » Thu Feb 16, 2017 1:15 am

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.
Image ImageImage
B
170
S
50
G
179
Posts: 8,378
Reputation: 113,425

Post » Thu Feb 16, 2017 1:41 am

@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.
B
98
S
34
G
20
Posts: 438
Reputation: 17,825

Post » Thu Feb 16, 2017 4:27 am

Thanks newt and AllanR... it makes perfect sense now!
B
4
S
1
Posts: 6
Reputation: 312


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 6 guests