SA-MP Forums Archive
Switch - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Switch (/showthread.php?tid=429357)



Switch - Syntax - 09.04.2013

Can someone give me some examples with 'switch' conditionals?


Re: Switch - IstuntmanI - 09.04.2013

Код:
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 !" );
	}
}



Re: Switch - [XST]O_x - 09.04.2013

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 Код:
if(a == b)
{
     //Do something
}
else if(a == c)
{
    //Do something
}
else if(a == d || a == e || a == f)
{
    //Do something
}
That is equal to:
pawn Код:
switch(a)
{
    case b:
    {
        //Do something
    }
    case c:
    {
        //Do something
    }
    case d, e, f:
    {
        //Do something
    }
}
It's quite useful if you're checking a single varible for various values.


Re: Switch - Riddick94 - 09.04.2013

Quote:
Originally Posted by [XST]O_x
Посмотреть сообщение
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 Код:
if(a == b)
{
     //Do something
}
else if(a == c)
{
    //Do something
}
else if(a == d || a == e || a == f)
{
    //Do something
}
That is equal to:
pawn Код:
switch(a)
{
    case b:
    {
        //Do something
    }
    case c:
    {
        //Do something
    }
    case d, e, f:
    {
        //Do something
    }
}
It's quite useful if you're checking a single varible for various values.
You forget about default (else). However, this example(s) is/are the best:
https://sampwiki.blast.hk/wiki/Control_Structures#switch
You got everything explained there.


Re: Switch - Syntax - 09.04.2013

I want a whole script to see how they actually work, not the theoretical part.


Re: Switch - Scenario - 09.04.2013

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


Re: Switch - Riddick94 - 09.04.2013

Is your math tutor giving you mathematical task at the beginning and then a theory? Mathematical formulas?


Re: Switch - Syntax - 09.04.2013

Quote:
Originally Posted by RealCop228
Посмотреть сообщение
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
What are you talking about? I am not trying to get a free script.

'whole script' - just a script in which switch-conditionals are used.

Nonetheless, I am aware that I should have been more careful when I asked this question, therefore I would like to sincerely apologise.


Re: Switch - RicaNiel - 09.04.2013

This is an example on
pawn Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
pawn Код:
switch(dialogid)
{
case 1: // dialog id that you are using
{
}
case 2:
{
}
case 3:
}
This is better than

pawn Код:
if(dialogid == 1 )
{
}
else blah blah blah



Re: Switch - Scenario - 09.04.2013

This should be a pretty good representation of showing the different ways to use a switch function in a dialog response...

pawn Код:
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;
}



Re: Switch - Isolated - 09.04.2013

Quote:
Originally Posted by RealCop228
Посмотреть сообщение
This should be a pretty good representation of showing the different ways to use a switch function in a dialog response...

pawn Код:
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;
}
Nice one buddy, never knew you could do
pawn Код:
1 .. 3



Re: Switch - Syntax - 09.04.2013

Quote:
Originally Posted by RealCop228
Посмотреть сообщение
This should be a pretty good representation of showing the different ways to use a switch function in a dialog response...

pawn Код:
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;
}
Thanks, buddy.


Re: Switch - Syntax - 10.04.2013

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.");
                            }
                        }
                    }
                }
            }
        }
    }
}
This is what I've done. I get the following error though:

Quote:

C:\Users\Denis\Desktop\Scripting\Server 0.3x\gamemodes\TDM.pwn(26 : error 054: unmatched closing brace ("}")




Re: Switch - Scenario - 10.04.2013

Your use else if like this:

else
if

It's supposed to he on the same line: else if


Re: Switch - SchurmanCQC - 10.04.2013

Quote:
Originally Posted by RealCop228
Посмотреть сообщение
Your use else if like this:

else
if

It's supposed to he on the same line: else if
I don't think that matters.


Re: Switch - Syntax - 10.04.2013

I fixed the problem. I guess this thread can be locked now.