For loop not behaving as expected

Get help using Construct 2

Post » Thu Jan 31, 2013 4:48 pm

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.

https://www.dropbox.com/s/rafv5qf1q1txkcr/crash_register.capx
Events screenie: https://www.dropbox.com/sh/bqgsor1i6zfg6pz/T_PUIYPWnF/Crash%20Register.png

Thanks in advance for the help!raddevon2013-01-31 16:49:47
B
4
Posts: 6
Reputation: 312

Post » Thu Jan 31, 2013 5:30 pm

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).Phyvo2013-01-31 17:32:26
B
6
S
1
Posts: 40
Reputation: 917

Post » Thu Jan 31, 2013 6:59 pm

@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 :)
B
14
S
6
G
2
Posts: 136
Reputation: 3,210

Post » Thu Jan 31, 2013 7:11 pm

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 :)
B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Post » Thu Jan 31, 2013 10:03 pm

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!
B
4
Posts: 6
Reputation: 312

Post » Sun Feb 03, 2013 3:31 am

Can't find forum rules; I'm not sure if bumping is allowed. Still needing some advice, so I'll give it a shot!
B
4
Posts: 6
Reputation: 312

Post » Sat Feb 09, 2013 12:39 am

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: https://www.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!
B
6
S
1
Posts: 40
Reputation: 917

Post » Thu Mar 14, 2013 10:02 pm

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?!
B
11
S
3
G
3
Posts: 79
Reputation: 3,343

Post » Thu Mar 14, 2013 10:08 pm

@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.
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Post » Fri Mar 15, 2013 4:00 am

[QUOTE=raddevon] 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?[/QUOTE]


I generally use global variables as flags.

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

gazoogle2013-03-15 04:03:11
B
34
S
4
G
8
Posts: 67
Reputation: 5,857

Next

Return to How do I....?

Who is online

Users browsing this forum: MarkThomas, theosciandra, Yahoo [Bot] and 16 guests