When to use "for each" while working with families?

  • Hey,

    I encountered a problem yesterday regarding families.

    So basically I got an "enemy" family where I put all different types of enemy sprites. (they all get assigned a new ID on spawn and also a hitbox with the same ID)

    I compared the distance between my player and the family "enemy" to make the enemy do something when you come close. At first it didn't work, before I finally added "for each enemy" to it.

    In all other cases, where I do not use distance/position, the code works just fine with the family.

    So why exactly does this happen, or how do families work in this case?

    Are there any other cases where I should concern adding "for each"?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Did you use system compare two values to calculate distance? The system object doesn't pick instances. When you add the 'for each family' it runs that distance check for each instance of the family.

  • plinkie

    Yes exactly, I used "compare two values".

    Already thought it may be because of that. How is the value then compared if I don't add "for each family" ? Only for the first instance?

  • Yes it picks one instance, the one with lowest UID. If you don't add a 'for each family' then what this event is saying is - I am true if the family object with the lowest UID is this distance from the player, then if this is true, apply the 'actions' to every instance of family. It picks only the one instance of family, with the lowest UID and compares the distance, if it's true then it runs the action against all family members since nothing was initially picked. If false i.e. the family object with lowest UID is outside that distance then nothing will happen.

  • plinkie

    Okay,thanks for the explanation!

    Need to take care of always adding "for each" then.

  • You don't need to use 'for each' every time. The reason your other logic is working is because family objects are picked by prior condition from the object themselves i.e. family.x = something or family.var = something or family is on screen etc. You need to use it in this scenario because the system condition is more like a true or false without picking all the family instances where that distance check is true.

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