How do I pick an single object from a family

0 favourites
  • 8 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • So I have 3 objects grouped in a Family. Each object has a variable (Distance) that holds a number.

    On mouse click I need to loop through the family inspecting each instance variable and pick the object with the lowest value. In C# this is very simple to do, but I cant wrap my head around it with this no code approach.

    As of right now all Cannons are firing... I need only the closest one to fire.

    "Code" so far:

    [attachment=1:1ry6x96x][/attachment:1ry6x96x]

    Project so far:

    [attachment=0:1ry6x96x][/attachment:1ry6x96x]

  • I got it "done"... So ugly and ridged!

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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • One method you could try is giving the family the LineOfSight object behaviour, and then set the range to 500 or so. LineOfSight automatically checks range and will apply it to each cannon, so if your cannons were 100 pixels apart, each cannon farther away wouldn't see a target because they're 500 plus the 100 pixels to the next cannon away. You can then replace all those cannon distance checks with a check for Cannons -> Has LOS to object and set the object to be checked to be a family of enemies. You can then spawn Bullet1 off Cannons and it should automatically spawn it at the cannons which actually have range on the target.

  • inquiesco Hmm I see your point, and I think it could partially work. But the issue I have with this solution is that more than 1 cannon may resolve "Has LOS" to true. In that case 2 or more cannons would fire. Plus I also need to add additional logic to check if the cannons still have bullets left, so the next closest one would take charge and fire instead. Does that make sense?

    Check out game mechanics so far....

    ec2-54-187-14-211.us-west-2.compute.amazonaws.com/missileDefense

  • fscopel To be honest, since you're using exact measurements for checking distance rather than rounding them up, the chances of you getting equal distance between two cannons is very, very low. I tried in your example to get somewhere in the middle of 290.181271743284 (or something equally hard, ha ha) and I couldn't even equalise the .18 part because of mouse sensitivity. If you were rounding up the numbers to whole numbers then yeah, it'd be more of a problem and require solving.

    As for bullets per cannon, you can give each cannon an instance variable of bullet_count, then give them 500 or however many you're wanting. The logic of the engine when objects are in a family is that they interpret the objects in them individually, so only objects meeting the required conditions you've put to the family are triggered. Based on that you should be able to then just set fam_cannons -> Subtract 1 from bullet_count. It's a bit different moving from programming languages where we need to explicitly check each object to see if it meets conditions, which is a good thing, I think, . Though I still get caught out sometimes wanting to write JavaScript to pick out specific objects and have to hunt down the equivalent method of thought, lol.

    As for your game mechanics, looks fun, I like those games as I'm heavily nostalgic, . Hence why I'm making a space shooter, and possibly an RPG after, ha ha. Keep up the good work!

  • You can use 'For each (ordered)':

    +System: For each Cannons order by Cannons.distance ascending

    -> Cannons: Spawn bullet

    -> System: Stop loop

  • ramones I see what you did there. The only issue is that I keep the distance in each individual cannon not at the Family level. I am not sure how to use the Family level variables. Could you extend on that really quick?

    Thank you

  • ramones Never mind - I got! Thank you for the info, very helpful.

    scirra.com/manual/142/families

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