09.04.2013, 11:42
Can someone give me some examples with 'switch' conditionals?
new var = random( 5 ); switch( var ) { case 0: { print( "Random returned 0 !" ); } case 1: { print( "Random returned 1 !" ); } case 2: { print( "Random returned 2 !" ); } case 3: { print( "Random returned 3 !" ); } case 4: { print( "Random returned 4 !" ); } }
if(a == b)
{
//Do something
}
else if(a == c)
{
//Do something
}
else if(a == d || a == e || a == f)
{
//Do something
}
switch(a)
{
case b:
{
//Do something
}
case c:
{
//Do something
}
case d, e, f:
{
//Do something
}
}
Switch-case? It's one of the simplest things around pawn.
It can replace if conditionals and sometimes save up some space aswell. Example: pawn Код:
pawn Код:
|
So, this is some sly way for you to get a free script? Ha.
There were two ways to use them shown to you. Normally, most people would be able to grasp how they work by those two examples. Or, if you still need some explanation, visit the Wiki, that's what it's there for! https://sampwiki.blast.hk/wiki/Control_Structures#switch |
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
switch(dialogid)
{
case 1: // dialog id that you are using
{
}
case 2:
{
}
case 3:
}
if(dialogid == 1 )
{
}
else blah blah blah
CMD:showlist(playerid, params[])
{
ShowPlayerDialog(playerid, DIALOG_DEFINITION, DIALOG_STYLE_LIST, "This is the list...", "Item 1\nItem 2\nItem 3\nItem 4\nItem 5\nItem 6\nItem 7", "Select", "Cancel");
return 1;
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch(dialogid) // so, basically "if(dialogid == ...)"
{
case DIALOG_DEFINITION: // if(dialogid == DIALOG_DEFINITION)
{
if(response) // if they selected one of the list items..
{
switch(listitem) // basically, "if(listitem == ...)"
{
case 0: // the first list item
{
SendClientMessage(playerid, COLOR, "You selected item 1!");
}
case 1 .. 3: // the second, third, and fourth list items
{
SendClientMessage(playerid, COLOR, "You selected either item 2, item 3, or item 4.");
}
case 4, 6: // items 4 and 6 (NOT 5)
{
SendClientMessage(playerid, COLOR, "You selected either item 5 or item 7.");
}
default: // any list item not listed in any of the above cases (here, it would be list item 6, or case 5)
{
SendClientMessage(playerid, COLOR, "You selected item 6.");
}
}
}
}
}
return 0;
}
This should be a pretty good representation of showing the different ways to use a switch function in a dialog response...
pawn Код:
|
1 .. 3
This should be a pretty good representation of showing the different ways to use a switch function in a dialog response...
pawn Код:
|
#define DIALOG_WEAPONS 1
public OnPlayerCommandText(playerid, cmdtext[])
{
if (strcmp("/buyweapons", cmdtext, true, 12) == 0)
{
ShowPlayerDialog(playerid, DIALOG_WEAPONS, DIALOG_STYLE_LIST, "Weapons", "AK47 - $400\nM4 - $400 \nSniper Rifle - $600", "Buy", "Cancel");
}
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch(dialogid)
{
case DIALOG_WEAPONS:
{
if(response)
{
switch(listitem)
{
case 0:
{
if(GetPlayerMoney(playerid) > 400)
{
new str[128]; new pName[MAX_PLAYER_NAME];
GetPlayerName(playerid, pName, sizeof(pName));
format(str, sizeof(str), "%d has just bought a weapon.", pName);
SendClientMessageToAll(COLOR_RED, str);
GivePlayerWeapon(playerid, AK-47, 500);
GivePlayerMoney(playerid, -400);
}
else
if(GetPlayerMoney(playerid) < 400)
{
SendClientMessage(playerid, COLOR_RED, "You do not have enough money to buy this weapon.");
}
}
case 1:
{
if(GetPlayerMoney(playerid) > 400)
{
new str[128]; new pName[MAX_PLAYER_NAME];
GetPlayerName(playerid, pName, sizeof(pName));
format(str, sizeof(str), "%d has just bought a weapon.", pName);
SendClientMessageToAll(COLOR_RED, str);
GivePlayerWeapon(playerid, M4, 500);
GivePlayerMoney(playerid, -400);
}
else
if(GetPlayerMoney(playerid) < 400)
{
SendClientMessage(playerid, COLOR_RED, "You do not have enough money to buy this weapon.");
}
}
case 2:
{
if(GetPlayerMoney(playerid) > 600)
{
new str[128]; new pName[MAX_PLAYER_NAME];
GetPlayerName(playerid, pName, sizeof(pName));
format(str, sizeof(str), "%d has just bought a weapon.", pName);
SendClientMessageToAll(COLOR_RED, str);
GivePlayerWeapon(playerid, SNIPER_RIFLE, 100);
GivePlayerMoney(playerid, -600);
}
else
if(GetPlayerMoney(playerid) < 600)
{
SendClientMessage(playerid, COLOR_RED, "You do not have enough money to buy this weapon.");
}
}
}
}
}
}
}
}
C:\Users\Denis\Desktop\Scripting\Server 0.3x\gamemodes\TDM.pwn(26 ![]() |