Switch
#1

Can someone give me some examples with 'switch' conditionals?
Reply
#2

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

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.
Reply
#4

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.
Reply
#5

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

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
Reply
#7

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

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.
Reply
#9

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
Reply
#10

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;
}
Reply
#11

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
Reply
#12

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.
Reply
#13

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 ("}")

Reply
#14

Your use else if like this:

else
if

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

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.
Reply
#16

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


Forum Jump:


Users browsing this thread: 1 Guest(s)