Efficient way of checking multiple values?

For questions about using Classic.

Post » Thu Nov 05, 2009 1:59 am

What is the best way to check multiple values against a variable? For example, if I have a private variable "state" and in my event sheet I have:


if state = 0
- do these events
if state = 1
- do these events
if state = 2
- do these events

...etc, for maybe 30 checks on the value of "state". This seems inefficient because if state=0, I don't need it to also check if it equals 1 through 30. I believe the switch statement from Gamemaker is similar to what I'm looking for. Is there anything like that or a better way of doing these checks?

Thanks!
B
77
S
49
G
24
Posts: 346
Reputation: 16,909

Post » Thu Nov 05, 2009 2:34 am

You could use the function object, set up a bunch of functions which are named using numbers between 1 and 30, then call a function using the private variable as the name. That should work as a reasonable work around, although I don't know if it would be more efficient.

A switch function would be pretty sweet for Construct 2.
Moderator
B
97
S
36
G
19
Posts: 1,584
Reputation: 17,692

Post » Thu Nov 05, 2009 5:54 am

[quote="UberLou":1ev38f9e]What is the best way to check multiple values against a variable? For example, if I have a private variable "state" and in my event sheet I have:

if state = 0
- do these events
if state = 1
- do these events
if state = 2
- do these events

...etc, for maybe 30 checks on the value of "state". This seems inefficient because if state=0, I don't need it to also check if it equals 1 through 30. I believe the switch statement from Gamemaker is similar to what I'm looking for. Is there anything like that or a better way of doing these checks?

Thanks![/quote:1ev38f9e]

I think this is the best way to do It. Just set state to random.
[code:1ev38f9e]
if state = random (0,2)
if state = 0
- do these events
if state = 1
- do these events
if state = 2
- do these events[/code:1ev38f9e]

This way it will switch the value.
Hope this help you.
B
21
S
4
G
4
Posts: 280
Reputation: 2,934

Post » Thu Nov 05, 2009 9:07 am

Toralord is right, you can just set a number then use subevents to check that number, like a series of Ifs following on each other.

Just make the events in the easiest way possible and don't worry about inefficiency - events add so much overhead that it's kind of ridiculous to worry about efficiency. You could set up an if-else-if-else-if event chain, but paradoxically, that might be less efficient, because the 'else' event does some special processing.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Thu Nov 05, 2009 4:15 pm

Thanks for the replies,

linkman: Thats an interesting workaround. I agree though, not sure if that's more efficient and I think the way I'm coding things, using functions would make it a little messier. Thanks for the suggestion though.

toralord: Does that work? If the value of state=0 you would still need to check if state=1 through 30 or however many values of state there are. Or am I not understanding how that works? I'm not concerned with switching the value of state, I'm concerned with checking the value of state 30 times rather than just once.

Ashley: I guess I've found that when you do things efficiently, it has the side effect of making code more organized (though since I'm not really a coder that could be wrong). I also like to keep thinking in terms of optimization in case I switch to another engine that doesn't handle code so well.
B
77
S
49
G
24
Posts: 346
Reputation: 16,909

Post » Thu Nov 05, 2009 5:38 pm

Yes I will work.
It will do it randomly just like the game maker switch statement you linked to.
Thing about this cap and the game maker switch statement is that the low the range of values to check the more likely is going repeat it self but it will eventually check the all values.
This kind of code is good for dialogues( character saying more then one thing.), guessing games, casino games, puzzles game, but mostly dialogues.
I made a cap to show you an example.
http://download71.mediafire.com/m2faimr ... witchs.cap
B
21
S
4
G
4
Posts: 280
Reputation: 2,934

Post » Thu Nov 05, 2009 7:10 pm

I see what you're saying and thanks for the cap. I probably didn't explain correctly so ill try and make it more clear. I want to check "state" only once per frame and not 30 times per frame if there are 30 state checks.
So in your example, when state is found to equal 0 it runs those state=0 events. But then it also goes through and checks if it equals 1,2,3,4,5 etc. I want it to skip checking if state=1, if state=2, if state=3, etc. because state is already known to equal 0. I'm pretty sure thats how the switch statement works in gamemaker.
B
77
S
49
G
24
Posts: 346
Reputation: 16,909

Post » Fri Nov 06, 2009 12:54 am

It doesn't really matter if you check all 30 variables, because the performance impact is ultra small. Just one collision check is probably hundreds of times more intensive than a simple variable check.

[quote:s8pz6ncv] events add so much overhead that it's kind of ridiculous to worry about efficiency.[/quote:s8pz6ncv]
Did you mean so little overhead? Or are you comparing it to real code?
B
25
S
3
G
6
Posts: 1,197
Reputation: 5,620

Post » Fri Nov 06, 2009 1:25 am

You could also run a loop.

For 1-30
if state=loopindex
- do events
- end loop

That way it will stop when the event runs.
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Fri Nov 06, 2009 12:45 pm

You should use this.
[code:1fxbs5bn]Every tick
if state = 0
- do this event[/code:1fxbs5bn]
This way it will check it once per frame.
That should do it.
B
21
S
4
G
4
Posts: 280
Reputation: 2,934

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 2 guests