Why do I need to use For Each when calling functions?

0 favourites
  • 7 posts
From the Asset Store
Medeival & Viking War Horns Sounds / 22 Unique Calls / 2:40 minutes of audio
  • Here’s my code. I have 3 Sprite objects, and all have state set to “good”. When I press the right arrow, “count” is set to 3 (i.e., 3 sprites are picked), but the “move” function is only called once with the UID of the first sprite create, so only that sprite moves.

    Can someone please explain to me why this code only calls the "move" function once, and not once for each object picked? Per the C3 docs: Another way to think about an event is "If all conditions are met then run actions on the instances meeting the conditions". https://www.construct.net/en/make-games/manuals/construct-3/project-primitives/events/how-events-work

    I understand that adding a ‘for each Sprite’ condition before calling “move” will get all sprites to move, but I’m trying to understand why this is necessary.

    Thanks

  • Let me take a stab at answering my own question: It's because calling the "move" function is not an object-specific action. Only object-specific actions (e.g., Sprite set animation) are called once per picked object. Actions that are not object-specific are only called once per event, regardless of how many objects are picked.

    Is this accurate?

  • It has to do with the way picking works.

    Actions always try to apply to one object. Without a condition, an action has no way to pick an object, so it applies to all objects.

    But when you add a condition, the action gets some instruction. If the condition could be satisfied by multiple objects, it picks one at random. Adding "for each" forces the actions to repeat for each object that satisfies it.

    So in your top event, the function is only picking one random Sprite uid. If you add a "for each sprite" on that subevent, it should call the function three times.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • because there's only one instance of the function object

  • Thanks for the reply. I'm trying to understand what exactly are the rules for running functions against a picked set of objects.

    Actions always try to apply to one object. Without a condition, an action has no way to pick an object, so it applies to all objects.

    Per my 2nd post above, I believe the rule is: object-specific actions are run once for each object picked, non-object-specific actions run only once per event (regardless of how many objects are picked).

    But when you add a condition, the action gets some instruction. If the condition could be satisfied by multiple objects, it picks one at random. Adding "for each" forces the actions to repeat for each object that satisfies it.

    So in your top event, the function is only picking one random Sprite uid. If you add a "for each sprite" on that subevent, it should call the function three times.

    Re: "random", I'm guessing C3 doesn't pick objects at random. There are probably some rules it follows for to decide which object UID to pass to the function - e.g., it picks the object with the smallest UID. That's what my code does every time, it picks the Sprite with the smallest UID.

  • because there's only one instance of the function object

    "function object"? As I understand it, functions aren't objects and they don't have instances. Instead, they're events. Per the docs:

    Functions are special kinds of events that can be called from actions. https://www.construct.net/en/make-games/manuals/construct-3/project-primitives/events/functions

    But maybe I'm misunderstanding what you're saying...?

  • u have many sprites but one function. function happens once, doesnt care how many sprites there are.

    if u want u can do "sprite add to instance variable whatever" and then instead of adding to the variable u give ur function a return type so it can be called in expressions and do function(sprite.uid) inside the add to instance variable whatever to call the function multiple times without a for each

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