How do I properly call a function within a function?

Get help using Construct 2

Post » Tue Jan 20, 2015 8:20 pm

I'm trying some tests with functions to understand them better. In this example, I'm calling a function that creates a box. Within that function is a call to another function to immediately change its opacity.

When I call the function "createbox", the box is created, but the opacity isn't changed ("fader" is not working). However, if I remove the "fader" call and assign it to its own key (like 'On F released, call "fader" (green), then it works. But, shouldn't it also work within the "createbox" function? Thanks.

boxtest.PNG


boxtest.capx
You do not have the required permissions to view the files attached to this post.
B
13
S
2
Posts: 20
Reputation: 756

Post » Tue Jan 20, 2015 9:26 pm

You can do "Green -> Set opacity to Function.Call("fader")"

And on function "fader" you do the Function return value to whatever the opacity value you want
B
84
S
22
G
15
Posts: 1,039
Reputation: 15,249

Post » Tue Jan 20, 2015 9:29 pm

You've come across the classic "can't pick newly created objects" problem. The universal solution would be a "Wait 0" before your other function call, but I personnaly hate that. I really prefer the "Pick by UID" solution. The "Pick by UID" condition is unaffected by the "can't pick newly created objects" constraint.
B
74
S
31
G
26
Posts: 994
Reputation: 20,198

Post » Tue Jan 20, 2015 10:34 pm

Thanks guys. Both suggestions worked out.

@magistross Is there a functionality reason to avoid Wait 0, or is it more of a coding practice thing?
B
13
S
2
Posts: 20
Reputation: 756

Post » Wed Jan 21, 2015 5:13 am

Wait 0 pushes the remaining actions at the end of the tick, thus interfering with the normal flow of the game logic. I prefer my code to execute when I want it to, and not being forced to execute it later. But in the end, I guess it's more or less a preference in coding practice.
B
74
S
31
G
26
Posts: 994
Reputation: 20,198


Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, Syazanie Amirin and 9 guests