Why do I need "for each" after picking objects?

0 favourites
  • 8 posts
From the Asset Store
Pick Up Items Sound effects for your game, Take them for a ride right now and you will worry no more.
  • Here's my code. I have a 11 sprite objects with health = 1. Pressing space subtracts 1 from their health, so all will have health set to 0.

    The code properly destroys all 11 sprites, but it only increments killCount once (the highlighted action). Why does it not increment killCount once for each sprite that meets the health <=0 condition?

    The commented "for each" section does what I want, but I'm trying to understand why I need this. I obviously don't understand object picking as much as I thought.

    Thanks

  • It can't really guess how you want to handle situations like that, you could only want one action to run. So using For Each tells it specifically what should happen.

    another way you could do it is to change the action on even 2 to say Add Sprite.pickedcount to killCount

  • You are thinking of killCount like an instance variable. It's global. The pick is building a collection of sprites. The Destroy works because it's working with that collection, but the global variable doesn't know anything about the collection, so it runs once. AllanR's suggestion is the optimal way of getting the count of sprites in the collection.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • "Add Sprite.pickedcount to killCount" -- That's good stuff right there!

    My understanding of picked logic was: iterate through every step in the action block X times, where X = picked count

    But it sounds like you're saying: iterate through every step THAT REFERECES THE PICKED OBJECT(s) in the action block X times, where X = picked count. For all other steps, execute those once

    Is my new understanding correct?

    Thanks

  • Yes. Picking provides a shortcut for the picked objects. Everything else follows the standard rules.

  • You don't always have to. If you're doing it the smart way you don't have to loop for each object all the time. In yor particular case there is zero reason to use loop every tick, because death actions can be run once on hit condition. Also you can use "self" to make all instances pick their own values without looping through each one of them. I'm not sure if it's more performant this way though, but it seems so in my case

  • On this occasion, just use - On Destroyed > Add 1 to KillCount

  • Without any conditions, all objects are picked. So when you say "sprite -> subtract 1 health", it's saying "subtract 1 health from them all".

    But once a condition is added to an action, it starts looking for an individual instance. So when you say "Sprite health ≤ 0", it's asking "if you find one Sprite with health ≤ 0, do [this action]". It'll pick out one random instance of Sprite, and apply the action to it.

    So "For each Sprite" means you want all the instances that meet your conditions, not just one random one.

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