AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [HELP] Convert if to switch in this code (https://forums.alliedmods.net/showthread.php?t=306141)

CrAzY MaN 03-18-2018 02:06

[HELP] Convert if to switch in this code
 
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



CookieCrumbler 03-18-2018 04:25

Re: [HELP] Convert if to switch in this code
 
https://wiki.alliedmods.net/Pawn_Tutorial#Conditionals

Relaxing 03-18-2018 04:47

Re: [HELP] Convert if to switch in this code
 
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.

CrAzY MaN 03-18-2018 04:54

Re: [HELP] Convert if to switch in this code
 
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;
        }
    }



Relaxing 03-18-2018 06:16

Re: [HELP] Convert if to switch in this code
 
Code:
public set_ranks(id) rank[id] = level[id];

NiHiLaNTh 03-18-2018 12:07

Re: [HELP] Convert if to switch in this code
 
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).

Bugsy 03-18-2018 13:22

Re: [HELP] Convert if to switch in this code
 
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;
        }
    }



CrAzY MaN 03-19-2018 07:12

Re: [HELP] Convert if to switch in this code
 
Quote:

Originally Posted by Bugsy (Post 2583510)
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!


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

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