Nested foreachs over same object type

0 favourites
  • 9 posts
From the Asset Store
Change delay, create new lines, "backspace" the text
  • 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"

  • {

        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

        }

    }

  • 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?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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,

  • 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?

  • 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.

  • 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

  • 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.

  • 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".

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