0 Favourites

Nested loops about same type of object? [resolved]

  • Hi!

    Let say I have this :

    For each Kangaroo

    (sub event) For each Kangaroo

    How can I make comparisons between the kangaroos from the first loop and those from the second one?

    I'm tyring to do this kind of stuff since yesterday and it makes my brain hurt.

  • Why would you want to do this?

  • You can't compare two instances directly. You can do it with Families. Otherwise you need to store the relevant data to compare in local variables after the first For, do a Pick All (assuming All is the default pick), then the second For, and do your comparisons against the local variables. It takes some work.

    Edit: you should explain what you are trying to do. There may be an easier way.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Thanks for your replies.

    My object is actually not a kangaroo, but a wall.

    Basically, what I'm trying to achieve is :

    "if a wall A has another wall B directly above it, make A red". I use a grid so my walls are well aligned.

  • In that case, you can use a 'For Each ordered' to place all walls in order. The trick is to get the right expression. If you normalize all indexing to your grid, it's easier to conceptualize. Assuming the grid is zero based, and your grid is 32, your expression would be something like: (Wall.X / 32) * 1000 + (Wall.Y / 32). What this does is order by column then row, using a large number (1000) to ensure each column is going to index beyond the height of the grid. Now every wall is in order, you just keep track of if there is a wall, in the same column, before the current wall, and set it to red. Again, use local variables to store the 'previous' walls details.

  • Hi!

    It took me some time to figure out how to do this with your advice (and to understand the "*1000" purpose), but I finally did it.

    previousWall_X = 0

    previousWall_Y = 0

      For each [wall] order by ( wall.X / wall.Width ) * 1000 + ( wall.Y / wall.Height )
        Pick wall where Y = previousWall_Y + wall.Height --> wall set animation to "block" (different hitbox) (no condition) --> system set previousWall_X to wall.X (no condition) --> system set previousWall_Y to wall.Y

    Thank you very much !

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