Function not being called on every instance? (answered)

Get help using Construct 2

Post » Thu Nov 13, 2014 5:58 pm

I'm noticing some behavior I don't understand. I've set up a simple test project with a few text objects, leaving them the default black color, and then added the following event:

event_color.png


When I run the layout, it works as expected: all the text objects turn blue. Then I changed the events to the following, disabling the action to change the color and creating a function to do it instead:

event_function.png


I would expect this to do the same thing; I pass in the UID for each text object as well as the color I want it to change to, and then the "ChangeColor" function picks the text instance with the given UID and changes its color. However, when I run the layout it only changes the color of one of the text objects.

Why is that? Is the function not getting called on every text object? If so, why would it apply the "set font color" action to each text object, but not the "call function" action? Or is something else going on?

Thanks!
You do not have the required permissions to view the files attached to this post.
Last edited by inejwstine on Thu Nov 13, 2014 7:26 pm, edited 1 time in total.
B
6
S
1
Posts: 38
Reputation: 683

Post » Thu Nov 13, 2014 6:20 pm

Actions do an implicit loop over all corresponding object types (same type as "self") in the SOL. However, there is only one Function object, so only one call is ever issued, even if there are multiple Text objects. "Text.UID" in this context is then the same as "Text(0).UID", so only the first Text object change its color. In those special cases where an implicit loop is not an option, now is the time to use the for each construct. In your example, only add a "For each Text" under the "start of layout", it should work as you intended.
B
69
S
28
G
24
Posts: 977
Reputation: 18,739

Post » Thu Nov 13, 2014 6:54 pm

So THAT'S what "for each" is for! I read the entry in the manual for it but still wasn't sure what it did.

And you're absolutely right, that fixed it. And thanks for the explanation as well; I understand now why that was happening. Thanks a bunch! :D
B
6
S
1
Posts: 38
Reputation: 683


Return to How do I....?

Who is online

Users browsing this forum: Fradno, HellMax, marcinkowski, tarek2 and 15 guests