How to "switch"
#1

Hello guys, I have some problem.
I know the code bellow i can write in switch, but i don't know how...

Код:
new string[128], Nick[MAX_PLAYER_NAME];
	GetPlayerName(playerid, Nick, sizeof(Nick));
	if(Player[playerid][pAdminLevel] == 3)
	{
		format(string, sizeof(string), "(Junior Admin ID:%d): %s",playerid, text);
		SendPlayerMessageToAll(playerid, string);
	}
	if(Player[playerid][pAdminLevel] == 4)
	{
		format(string, sizeof(string), "(Admin ID:%d): %s",playerid, text);
		SendPlayerMessageToAll(playerid, string);
	}
	if(Player[playerid][pAdminLevel] == 5)
	{
	    format(string,sizeof(string), "(Head Admin ID:%d): %s", playerid, text);
	    SendPlayerMessageToAll(playerid, string);
	}
	else
	{
		format(string, sizeof(string), "(ID:%d): %s", playerid, text);
	 	SendPlayerMessageToAll(playerid, string);
	}
Greetings, micol.
Reply
#2

pawn Код:
new string[128], Nick[MAX_PLAYER_NAME];
    GetPlayerName(playerid, Nick, sizeof(Nick));
    switch(Player[playerid][pAdminLevel])
    {
        case 3: //If Player[playerid][pAdminLevel] equals to 3.
        {
            format(string, sizeof(string), "(Junior Admin ID:%d): %s",playerid, text);
            SendPlayerMessageToAll(playerid, string);
        }
        case 4: //If Player[playerid][pAdminLevel] equals to 4.
        {
            format(string, sizeof(string), "(Admin ID:%d): %s",playerid, text);
            SendPlayerMessageToAll(playerid, string);
        }
        case 5: //If Player[playerid][pAdminLevel] equals to 5.
        {
            format(string,sizeof(string), "(Head Admin ID:%d): %s", playerid, text);
            SendPlayerMessageToAll(playerid, string);
        }
        default: //If all of the cases above are false.
        {
            format(string, sizeof(string), "(ID:%d): %s", playerid, text);
            SendPlayerMessageToAll(playerid, string);
        }
    }
http://wiki.amxmodx.org/Pawn_Tutorial#Switch_Statements
Reply
#3

Just a tip if you're going to send the message in all cases: move SendPlayerMessageToAll from the switch parts to the end of the switch statement. Should make your code smaller!
Reply
#4

Thanks for all sugestions.

Greetings, micol.


@EDIT

And if we're almost at the end can you show me how this code will be look in switch? : D

Код:
if(arena1[playerid] == true)
	{
	    new arena2 = random(sizeof(arenaspawn));
	    SetPlayerPos(playerid, arenaspawn[arena2][0], arenaspawn[arena2][1], arenaspawn[arena2][2]);
	    ResetPlayerWeapons(playerid);
	    GivePlayerWeapon(playerid, 38,999999);
	}
	if(arenade1[playerid] == true)
	{
 		new arenade2 = random(sizeof(arenadespawn));
	    SetPlayerPos(playerid, arenadespawn[arenade2][0], arenadespawn[arenade2][1], arenadespawn[arenade2][2]);
	    ResetPlayerWeapons(playerid);
	    GivePlayerWeapon(playerid, 24,200);
	}
	if(arenaso1[playerid] == true)
	{
	    new arenaso2 = random(sizeof(arenasospawn));
	    SetPlayerPos(playerid, arenasospawn[arenaso2][0], arenasospawn[arenaso2][1], arenasospawn[arenaso2][2]);
	    ResetPlayerWeapons(playerid);
	    GivePlayerWeapon(playerid, 26,300);
	}
Greeting, micol.
Reply
#5

Why not give it a try by yourself?
Reply
#6

My try:

pawn Код:
switch(arena1[playerid])
{
    case 1:
    {
        new arena2 = random(sizeof(arenaspawn));
        SetPlayerPos(playerid, arenaspawn[arena2][0], arenaspawn[arena2][1], arenaspawn[arena2][2]);
        ResetPlayerWeapons(playerid);
        GivePlayerWeapon(playerid, 38,999999);
    }
}
switch(arenade1[playerid])
{
    case 1:
    {
        new arenade2 = random(sizeof(arenadespawn));
        SetPlayerPos(playerid, arenadespawn[arenade2][0], arenadespawn[arenade2][1], arenadespawn[arenade2][2]);
        ResetPlayerWeapons(playerid);
        GivePlayerWeapon(playerid, 24,200);
    }
}
switch(arenaso1[playerid])
{
    case 1:
    {
        new arenaso2 = random(sizeof(arenasospawn));
        SetPlayerPos(playerid, arenasospawn[arenaso2][0], arenasospawn[arenaso2][1], arenasospawn[arenaso2][2]);
        ResetPlayerWeapons(playerid);
        GivePlayerWeapon(playerid, 26,300);
    }
}
I know this can be smaller, but how ?(Just another question)


Greetings, micol.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)