Test if true for each instance of an object

Get help using Construct 2

Post » Thu Nov 28, 2013 7:59 pm

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?
B
15
S
4
G
1
Posts: 286
Reputation: 1,988

Post » Thu Nov 28, 2013 8:10 pm



Use pick by evaluating to find objects with certain expression, ie object.variable = "dead"

Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,562

Post » Thu Nov 28, 2013 8:21 pm

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?lolpaca2013-11-28 20:22:25
B
15
S
4
G
1
Posts: 286
Reputation: 1,988

Post » Thu Nov 28, 2013 8:42 pm

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 :)lennaert2013-11-28 20:42:55
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,562

Post » Thu Nov 28, 2013 8:58 pm

In a few less lines of code.
pick_BHT.capx
ImageImageImage
B
71
S
21
G
220
Posts: 3,648
Reputation: 123,725

Post » Thu Nov 28, 2013 9:04 pm

[QUOTE=blackhornet] In a few less lines of code.
pick_BHT.capx[/QUOTE]

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


Thanks !!!
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,562

Post » Thu Nov 28, 2013 9:32 pm

Brilliant, thanks so much both!
B
15
S
4
G
1
Posts: 286
Reputation: 1,988

Post » Fri Nov 29, 2013 1:21 pm

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.
B
15
S
4
G
1
Posts: 286
Reputation: 1,988


Return to How do I....?

Who is online

Users browsing this forum: matrixreal, rotacak, Yahoo [Bot] and 4 guests