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

[HELP] Convert if to switch in this code


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
CrAzY MaN
Senior Member
Join Date: Mar 2017
Location: India
Old 03-18-2018 , 02:06   [HELP] Convert if to switch in this code
Reply With Quote #1

How do i convert if to switch statement?

PHP Code:
public set_ranks(id)
{
    if(
level[id] <= 2
        
rank[id] = RANKS_Noobest
    
if(level[id] >= 2
        
rank[id] = RANKS_Noob
    
if(level[id] >= 5
        
rank[id] = RANKS_Newbiee
    
if(level[id] >= 8
        
rank[id] = RANKS_Easy
    
if(level[id] >= 11
        
rank[id] = RANKS_Normal
    
if(level[id] >= 14
        
rank[id] = RANKS_Hard
    
if(level[id] >= 17
        
rank[id] = RANKS_Expert
    
if(level[id] >= 20
        
rank[id] = RANKS_SuperExpert
    
if(level[id] >= 23
        
rank[id] = RANKS_Specialist
    
if(level[id] >= 26
        
rank[id] = RANKS_Leader
    
if(level[id] >= 29
        
rank[id] = RANKS_Mayor
    
if(level[id] >= 32
        
rank[id] = RANKS_Pro
    
if(level[id] >= 33
        
rank[id] = RANKS_SuperPro
    
if(level[id] >= 36
        
rank[id] = RANKS_Heroic
    
if(level[id] >= 43
        
rank[id] = RANKS_God

__________________
CrAzY MaN is offline
CookieCrumbler
Senior Member
Join Date: Feb 2013
Location: Australia
Old 03-18-2018 , 04:25   Re: [HELP] Convert if to switch in this code
Reply With Quote #2

https://wiki.alliedmods.net/Pawn_Tutorial#Conditionals
__________________
--------------------------------------------------
C is for cookie ... thats good enuff 4 me
CookieCrumbler is offline
Relaxing
AlliedModders Donor
Join Date: Jun 2016
Location: White Plains
Old 03-18-2018 , 04:47   Re: [HELP] Convert if to switch in this code
Reply With Quote #3

I'm astonished because of your take over of making a xp(stuffnthings) plugin without knowing the usage of switch. It also isn't used only on Pawn, but mostly on all programming languages.
__________________
Relaxing is offline
CrAzY MaN
Senior Member
Join Date: Mar 2017
Location: India
Old 03-18-2018 , 04:54   Re: [HELP] Convert if to switch in this code
Reply With Quote #4

You mean this?
PHP Code:
public set_ranks(id)
{
    switch (
level[id])
    {
        case <=
:
        {
            
rank[id] = RANKS_Noobest;
        }
        case >=
:
        {
            
rank[id] = RANKS_Noob;
        }
        case >=
:
        {
            
rank[id] = RANKS_Newbiee;
        }
        case >=
:
        {
            
rank[id] = RANKS_Easy;
        }
        case >=
11 :
        {
            
rank[id] = RANKS_Normal;
        }
        case >=
14 :
        {
            
rank[id] = RANKS_Hard;
        }
        case >=
17 :
        {
            
rank[id] = RANKS_Expert;
        }
        case >=
20 :
        {
            
rank[id] = RANKS_SuperExpert;
        }
        case >= 
23 :
        {
            
rank[id] = RANKS_Specialist;
        }
        case >=
26 :
        {
            
rank[id] = RANKS_Leader;
        }
        case >=
29 :
        {
            
rank[id] = RANKS_Mayor;
        }
        case >=
32 :
        {
            
rank[id] = RANKS_Pro;
        }
        case >=
33 :
        {
            
rank[id] = RANKS_SuperPro;
        }
        case >=
36 :
        {
            
rank[id] = RANKS_Heroic;
        }
        case >=
43 :
        {
            
rank[id] = RANKS_God;
        }
    }

__________________
CrAzY MaN is offline
Relaxing
AlliedModders Donor
Join Date: Jun 2016
Location: White Plains
Old 03-18-2018 , 06:16   Re: [HELP] Convert if to switch in this code
Reply With Quote #5

Code:
public set_ranks(id) rank[id] = level[id];
__________________

Last edited by Relaxing; 03-18-2018 at 06:55. Reason: jst lower da ranks
Relaxing is offline
NiHiLaNTh
Way Past Expiration
Join Date: May 2009
Location: Latvia
Old 03-18-2018 , 12:07   Re: [HELP] Convert if to switch in this code
Reply With Quote #6

Use arrays or create some sort of formula to calculate the level. I can see the pattern - each 3 levels score against higher ranks (with some exceptions).
__________________

NiHiLaNTh is offline
Send a message via Skype™ to NiHiLaNTh
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-18-2018 , 13:22   Re: [HELP] Convert if to switch in this code
Reply With Quote #7

This uses enums to organize everything. You can set the rank ranges and display names in the rdRanks array.
PHP Code:
#include <amxmodx>

enum Ranks
{
    
RANKS_Noobest,
    
RANKS_Noob,
    
RANKS_Newbiee,
    
RANKS_Easy,
    
RANKS_Normal,
    
RANKS_Hard,
    
RANKS_Expert,
    
RANKS_SuperExpert,
    
RANKS_Specialist,
    
RANKS_Leader,
    
RANKS_Mayor,
    
RANKS_Pro,
    
RANKS_SuperPro,
    
RANKS_Heroic,
    
RANKS_God
}

enum RankData
{
    
RankStart,
    
RankEnd,
    
RankName32 ]
}

new 
rdRanksRanks ][ RankData ] = 
{
    { 
0  2  "Noobest" },
    { 
3  4  "Noob" },
    { 
5  7  "Newbiee" },
    { 
8  10 "Easy" },
    { 
11 13 "Normal" },
    { 
14 16 "Hard" },
    { 
17 19 "Expert" },
    { 
20 22 "Super Expert" },
    { 
23 25 "Specialist" },
    { 
26 28 "Leader" },
    { 
29 31 "Mayor" },
    { 
32 32 "Pro" },
    { 
33 35 "Super Pro" },
    { 
36 42 "Heroic" },
    { 
43 999999 "God" }
};
    
new 
Ranks:rPlayerRankMAX_PLAYERS ] , iLevelMAX_PLAYERS ];

public 
SetRankid )
{
    for ( new 
Ranks:rRank RANKS_Noobest rRank Ranks rRank++ )
    {
        if ( 
rdRanksrRank ][ RankStart ] <= iLevelid ] <= rdRanksrRank ][ RankEnd ] )
        {
            
rPlayerRankid ] = rRank;
            
server_print"Your rank is now %s" rdRanksrRank ][ RankName ] );
            break;
        }
    }

__________________

Last edited by Bugsy; 03-18-2018 at 13:31.
Bugsy is offline
CrAzY MaN
Senior Member
Join Date: Mar 2017
Location: India
Old 03-19-2018 , 07:12   Re: [HELP] Convert if to switch in this code
Reply With Quote #8

Quote:
Originally Posted by Bugsy View Post
This uses enums to organize everything. You can set the rank ranges and display names in the rdRanks array.
PHP Code:
#include <amxmodx>

enum Ranks
{
    
RANKS_Noobest,
    
RANKS_Noob,
    
RANKS_Newbiee,
    
RANKS_Easy,
    
RANKS_Normal,
    
RANKS_Hard,
    
RANKS_Expert,
    
RANKS_SuperExpert,
    
RANKS_Specialist,
    
RANKS_Leader,
    
RANKS_Mayor,
    
RANKS_Pro,
    
RANKS_SuperPro,
    
RANKS_Heroic,
    
RANKS_God
}

enum RankData
{
    
RankStart,
    
RankEnd,
    
RankName32 ]
}

new 
rdRanksRanks ][ RankData ] = 
{
    { 
0  2  "Noobest" },
    { 
3  4  "Noob" },
    { 
5  7  "Newbiee" },
    { 
8  10 "Easy" },
    { 
11 13 "Normal" },
    { 
14 16 "Hard" },
    { 
17 19 "Expert" },
    { 
20 22 "Super Expert" },
    { 
23 25 "Specialist" },
    { 
26 28 "Leader" },
    { 
29 31 "Mayor" },
    { 
32 32 "Pro" },
    { 
33 35 "Super Pro" },
    { 
36 42 "Heroic" },
    { 
43 999999 "God" }
};
    
new 
Ranks:rPlayerRankMAX_PLAYERS ] , iLevelMAX_PLAYERS ];

public 
SetRankid )
{
    for ( new 
Ranks:rRank RANKS_Noobest rRank Ranks rRank++ )
    {
        if ( 
rdRanksrRank ][ RankStart ] <= iLevelid ] <= rdRanksrRank ][ RankEnd ] )
        {
            
rPlayerRankid ] = rRank;
            
server_print"Your rank is now %s" rdRanksrRank ][ RankName ] );
            break;
        }
    }

Thanks, i ll check it out soon!
__________________
CrAzY MaN 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 05:26.


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