Loops with variables as names

For questions about using Classic.

Post » Fri Nov 07, 2008 12:05 pm

I have an event looking like this:



When i press space, shouldn't the sprite be destroyed? How is this working?
B
8
S
2
G
5
Posts: 744
Reputation: 3,288

Post » Fri Nov 07, 2008 12:18 pm

Why did you name loop by "Sprite.Value('whut')"? It wont affect anything but name of the loop.

Edit: just add condition for "On loop" with "Sprite.Value('whut')=1 or something like that and it will work.
B
4
S
2
G
4
Posts: 719
Reputation: 2,938

Post » Fri Nov 07, 2008 12:47 pm

"For" loops are supposed to get STRING values for their names. If your variable is NUMERIC, you should do str( Sprite('whut') ) instead of Sprite('whut').
Anyways that quite surprising idea to post loop with name depending on (changable!) private variable ^^.
B
6
S
3
G
6
Posts: 219
Reputation: 3,013

Post » Fri Nov 07, 2008 2:10 pm

The 'On loop' condition only works with a constant (ie. typed in) loop name. I thought I put in a warning in case the expression wasn't constant, but I guess somehow it got turned off. I could have allowed variable names, but it would reduce the algorithmic efficiency considerably. If you can't assume that every string is going to stay the same, every time you run 'On Loop', you have to check all other unrelated 'On loop' conditions to see if their name matches the loop being run, every iteration. This means if you have On loop "a", simply adding some events for loop "b" slows down loop "a"! However, in Construct, forcing a constant string name for the loop allows for an optimisation I've coded that keeps that painful inefficiency from occurring.

Still, the 'start loop' loops are much, much slower than the condition loops (eg. 'For'). I wouldn't recommend you use them at all. They spread out the code to unrelated places in the event list as well. Condition loops keep everything tidy, and are much faster. The CPU cycle overhead per-iteration for the 'Repeat' system condition was about 9 cycles last time I measured it; it'll be hundreds of cycles for anything using 'start loop'.

As for the condition loops, you should be able to get away with using a variable for the name, but I wouldn't recommend it. It'll only work so long as the variable stays the same for the duration of the loop. I can't see why it's necessary to ever do that though.
Scirra Founder
B
359
S
214
G
72
Posts: 22,949
Reputation: 178,544

Post » Fri Nov 07, 2008 3:57 pm

Right now i have a good reason for needing this in my engine, but that might be cause i don't know how to use the "for" loop. Is it documented somewhere, or can someone explain its use to me? I didn't understand much from the wiki.
B
8
S
2
G
5
Posts: 744
Reputation: 3,288

Post » Fri Nov 07, 2008 10:46 pm

It repeats all its actions and subevents with a named loop and a range of loop indices. For example,

+ Start of layout
-> Start loop "blah" 100 times

+ On loop "blah"
-> Actions to do in the loop

is identical to:

+ For "blah" from 1 to 100
-> Actions to do in the loop

Faster, fewer events, more compact, still dead easy :) If you don't need the loop index you can just use 'Repeat':

+ Repeat 100 times
-> Actions to do in the loop
Scirra Founder
B
359
S
214
G
72
Posts: 22,949
Reputation: 178,544


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 5 guests