Raised This Month: $51 Target: $400
 12% 

if to switch


Post New Thread Closed Thread   
 
Thread Tools Display Modes
Author Message
XSlayer
Member
Join Date: Dec 2021
Old 04-05-2022 , 21:53   if to switch
#1

Hi i want to change this strings conditions to switch, how can a i do it?

Code:
new Name[33][32];

public client_connect( Client )
{
       get_user_name( Client, Name[Client], 31 );
 
       set_task( 5.0, "SiHi", Client );
}
public SiHi( Client )
{
      if(equal(Name[Client], "Pedrito" )) // Change ifs to switch
      { 
           client_print( Client, print_center, "Hello Pedrito" );
      } 
      else if( equal(Name[Client], "Alex" ))
      {
          client_print( Client, print_center, "Hello Alex" );
      }
}

Last edited by XSlayer; 04-05-2022 at 21:54.
XSlayer is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-05-2022 , 21:58   Re: if to switch
#2

You cannot use switch() with strings. Since this looks like very simple test code, you should explain what you are actually trying to do and we might be able to find a better way to do it.
__________________
fysiks is offline
XSlayer
Member
Join Date: Dec 2021
Old 04-05-2022 , 22:10   Re: if to switch
#3

Quote:
Originally Posted by fysiks View Post
You cannot use switch() with strings. Since this looks like very simple test code, you should explain what you are actually trying to do and we might be able to find a better way to do it.
nothing different by the way, i have many classes with strings names, so i have many conditions with if not two like the example, i read that using switchs is better than ifs to optimize the plugin just that

Code:
public fxSound2( Client )
{     
          if(!is_user_bot(Client)) 
          {
             if(equal(__str_AMXX[Client],"Freezer"))
             { 
                   emit_sound( Client, CHAN_ITEM, "xs.freezer/prepunch.wav", 1.0, ATTN_NORM, 0, PITCH_NORM ); 
             }
             else if(equal(__str_AMXX[Client],"Vegeta"))
             { 
	           emit_sound( Client, CHAN_ITEM, "xs.vegeta/prepunch.wav", 1.0, ATTN_NORM, 0, PITCH_NORM ); 
             }
             else if(equal(__str_AMXX[Client],"Goku") )
             { 
	           emit_sound( Client, CHAN_ITEM, "xs.goku/prepunch.wav", 1.0, ATTN_NORM, 0, PITCH_NORM ); 
             }
             else if(equal(__str_AMXX[Client],"Trunks") )
             { 
	           emit_sound( Client, CHAN_ITEM, "xs.trunks/prepunch.wav", 1.0, ATTN_NORM, 0, PITCH_NORM ); 
             }
             else if(equal(__str_AMXX[Client],"A18") )
             { 
	           emit_sound( Client, CHAN_ITEM, "xs.androide18/prepunch.wav", 1.0, ATTN_NORM, 0, PITCH_NORM ); 
             }
             else if(equal(__str_AMXX[Client],"Gohan") )
             { 
	           emit_sound( Client, CHAN_ITEM, "xs.gohan/prepunch.wav", 1.0, ATTN_NORM, 0, PITCH_NORM ); 
             }
             else if(equal(__str_AMXX[Client],"Bardock") )
             { 
	           emit_sound( Client, CHAN_ITEM, "xs.bardock/H3.wav", 1.0, ATTN_NORM, 0, PITCH_NORM ); 
             }
          }
}

Last edited by XSlayer; 04-05-2022 at 22:11.
XSlayer is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 04-05-2022 , 22:20   Re: if to switch
#4

PHP Code:
switch(Name[Client][0])
{
    case 
'P'client_printClientprint_center"Hello Pedrito" );
    case 
'A'client_printClientprint_center"Hello Alex" );

PHP Code:
new const fxSounds[][] = 
{
    
"Freezer",
    
"Vegeta",
    
"Goku"///so on..
}

new const 
fxSoundsPath[][] = 
{
    
"xs.freezer/prepunch.wav",
    
"xs.vegeta/prepunch.wav",
    
"xs.goku/prepunch.wav"//so on..
}

public 
fxSound2Client )
{     
    for(new 
isizeof(fxSounds); i++)
    {
        if(
equali(__str_AMXX[CLIENT], fxSounds[i]))
        {
            
emit_soundClientCHAN_ITEMfxSoundsPath[i], 1.0ATTN_NORM0PITCH_NORM ); 
        }
    }


