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

Switch Statement Help


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
andreblue
New Member
Join Date: Sep 2015
Old 09-19-2015 , 21:40   Switch Statement Help
Reply With Quote #1

I am trying to use a switch statement to be able to check which bodypart was hit in csgo.
I tried below but it would return errors. I am new to source pawn/pawn thus am unsure exactly what to do even with the help of google. Can anyone shed some light on what im doing wrong or how to fix it? Thanks

Code:
new String:bodyPart[] = "";
switch(hit_area)
{
    case 1:   bodyPart[] = "Head";
    case 2:   bodyPart[] = "Chest";
    case 3:   bodyPart[] = "Stomach";
    case 4:   bodyPart[] = "Left Arm";
    case 5:   bodyPart[] = "Right Arm";
    case 6:   bodyPart[] = "Left Leg";
    case 7:   bodyPart[] = "Right Leg";
    default:  bodyPart[] = "Unknown";
}
andreblue is offline
Phil25
AlliedModders Donor
Join Date: Feb 2015
Old 09-19-2015 , 22:35   Re: Switch Statement Help
Reply With Quote #2

For declaring a String variable which you're going to assign a value right away you should use decl instead of new. It's better for optimization. Also, you don't have it equal to anything, simply put the buffer size in [].

For switches you need to use {} after the colon otherwise it won't work.

And, for assigning strings, you need to use strcopy, you can't just have it equal to something as simple as that. :p Within the strcopy you need to specify the buffer size, as well.

So it would be something like this:
PHP Code:
decl String:bodyPart[16];
switch(
hit_area)
{
    case 
1:{    strcopy(bodyPartsizeof(bodyPart), "Head");}    //"sizeof(bodyPart)" is 16, ofc
    
case 2:{    strcopy(bodyPartsizeof(bodyPart), "Chest");}
    case 
3:{    strcopy(bodyPartsizeof(bodyPart), "Stomach");}
    case 
4:{    strcopy(bodyPartsizeof(bodyPart), "Left Arm");}
    case 
5:{    strcopy(bodyPartsizeof(bodyPart), "Right Arm");}
    case 
6:{    strcopy(bodyPartsizeof(bodyPart), "Left Leg");}
    case 
7:{    strcopy(bodyPartsizeof(bodyPart), "Right Leg");}
    default:{    
strcopy(bodyPartsizeof(bodyPart), "Unknown");}

Phil25 is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 09-19-2015 , 22:48   Re: Switch Statement Help
Reply With Quote #3

Quote:
Originally Posted by Phil25 View Post
For declaring a String variable which you're going to assign a value right away you should use decl instead of new.
decl does not exist in the new syntax. We are moving away from decl as it is unsafe.

Quote:
And, for assigning strings, you need to use strcopy
No, you don't. The assignment operator should work just fine here.


PHP Code:
char bodyPart[10]; 
switch(
hit_area

    case 
1:{    bodyPart "Head"; }
    case 
2:{    bodyPart "Chest"; }
    case 
3:{    bodyPart "Stomach"; }
    case 
4:{    bodyPart "Left Arm"; }
    case 
5:{    bodyPart "Right Arm"; }
    case 
6:{    bodyPart "Left Leg"; }
    case 
7:{    bodyPart "Right Leg"; }
    default:{    
bodyPart "Unknown"; }

__________________

Last edited by ddhoward; 09-19-2015 at 22:55.
ddhoward is offline
Phil25
AlliedModders Donor
Join Date: Feb 2015
Old 09-19-2015 , 23:05   Re: Switch Statement Help
Reply With Quote #4

Quote:
Originally Posted by ddhoward View Post
No, you don't. The assignment operator should work just fine here.
Having the assignment operator was making one of my old plugins not work. Maybe it can be used now, in the new syntax.
Phil25 is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 09-19-2015 , 23:21   Re: Switch Statement Help
Reply With Quote #5

Quote:
Originally Posted by Phil25 View Post
Having the assignment operator was making one of my old plugins not work. Maybe it can be used now, in the new syntax.
IIRC, it's worked for me since I started writing plugins back in 2013.
__________________
ddhoward 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 11:33.


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