AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Is there a faster way to do this? (https://forums.alliedmods.net/showthread.php?t=11551)

lunarwolfx 03-22-2005 20:38

Is there a faster way to do this?
 
I'm was thinking the other day about changing speeds based on levels, and I came up with this.
Code:


if(level[id] == 1) {

set_user_maxspeed(id,320.0)
}
else if(level[id] == 2) {

set_user_maxspeed(id,321.0)

}

etc....

I was just wondering if there is a faster way then making a mess of ifs and else ifs.

v3x 03-22-2005 20:45

Code:
// ..     switch(level[id]) {         case 1: set_user_maxspeed(id,320.0)         case 2: set_user_maxspeed(id,321.0)         case 3: set_user_maxspeed(id,321.0)         // etc..     }
Should work :).

|2eM!x 03-22-2005 20:46

ooh i like that idea! Is that your whole code?

XxAvalanchexX 03-22-2005 22:47

Depending on the max levels, that could take forever, and it would use up a huge amount of space in the plugin. Just use a simple formula for it.

Code:
new Float:maxspeed = 319.0 + float(level); set_user_maxspeed(id,maxspeed);

lunarwolfx 03-22-2005 23:30

/me slaps forehead

so simple! Yet I couldn't figure it out.

Wow, you totally opened up a new line of thinking for me. Thanks again.

Twilight Suzuka 03-23-2005 09:45

Quote:

Originally Posted by XxAvalanchexX
Depending on the max levels, that could take forever, and it would use up a huge amount of space in the plugin. Just use a simple formula for it.

Code:
new Float:maxspeed = 319.0 + float(level); set_user_maxspeed(id,maxspeed);

Yep. Algorithims are the best way to get things done.

dwelty 03-23-2005 11:17

Quote:

Originally Posted by v3x
Code:
// ..     switch(level[id]) {         case 1: set_user_maxspeed(id,320.0)         case 2: set_user_maxspeed(id,321.0)         case 3: set_user_maxspeed(id,321.0)         // etc..     }
Should work :).


I see a lot of this. Common mistake. You forgot your breaks. Case execution falls through. it should look like this:

Code:
// ..   switch(level[id])   {     case 1:     {       set_user_maxspeed(id,320.0);       break;     }     case 2:     {       set_user_maxspeed(id,321.0);       break;     }     case 3:     {       set_user_maxspeed(id,322.0);       break;     }     // etc..   }


XxAvalanchexX had the right idea though. That's your best bet.

twistedeuphoria 03-23-2005 11:20

Small doesn't require breaks :wink:

dwelty 03-23-2005 11:23

Oh. :oops:
Thanks for letting me know.

Twilight Suzuka 03-23-2005 11:24

SMALL breaks when it reaches a case that matches. No fall through.


All times are GMT -4. The time now is 09:55.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.