Last edited by lexzor; 04-05-2022 at 22:24.
lexzor is offline
XSlayer
Member
Join Date: Dec 2021
Old 04-05-2022 , 22:22   Re: if to switch
#5

Quote:
Originally Posted by lexzor View Post
PHP Code:
switch(Name[Client][0])
{
    case 
'P'client_printClientprint_center"Hello Pedrito" );
    case 
'A'client_printClientprint_center"Hello Alex" );

mmm i think thats not exactly at all, cause if i have two names with the "P" at first letter like Pedrito and Pablo, it will cause a trouble i think, like "Goku" and "Gohan"
XSlayer is offline
lexzor
Veteran Member
Join Date: Nov 2020
Old 04-05-2022 , 22:26   Re: if to switch
#6

then you can do this

PHP Code:
client_printClientprint_center"Hello %s"Name[Client]); 
i just wanted to show that s the only way to use strings in switch
lexzor is offline
XSlayer
Member
Join Date: Dec 2021
Old 04-05-2022 , 22:29   Re: if to switch
#7

Quote:
Originally Posted by lexzor View Post
PHP Code:
switch(Name[Client][0])
{
    case 
'P'client_printClientprint_center"Hello Pedrito" );
    case 
'A'client_printClientprint_center"Hello Alex" );

PHP Code:
new const fxSounds[][] = 
{
    
"Freezer",
    
"Vegeta",
    
"Goku"///so on..
}

new const 
fxSoundsPath[][] = 
{
    
"xs.freezer/prepunch.wav",
    
"xs.vegeta/prepunch.wav",
    
"xs.goku/prepunch.wav"//so on..
}

public 
fxSound2Client )
{     
    for(new 
isizeof(fxSounds); i++)
    {
        if(
equali(__str_AMXX[CLIENT], fxSounds[i]))
        {
            
emit_soundClientCHAN_ITEMfxSoundsPath[i], 1.0ATTN_NORM0PITCH_NORM ); 
        }
    }


Thanks the second is what that i wanted, thanks!
edit:i still dont know if what of all the options is better to optimize, the first one that i posted, yours second code, or using a switch, if somone knows pls let me know thanks!

Last edited by XSlayer; 04-05-2022 at 22:30.
XSlayer is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-05-2022 , 22:42   Re: if to switch
#8

Quote:
Originally Posted by XSlayer View Post
nothing different by the way, i have many classes with strings names, so i have many conditions with if not two like the example, i read that using switchs is better than ifs to optimize the plugin just that

