Picking objects by conditions and calculating total... value of sheep in field

0 favourites
  • 3 posts
From the Asset Store
A very simple to set up ZElevation based depth of field system for your game. Fully commented.
  • Lets say we have Field and Sheep.

    Sheep have a instance variable "value", and fields have variable "value"

    If I want to calculate the total value of sheep in a particular field:

    1st: set Field.value = 0

    2nd: Field overlaps Sheep -> Field Add sheep.value to field.value

    ...But the above only adds the value of the first picked sheep, not all picked sheep. Is there a way to add the value of all picked sheep?

    I was trying to avoid for each loops, for academic reasons :D

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I don't think it's possible to do without a loop.

    You can add sheep value to the field value as sheep enter the field with "On collision" event. But if they are already there, this won't work, you'll still need for-each loop.

  • dop2000

    Thanks for confirming. I ran a sample project testing different ways of picking sheep, just for fun.

    I tried the method I mentioned in OP, using overlap, then using a for each. I also created a variant using a script.

    Alternatively,

    Using an array attached to each field to maintain UID of sheep in the field, looping through each field, picking the ascociated array by uid, then looping through each element in the array, picking the sheep from the array value,and addings its worth to the field.

    This was the best solution and hadnles large numbers of fields and sheeps much better than the simple overlap.

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