Public Group

# How to use LESS THAN, GREATER THAN, NOT EQUAL, OR, etc in switches???

This topic is 4339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So Ive been using switched a while, via javascript in the DX Studio IDE, and I ran into a situation where I'm not sure what the correct syntax is. I want to turn this into a switch... if(nodeDistance>=10) { nodeTable(nodeNumber[currentNode],nodeNumber[nextNode])=10 } if(nodeDistance>10 && nodeDistance<=15) { nodeTable(nodeNumber[currentNode],nodeNumber[nextNode]=15) } if(nodeDistance>15) { nodeTable(nodeNumber[currentNode],nodeNumber[nextNode]=999999) } something like... switch(nodeDistance) { case >=10: break; case >10 && <=15: break; case >15: break; } I guess I can just run through the algorithm, but I like to use cases as much as I can.

##### Share on other sites
You can't. A switch is basically a 'jump table'. You can do other neat things, though, like:

switch (state)
{
case IDLE:
case RUNNING:
// this part will be executed when idle or running
case WALKING:
// this part will be executed when idle, running or walking
break;

case CLIMBING:
// this part will be executed when climbing
break;

default:
// this part will be executed in all other cases
break;
}

...so 'switch/case' and 'if/else if' are not the same thing; each have their uses and it's up to you to use them when appropriate.

Also note that in long 'if / else if' sequences, a lot of expressions have to be evaluated for the last 'case':

if (a == b)
{
// when executed, your program evaluated 1 expression
}
else if (c == d)
{
// when executed, your program evaluated 2 expressions
}
else if (e == f)
{
// when executed, your program evaluated 3 expressions
}
else
{
// when executed, your program evaluated 3 expressions
}

So it's wiser to use a switch statement when possible, which usually does:

jump_table[state]();

##### Share on other sites
Yea I already use it for ai, and other stuff, I just have never tried to use it for iteration statements, etc and couldn't find anything about it on google, so thought Id ask here. Thanks a lot for the fast answer!

I guess I could always do the compairison then jump into the switch via a method...

if(distance>10)
{
assignWeights("close")
}

function assignWeights(distanceInfo)
{
switch(distanceInfo)
{
case "close":
break;
}
}

I guess thats pointless though...

##### Share on other sites
Well, if nodeDistance is an integer, you can also do something like:

switch (nodeDistance)
{
case 10: case 11: case 12: case 13: case 14: case 15:
// >= 10 && <= 15
break;

default:
if (nodeDistance > 15)
{
// > 15
}
break;
}

But I would only do that if I'd have very good reasons to do so ;)

##### Share on other sites
Yea I have run into that when deciding which animation to play...

if(!movingForward)
{
if(!straffing)
{
if(!turning)
{
//play idle animation (takes last priority)
}
else
{
//play turning animation (takes third priority)
}
}
else
{
//play straffing animation (takes second priority)
}
}
else
{
//play running forward animation(takes first priority)
}

Anyway, I'll move that into a switch statement once i get more animations made, but for now I only have idle, walk, turn, straff... so one will take priority over the other depending on the keys down.

Eventually i'll have straff_right, straff_left, walk_straff_right, walk_straff_left, etc etc.

Is this the best way to do it? regarding the animations?

I'll need a crap load of animations for movement...

idle
standing && turning right
standing && turning left
walk forward
walk backward
run forward
run backward
straff right
straff left
turn right
turn left
walk forward && straff right
walk forward && straff left
walk forward && turn right
walk forward && turn left
walk backward && straff right
walk backward && straff left
walk backward && turn right
walk backward && turn left

The list goes on and on, for jumping, etc...

##### Share on other sites
Try to find a way to blend/combine your animations. This way you can have for instance:

'legs walk'
'legs standing'
'torso idle'
'torso shoot'

and generate:

walking
walking & shooting
standing
standing & shooting

##### Share on other sites
What's wrong with the ifs? Is this premature optimisation or do you just like switches or what? I don't get your second example, and you can't switch on a string.

It might be possible to get rid of the ifs entirely.
int values[] = {10, 15, 999999}; // Please name it better than I have.nodeTable(nodeNumber[currentNode],nodeNumber[nextNode]) = values[clamp((nodeDistance - 6) / 5, 0, 2)];

Notice it's now much harder to tell what that code's doing. It will give 10 for values lower than 10, so beware (I didn't know what you wanted in that case anyway). You can do a clamp like this by the way: max(min(value, maxValue), minValue); But really, the moral of the story is don't bother.

EDIT: If your character is symmetrical you can pretty much immediately drop half the animations. Play walk forward backwards to walk backwards (if feasible), and see what other stuff you can cleverly reuse.

1. 1
2. 2
Rutin
18
3. 3
khawk
15
4. 4
A4L
14
5. 5

• 9
• 13
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013665
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!