Function not being called on every instance? (answered)

0 favourites
  • 3 posts
From the Asset Store
2D fighting template based in the game that defined the fighting games genre.
  • 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:

    [attachment=1:2c10tc4z][/attachment:2c10tc4z]

    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:

    [attachment=0:2c10tc4z][/attachment:2c10tc4z]

    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!

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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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!

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