0 Favourites

For loop not behaving as expected

  • I'm having some trouble grasping the for loop in Construct 2. This may be because I am bringing expectations from previous programming experience, but, regardless, I can't wrap my mind around it.

    I'm trying to build a money counting game. Customers will come into the store, buy an item, and the player must count out their change. I'm in the very early stages and trying to write a loop that will run for each customer. The loop seems to run all iterations at once. Are the sub-events of the loop nested inside the loop? Do they run for each iteration of the loop, or am I thinking about this the wrong way?

    As an aside, I have an animation in which the arm comes out onto the counter from off the layout (ultimately to put down that customer's payment). It should stop once it reaches a y value and retreat back off the layout. Instead, it shoots across the layout never to be seen again.

    dropbox.com/s/rafv5qf1q1txkcr/crash_register.capx

    Events screenie: dropbox.com/sh/bqgsor1i6zfg6pz/T_PUIYPWnF/Crash%20Register.png

    Thanks in advance for the help!

  • The loop runs all iterations at once in the same tick, including sub events. Moreover the wait action in a loop operates in parallel, not in a series, as it has no effect on any actions except those that come after it in it's own event (not even those in subsequent sub events are affected). Currently your wait action is only affecting the 3 subsequent actions and since the loop runs all iterations immediately those 3 actions happen all at the same time for all the arms.

    I highly suggest reading through the manual, specifically the section on the event system and how objects are picked. For instance it looks like you're trying to set each arm to a random arm animation but what you're actually doing is setting them all to the same random arm. And you probably want to use instance variables, not global variables, for properties of turning your arms around.

    Also keep in mind that for a lot of this stuff you don't even need the for loop. The event sheet itself is checked every tick and acts as a kind of big loop so even events like "arm.X > globalvariable" will be checked every tick and run if the conditions match, and the picking system will automatically pick only those instances of arm for which the conditions are true (starting with all instances of arm and then eliminating those that do not match the conditions).

  • raddevon - ^^ what Phyvo says.

    i've found that the easiest way to think of it is as filters and (for example) i've learned that "for each" treats each instance as it's own, say for enemies in a shooter game.

    as for debugging, your loops isn't working because it says (event 5) system dayready = 1 and then the action set dayready = 0, the rest of your events are nested inside this "dayready = 1" condition so they don't fire because they aren't true.

    try disabling the dayready = 1 line to see what i mean :)

  • What they said. :)

    I found it really hard coming into C2 with coding background. But when you realize that you don't actually have to think in grassroots level and can focus much more on the designing aspects it'll be sweet, I promise :)

  • OK. This is great feedback so far. It seems the for loop is not a good structure for what I want to do. Does anyone have a suggestion how I could create these sequences of actions then pause for the player to respond? I tried a loop because I wanted to be able to change the number of customers dynamically. I could see how I could hard-code each customer and mark a boolean variable when one finishes then start the next one, but it seems like there must be a better way.

    Another curiosity of mine: are the sub-events of a loop event also looped?

    Thanks again for your help!

  • Can't find forum rules; I'm not sure if bumping is allowed. Still needing some advice, so I'll give it a shot!

  • People do bump their topics, a little bumping is allowed if it doesn't get out of hand. And yes sub-events within loops are also looped. Sorry I'm late in responding myself, I hope you're not still having trouble but just in case you are I'll try to help.

    Now, it is hard for me to give you a suggestion as to how exactly to create your sequence of actions because you haven't been specific about what you want your objects to do. But I'll try to give you an example for something I know you want to do, I hope it will make the event system slightly clearer to you.

    Say you have an arm moving downwards and you want it to stop at a particular location that's constant for every arm. Create an event for arm with the condition "Arm - Compare Y > (some number)" with an action that sets the speed of the arm to 0. Every time the event sheet loops (which is roughly 60 times a second) the event will "pick" every arm that matches your condition (Y > some number) and set the arm's speed to 0. So even if you had 100 arms hurtling downward the event would only stop the ones that matched the condition (Y > some number). If you had *no* condition (that is used System - Every Tick) then your event would have no filter and thus would constantly stop ALL your arms.

    Again I highly recommend reading the manual concerning how the event system and picking objects work here: scirra.com/manual/75/how-events-work

    Looking at some of the examples that came with Construct and fiddling around with them will probably help you understand this better too.

    If you have further questions please ask them!

  • Please, let me ask here a question about the For loop. Why does it run over and over again??

    I'm expecting, that an event "System For "" from 2 to 12" will run exactly 11 times and stop. Unfortunately that loop executes the actions continuously without stopping?!

  • XManBG It does run exactly 11 times... every time you run that event. If you have it run on key press then it will loop 11 times when you press the key. If you have it running every tick then it will loop 11 times every tick.

  • OK. This is great feedback so far. It seems the for loop is not a good structure for what I want to do. Does anyone have a suggestion how I could create these sequences of actions then pause for the player to respond?

    I generally use global variables as flags.

    flag = 0 > whatever action you want and set flag to 1

    flag = 1 > next action

  • Yes, that was my theoretical expectation too!! But... in real C2... I just would like to write out the numbers from 2 to 12 on the screen. Sorry, maybe I don't understand the C2 programming logic. It would be realized that way with a common programming software. Please try this at home. No key press, no touch, no mouse.

    Strange For-Loop Behavior CAPX

    <img src="http://www.colarion.com/construct2-test/strange_for_loop.jpg" border="0">

    ... results in ... <font color="red">Please, note that it starts with 1 and goes to 12!! Furthermore, I'm expecting loopindex to return the numbers 2 to 12.</font>

    <img src="http://www.colarion.com/construct2-test/strange_for_loop_result.jpg" border="0">

    XManBG It does run exactly 11 times... every time you run that event. If you have it run on key press then it will loop 11 times when you press the key. If you have it running every tick then it will loop 11 times every tick.

  • Yes, that was my theoretical expectation too!! But... in real C2... I just would like to write out the numbers from 2 to 12 on the screen. Sorry, maybe I don't understand the C2 programming logic. It would be realized that way with a common programming software. Please try this at home. No key press, no touch, no mouse.

    Place 'trigger once' or other trigger to your event. Otherwise it will run every tick.

  • 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 vee41, with trigger once it works as expected. It shows the numbers from 2 to 12 once on the screen.

    <img src="http://www.colarion.com/construct2-test/strange_for_loop2.jpg" border="0" />

    Anyway I'm really wondering in what situation you could need the For-Loop to runs to eternity and not only once from 2 to 12?! It seams without Trigger once to be unusable?!

  • Thanks vee41, with trigger once it works as expected. It shows the numbers from 2 to 12 once on the screen.

    <img src="http://www.colarion.com/construct2-test/strange_for_loop2.jpg" border="0">

    Anyway I'm really wondering in what situation you could need the For-Loop to runs to eternity and not only once from 2 to 12?! It seams without Trigger once to be unusable?!

    Many situations! :)

    Here is an example project which uses for loop each tick to determine RTS units course of action:

    Sidescrolling RTS example

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
378 33,262
part12studios's avatar
part12studios
Unread hot topic Locked topic
115 17,214
Kyatric's avatar
Kyatric Construct Team Support
Unread sticky
83 102,300
gamearcad's avatar
gamearcad