0 Favourites

How do I create a nested loop with condition?

  • Hi!

    I really want to get this work now. It's a nested loop seeking an empty slot and putting the item in the slot if it's true. It's useful when a player takes some items.

    The problem is that the first loop moves too fast and it results in that the first loop getting maxvalue and the second loop getting minvalue instead.

  • spokplumpen : I am not getting, what you do there exactly. Maybe it would be helpful to see the whole inventory mechanics? (I have to admit, I work not too much for loops, I don't see the point in them too much.)

  • Hi, i don't think that I should send my whole capx. Maybe only part of the capx but I'm too tired and going to bed now. I will do it tomorrow instead.

    And it works better without else now. I think that I have to create local variables first istead of the loopindex. The X-loopindex disappears after the Y-for starts. Wrong x-loopindex but right y-loopindex.

    Thank you for your quick response!

  • Solved! The y-loop have to be first in the event. The X-loop second. It's weird!

  • It might work better (next time maybe) to put the second loop as a sub event. Traditionally, a nested loop goes inside the first loop. What you have here are both loops running at the same time with the way Construct works.

    Essentially:

    Event: For X 1 to 10 : action

    Sub Event: For y 1 to 10: action (Set position to tile map x,y)

    Sorry, I'm at work and can't access C2 right now. If you need a better example then let me know. I can try and make one tonight or Wednesday.

  • The two for loops at the same level of condition work perfectly in some places, mepis. For example, Zatyka's calculating-rectangular-coverage-area .capx uses X and Y for loops in the same condition.

  • located at tutorials/ 1339/ calculating-rectangular-coverage-area

  • I'm pretty sure you would just be getting lucky and that the loops are processing so fast with such small indexes that they are eventually matching up. With the way C2 works, putting two for loops in the same condition run both loops at the same time.

    If you want to be exact and loop through, say every part of a two dimensional array, you would want to use a true nested loop.

    Ashley can shine some better light on this, I'm sure.

  • you could do it like this as well

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • you could do it like this as well

  • Loops do not process "too fast" or simultaneously, they have a specific and well-defined order of execution.

    I'm not sure what you're even trying to do with that loop - it looks like it picks an object based on an overlap condition, then it (pointlessly?) moves it in a loop and will end up leaving it in the last position.

  • Ashley I assumed (wrongly it seems) that if both loops are put in the same condition, Construct would process one loop first, complete it, and then move to the second loop (or process both in separate threads which you just debunked.)

    If both loops are defined in a single conditional statement on Construct, how are they formed code wise and executed when the application is exported? I attempted to play with the debugger to see how it would work but either the loop only processed once or processed so fast that the variable assigned to the loop index stayed static at the top side of the loop.

  • It nests the loops. Two loops in one event is the same as having one loop and then another loop in a subevent under that.

  • Loops do not process "too fast" or simultaneously, they have a specific and well-defined order of execution.

    I'm not sure what you're even trying to do with that loop - it looks like it picks an object based on an overlap condition, then it (pointlessly?) moves it in a loop and will end up leaving it in the last position.

    Have you checked my last picture? it's about a family of object(items) which is from the shop for example. When a player buy a item from a seller in the store, the algorithms start and search a empty slot in the player's bag with some items which are busy in their places. The algorithms search by testing a family overlapping another family. If false = the loop stays and results in right coordinations.

    it's worth to mention that these families 1 and 2 have same objects because the first family cannot overlap same family.

    And i don't think that a nested loop with condition works in subevent. I have tried it. No result.

  • spokplumpen question here is who is right Person created C2 or you?

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