How do I do an "if" in a "for each"

Get help using Construct 2

Post » Wed Feb 22, 2017 4:22 am

I want to place some objects randomly, but make sure they dont overlap. I am using families because I also want to set some instance variables at the same time. Say the family of sprites is called "ships"

I can do something like this:

Code: Select all
on start of layout
for each ships                 -> ships -> set X to ...     
                               -> set myinstanceVar to ...


Now I want to do something like "if ships is overlapping ships then start again for this ship" WITHIN the for each ships, but cant figure out how to do this.

I thought of using some nested functions, but you dont seem to be able to pass objects to functions (unless you pass the UID as an integer), which makes it harder.

I saw some function examples with "remember picked object" set somewhere, but could not find this feature. Anyone got a hand example?
B
17
S
6
G
3
Posts: 370
Reputation: 3,942

Post » Wed Feb 22, 2017 4:57 am

I think I have sussed it after reading https://www.scirra.com/tutorials/556/understanding-picking-with-respect-to-families

However, the resultant code is too long to hand type here.

Basically, I have a function "placeShip" with "pick instance with UID Function.Param(0)" after it. Then I call this from the "for each ships" function. Then I have a separate event "ships Is overlapping ships" with a "Pick ships instance 0" after it. I am hoping that this is picking the first overlapping instance from the family, not the first instance in the family (which may not be overlapping).

How do people paste code or screenshots into the forums?
B
17
S
6
G
3
Posts: 370
Reputation: 3,942

Post » Wed Feb 22, 2017 1:27 pm

With a family, it can be done in a few events.

https://www.dropbox.com/s/u23pivjynifv9 ... .capx?dl=0
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 30 guests