Raised This Month: $ Target: $400
 0% 

Switch statements with strings


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
hornet
AMX Mod X Plugin Approver
Join Date: Mar 2010
Location: Australia
Old 05-05-2010 , 05:51   Switch statements with strings
Reply With Quote #1

Hi guys, I'm having trouble using a switch statement with strings.
I'm trying to get the name of the player's model, and I understand that the model name will return as the name of the model folder / model name without the file extension.
I have these models set as constants set before plugin_init() like so:
PHP Code:
new const modelname_up1="up1";
//etc 
Then this is my code:
PHP Code:
new playerModel[32];
cs_get_user_model(id,playerModel,32);

switch(
playerModel)
        {
            case 
modelname_up1:
            {
                
cs_set_user_model(id,modelname_zp1);
            }
            case 
modelname_up2:
            {
                
cs_set_user_model(id,modelname_zp2);
            }
            case 
modelname_up3:
            {
                
cs_set_user_model(id,modelname_zp3);
            }
            case 
modelname_up4:
            {
                
cs_set_user_model(id,modelname_zp4);
            }
        } 
On the switch statement line I'm getting the error:
Error: Array must be indexed (variable "-unknown-")

And on each of the case lines I'm getting:
Error: Must be a constant expression; assumed zero

Please help me solve this.
Thanks!
hornet is offline
drekes
Veteran Member
Join Date: Jul 2009
Location: Vault 11
Old 05-05-2010 , 06:45   Re: Switch statements with strings
Reply With Quote #2

PHP Code:

switch(playerModel[])
        {
            case 
modelname_up1:
            {
                
cs_set_user_model(id,modelname_zp1);
            }
            case 
modelname_up2:
            {
                
cs_set_user_model(id,modelname_zp2);
            }
            case 
modelname_up3:
            {
                
cs_set_user_model(id,modelname_zp3);
            }
            case 
modelname_up4:
            {
                
cs_set_user_model(id,modelname_zp4);
            }
        } 
try this, and if you still get the constant expression error , do this
PHP Code:
new const modelname_up1[] ="up1";

cs_set_user_model(id,modelname_zp1[]); 
__________________

Quote:
Originally Posted by nikhilgupta345 View Post
You're retarded.
drekes is offline
Send a message via MSN to drekes
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 05-05-2010 , 06:52   Re: Switch statements with strings
Reply With Quote #3

drekes you should test your solutions before you post them...

Strings don't work with switch.

You have to use equal with ifs:
PHP Code:
if(equal(playerModel"something1")){

}else if(
equal(playerModel"something2")){

}else if(
equal(playerModel"something3")){

//..... 
or another option is to convert playerModel into number using some hashing function or storing number in a trie like this:
PHP Code:
#include <amxmodx>
#include <celltrie>
#include <cstrike>

new Trie:g_model_replacement_id
new g_custom_model[][] = {"model1""model2""model3""model4"}
//cells:
// g_custom_model[0] equals "model1"
// g_custom_model[1] equals "model2"
// g_custom_model[2] equals "model3"
// g_custom_model[3] equals "model4"

// you can add more of them...

public plugin_init(){
    
g_model_replacement_id TrieCreate()
    
    
//adding replacements
    
TrieSetCell(g_model_replacement_id"terror"0//sets "model1" as a replacement for "terror"
    
TrieSetCell(g_model_replacement_id"artic"0//sets "model1" as a replacement for "arctic"
    
TrieSetCell(g_model_replacement_id"urban"3//sets "model4" as a replacement for "urban"
    //....
}

public 
change_model(id){
    static 
playerModel[32], model_id
    cs_get_user_model
(idplayerModel31)
    if(!
TrieGetCell(g_model_replacement_idplayerModelmodel_id)){
        return 
//model replacement has not been configured for retrieved playerModel
    
}
    
cs_set_user_model(idg_custom_modelmodel_id ])

    
//with replacements examples set in plugin_init
    //if someone has "terror" or "arctic" then he will get "model1"
    //if someone has "urban" then he will get "model4"
}

public 
plugin_end(){
    
TrieDestroy(g_model_replacement_id)

__________________
Impossible is Nothing

Last edited by Sylwester; 05-05-2010 at 06:57.
Sylwester is offline
hornet
AMX Mod X Plugin Approver
Join Date: Mar 2010
Location: Australia
Old 05-05-2010 , 07:42   Re: Switch statements with strings
Reply With Quote #4

Sorry I left that bit out, I actually do have:
PHP Code:
new const modelname_up1[] ="up1"
Thanks I'll Sylwester's idea.
hornet is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:34.


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