Nested foreachs over same object type

Get help using Construct 2

Post » Thu Jan 16, 2014 4:58 am

Hi guys, can you tell me how can I do something like this?

foreach(Car as car1)
{
foreach(Car as car2)
{
    if(car1.x -whatever- car2.x)
    {
      //do something
    }
}
}

as far as I know, construct doesn't let you give alias to objects instances, so I really don't know how can I differentiate "car1" from "car2" because they will be just "car"
B
4
Posts: 12
Reputation: 249

Post » Thu Jan 16, 2014 5:31 am

{
    if(car1.x -whatever- car2.x)
    {
      //do something
    }
}

That's all you need.
Unless you are talking about multiple instances of car.
In which case you would use an instance variable to differentiate between the two.
An instance index would be better however.
{
    if(car{1}.x -whatever- car{2}.x)
    {
      //do something
    }
}newt2014-01-16 05:35:36
Image ImageImage
B
172
S
50
G
183
Posts: 8,443
Reputation: 115,605

Post » Thu Jan 16, 2014 6:11 am

didn't get it. let me be more specific. I need to do something like this:

Car On Colission With Car
If car(1) is behind car(2) stop car(1)

how do I reference Car 1?
B
4
Posts: 12
Reputation: 249

Post » Thu Jan 16, 2014 8:05 am

you can do: on collision, set sprite.var to sprite.y

subcondition: sprite pick instance with highest var,

i couldn't make it work with pick by comparison,
ImageImage
B
71
S
22
G
7
Posts: 827
Reputation: 10,177

Post » Thu Jan 16, 2014 8:20 pm

@newt
I have multiple instances of cars. I dont even know how many cars i'll have. I spawn cars every n seconds, and they can be destroyed.

what I want to do is prevent colissions between cars, so if a car is going to collide with another car, I would like it to stop it before they crash.

as a programmer, the solution for that is:
for each car car1
{
for each car car2
{
if car1 "is going to collide with" car2
{
stop car1;
}
}
}


im not asking here how to write a condition to determine if car1 is going to collide with car2.


what im asking is how do I differentiate the cars of each "foreach" loop.

i need to measure the distance beween both cars, for which i need to know the position of both, and write something like

distance(car1.x, car1.y, car2.x, car2.y)

but i just dont have that car1 and car2 references. you said something about an "instance variable"

am i missing something?


B
4
Posts: 12
Reputation: 249

Post » Thu Jan 16, 2014 8:42 pm

You can do this easily with families, but it's harder without. Just store the couple of data points you need in some local variables in your first foreach (include the UID), then pick all again, then pick all BUT the saved UID (to get all other cars) and test against the locals. You may want to add some extra code to prevent testing twice, but this should get you started.blackhornet2014-01-16 20:43:15
ImageImageImage
B
71
S
22
G
259
Posts: 3,794
Reputation: 143,704

Post » Thu Jan 16, 2014 10:05 pm

Another way of doing this "make sure I don't crash" that you may not have considered is just attaching a invisible sprite to the front of each car (pinned in place) and then if something enters this "bufferzone" your cars could take some action...

example capx (very rough but if you are interested and want me to try to clarify anything then leave me a message and I will try to tidy it up tomorrow)


https://dl.dropboxusercontent.com/u/143636437/examples%20for%20web/car%20crash%20instant%20variables.capx
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892

Post » Thu Jan 16, 2014 10:49 pm

I think the dummy detector method would be better.
While the only way to use distance() is with it, any time you add a for each you add to the cpu almost exponentially.
Image ImageImage
B
172
S
50
G
183
Posts: 8,443
Reputation: 115,605

Post » Sun Jan 19, 2014 8:02 am

thanks, i used an insivible sprite pinned to my car.
anyway, I find very dissapointing to not have a trivial way go get references to the objects being iterated in nested for each loops.
construct 2 is missing a way to get references with "alias".
B
4
Posts: 12
Reputation: 249


Return to How do I....?

Who is online

Users browsing this forum: VValt and 21 guests