Test if true for each instance of an object

0 favourites
  • 8 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • Hi, I'm sure this is something pretty simple, but for some reason I don't seem to have come up against it before. Basically, I want to test if a condition is true for each instance of an object, and do an action if so. For example:

    If all [object] Zombie is [boolean] Dead, then xxx

    It'd also be handy to know if I can test for certain numbers of an instance, eg

    If 20 Zombie is Dead, then xxx

    How do I do this? <img src="smileys/smiley4.gif" border="0" align="middle" />

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Use pick by evaluating to find objects with certain expression, ie object.variable = "dead"

  • Sorry, I don't understand. The event I want to trigger is one that has doesn't necessarily relate to the objects being picked - I just want to test that a certain state is true. So for instance:

    If all [sprite - Zombie] are [boolean - Dead], then [Go to next level]

    Or alternatively

    If 20 [Zombie] are [Dead], then [Give extra life]

    As it is, I can only figure out how to test if any Zombie is Dead or not, not all of them or any given number of them at once.

    I can't work out how to do it by evaluating - please could you explain the exact events you'd use to do that?

  • Seriously, with pick by evaluate ....

    web example

    capx

    I applied it as a 1 time even on layout start, but you can plan it anyway you like, timed events or trigger whatever.

    2 indicates dead units, dead units are black, living ones blue

    It runs the for each loop once, only picks the objects with value, addds numbers and sets animation frame depending on each spawned objects "dead" variable.

    I make them text strings noadays, defining yes or no, I ran into a few issues with booleans (have this in other languages too) and prefer to resort to strings or numbers.

    Anyway, hope you can adjust to fit your needs :)

  • In a few less lines of code.

    pick_BHT.capx

  • In a few less lines of code.

    pick_BHT.capx

    Oeehhh, hadnt noticed the sprite,pickedcount expression yet :o

    Thanks !!!

  • Brilliant, thanks so much both! <img src="smileys/smiley4.gif" border="0" align="middle" />

  • As a PS to this, I found out that it won't set the variable to 0 if none of the objects have the condition; ie I guess it can't 'Pick by X' unless there's at least 1 object that's X. I got around this by adding "Else > Set [variable] to 0" after the event.

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