Jump to content
  • Advertisement
Sign in to follow this  
BUnzaga

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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...

any advice on this?

Share this post


Link to post
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'
'torso reload'

and generate:

walking
walking & shooting
walking & reloading
standing
standing & shooting
standing & reloading

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!