Code:
public fxSound2( Client )
{     
          if(!is_user_bot(Client)) 
          {
             if(equal(__str_AMXX[Client],"Freezer"))
             { 
                   emit_sound( Client, CHAN_ITEM, "xs.freezer/prepunch.wav", 1.0, ATTN_NORM, 0, PITCH_NORM ); 
             }
             else if(equal(__str_AMXX[Client],"Vegeta"))
             { 
	           emit_sound( Client, CHAN_ITEM, "xs.vegeta/prepunch.wav", 1.0, ATTN_NORM, 0, PITCH_NORM ); 
             }
             else if(equal(__str_AMXX[Client],"Goku") )
 ...
switch() is more efficient than a bunch of if else if statements for integers. If these string that you're checking for are hard coded in the plugin, you should use an enumeration for them. When you do this, you can get rid of the if else if statements and entirely skip the switch() statement entirely and just use the enumeration variable to grab the sound file string.

Something like this:

PHP Code:
enum CLASSES {
    
FREEZER,
    
VEGETA,
    
GOKU
}

new 
CLASSES:gPlayerClass[33]
new 
gSounds[CLASSES][] = {
    
"xs.freezer/prepunch.wav",
    
"xs.vegeta/prepunch.wav",
    
"xs.goku/prepunch.wav"
}

public 
setPlayerClass(id)
{
    
gPlayerClass[id] = FREEZER;
}

public 
fxSound2(id)
{
    if( !
is_user_bot(id) )
    {
        
emit_sound(idCHAN_ITEMgSounds[gPlayerClass[id]], 1.0ATTN_NORM0PITCH_NORM);
    }

And even if they are not hard coded, something similar can easily be done.

Quote:
Originally Posted by lexzor View Post
PHP Code:
switch(Name[Client][0])
{
    case 
'P'client_printClientprint_center"Hello Pedrito" );
    case 
'A'client_printClientprint_center"Hello Alex" );

Absolutely not, completely unreliable.
__________________

Last edited by fysiks; 04-07-2022 at 22:52. Reason: fix minor typo
fysiks is offline
XSlayer
Member
Join Date: Dec 2021
Old 04-05-2022 , 22:56   Re: if to switch
#9

Quote:
Originally Posted by fysiks View Post
switch() is more efficient than a bunch of if else if statements for integers. If these string that you're checking for are hard coded in the plugin, you should use an enumeration for them. When you do this, you can get rid of the if else if statements and entirely skip the switch() statement entirely and just use the enumeration variable to grab the sound file string.

Something like this:

PHP Code:
enum CLASSES {
    
FREEZER,
    
VEGETA,
    
GOKU
}

new 
CLASSES:gPlayerClass[33]
new 
gSounds[CLASSES][] = {
    
"xs.freezer/prepunch.wav",
    
"xs.vegeta/prepunch.wav",
    
"xs.goku/prepunch.wav"
}

public 
setPlayerClass(id)
{
    
gPlayerClass[id] = FREEZER;
}

public 
fxSound2(id)
{
    if( !
is_user_bot(id) )
    {
        
emit_sound(idCHAN_ITEMgSounds[gPlayerClass[0]], 1.0ATTN_NORM0PITCH_NORM);
    }

And even if they are not hard coded, something similar can easily be done.



Absolutely not, completely unreliable.

Thanks i understand it a little, a have a few doubts..

PHP Code:
public setPlayerClass(id)
{
    
gPlayerClass[id] = FREEZER;

which does this exactly, because I am getting the string or the class name like this:

PHP Code:
public @ClassChangeClientPreClassPostClass )
{
          
getClassDATAv(getClientCLASS(Client), cxName__str_AMXX[Client], charsmax__str_AMXX[] ));

maybe you won't recognize the stock, and why are u setting the playerclass on freezer, and is a public where is not called in ur coded, the @ClassChange is a forward, that is called when a client change a class, so every time u change the class, the strings for the client change.

PHP Code:
public fxSound2(id)
{
    if( !
is_user_bot(id) )
    {
        
emit_sound(idCHAN_ITEMgSounds[gPlayerClass[0]], 1.0ATTN_NORM0PITCH_NORM);
    }

and finally why the size in gPlayerrClass is 0 ?

Last edited by XSlayer; 04-05-2022 at 22:57.
XSlayer is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-05-2022 , 23:44   Re: if to switch
#10

Quote:
Originally Posted by XSlayer View Post
Thanks i understand it a little, a have a few doubts..

PHP Code:
public setPlayerClass(id)
{
    
gPlayerClass[id] = FREEZER;

which does this exactly, because I am getting the string or the class name like this:

PHP Code:
public @ClassChangeClientPreClassPostClass )
{
          
getClassDATAv(getClientCLASS(Client), cxName__str_AMXX[Client], charsmax__str_AMXX[] ));

maybe you won't recognize the stock, and why are u setting the playerclass on freezer, and is a public where is not called in ur coded, the @ClassChange is a forward, that is called when a client change a class, so every time u change the class, the strings for the client change.

PHP Code:
public fxSound2(id)
{
    if( !
is_user_bot(id) )
    {
        
emit_sound(idCHAN_ITEMgSounds[gPlayerClass[0]], 1.0ATTN_NORM0PITCH_NORM);
    }

and finally why the size in gPlayerrClass is 0 ?
I was just providing example code, you'd need to take the example and make it work for your use case. This is why I originally asked about your actual goal.

The size of gPlayerClass is not 0, it is 33. When you're using an array, you need to index it (by using the square brackets). I used a 0 just to make sure I could compile it and forgot to change it to "id" when I added more code around it. So, to fix this part, you'd need to change the 0 to id like this: gSounds[gPlayerClass[id]].

To be able to use the enum (or similar) method, you'd need to change all the places that where you need to know the class type to use the enum. When you actually do need the text, you'd have an array similar to gSounds to store the string name. This is more efficient than passing around strings for knowing the player's class.

Since you seem to be very new to scripting, it might be difficult to convert an existing plugin from using a class string to using a class enum without significant help. You're welcome to attach the .sma file in its entirety so that we can see how difficult it may be.
__________________
fysiks is offline
Closed Thread



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 05:42.


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