AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   question about enums and switch (https://forums.alliedmods.net/showthread.php?t=185642)

Alekkkk 05-20-2012 00:15

question about enums and switch
 
Example:
PHP Code:

enum Weapon
{
    
Deagle,
    
AWP,
    
Knife,
    
Grenade,
    
Scout,
    
SG550,
    
G3SG1,
    
AK47,
    
M4A1
}
new 
gWeapon[MAX_PLAYERS 1][Weapon

Can i use switch and cases to retrieve which one is true (let say that i will set one of them to true) , or it can be done only with "if". And if it can is it better than "if"?

hornet 05-20-2012 02:07

Re: question about enums and switch
 
Why don't you test it? And yes switch statements are more efficient than using "if" multiple times.

Liverwiz 05-20-2012 10:35

Re: question about enums and switch
 
Quote:

Originally Posted by hornet (Post 1712640)
Why don't you test it? And yes switch statements are more efficient than using "if" multiple times.

switches are easier to code too. :P

Alekkkk 05-21-2012 13:42

Re: question about enums and switch
 
PHP Code:

enum Weapon
{
    
Deagle,
    
AWP,
    
Knife,
    
Grenade,
    
Scout,
    
SG550,
    
G3SG1,
    
AK47,
    
M4A1
}
new 
gWeapon[MAX_PLAYERS 1][Weapon]

new const 
gNames[Weapon][10] =
{
    
"Deagle",
    
"AWP",
    
"Knife",
    
"Grenade",
    
"Scout",
    
"SG550",
    
"G3SG1",
    
"AK47",
    
"M4A1"
}
new 
Weapon:gName[MAX_PLAYERS 1

Usage:
PHP Code:

gWeapon[id][Deagle] = true 

PHP Code:

if(gWeapon[id][Deagle])
        
gName[id] = Deagle 

PHP Code:

switch(gName[id])
{
     case 
Deagle:
          {
                
// Do something
          
}


Is this right ?

ConnorMcLeod 05-21-2012 14:00

Re: question about enums and switch
 
More correct code would be :

new Weapon :gWeapon[MAX_PLAYERS + 1]

And then :

gWeapon[id] = Deagle

Considering a player can be in only once duel type at the same time.

Alekkkk 05-21-2012 14:14

Re: question about enums and switch
 
fixed
PHP Code:

new Weapon:gName[MAX_PLAYERS 1

this part. (That was my fault)

If i set only gWeapon[id] = Deagle , can i use it in print_chat like that:

PHP Code:

client_print_color(0Red,"%s"gWeapon[gName[id]]) 

Edit:
I can't set gWeapon[id] = Deagle (Error : Array must be indexed)
When i set gName[id] = Deagle , i can use it in chat : client_print_color(0, Red,"%s", gNames[gName[id]])
I think it is solved .


All times are GMT -4. The time now is 00:26.

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