Pick by evaluate / for each ?

Just started using Construct 2? Post your questions here

Post » Wed Feb 18, 2015 5:33 pm

Hi.

I would like to pick some instances by evaluate, then iterate over the returned elements them to do some specific actions (like verifying if there are other instances at fixed coordinates around them).
What's the best way to achieve that ? Should I use a function or an array or something like that or can I manage to do this with basic conditions ?
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

Post » Wed Feb 18, 2015 7:16 pm

wouldn't using "pick by evaluate" followed by a "for each" work? Or do you mean the issue of comparing two instances of the same type?

For the latter, yes you could use an array. One idea would be to pick by evaluate, loop over the picked sprites and store any values you need. Then you loop over them again, checking with the values saved in the array.

For instance you could do the following to do something for any sprite that has another 32 pixels to it's left.

pick by evaluate
--> set array size to (pickedCount, 3, 1)
-- for each sprite
----> set array(loopindex, 0) to sprite.uid
----> set array(loopindex, 1) to sprite.x
----> set array(loopindex, 2) to sprite.y
-- for each sprite
---- array: for each x
---- array at (array.curX, 1) = sprite.x-32
---- array at (array.curX, 2) = sprite.y
------> do something

If instead all objects are positioned in a grid you can make it much simpler and faster.

Start of layout
--> array: set size to (20,15,1)
-- for each sprite
----> set array(int(sprite.x/32), int(sprite.y/32)) to 1

pick by evaluate
for each sprite
array at (int(sprite.x/32)-1, int(sprite.y/32) = 1
--> do something

You'd just need to update the array if an object is moved.
B
94
S
33
G
118
Posts: 5,395
Reputation: 75,851

Post » Wed Feb 18, 2015 7:22 pm

Thank you for your reply :)

Well, I never tried but I don't know why, I assumed that pick and for each couldn't be used together. And it caused me a lot of issues these past years ! Shame on me :D
B
12
S
7
G
7
Posts: 450
Reputation: 4,276

Post » Sat Mar 07, 2015 9:12 pm

Coin don't be hard on yourself mate everyone learns that way. You not the only one.
B
257
S
43
G
15
Posts: 1,992
Reputation: 41,535


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 1 guest