SA-MP Forums Archive
What are switches ment for? (Dialogs). - 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: What are switches ment for? (Dialogs). (/showthread.php?tid=423277)



What are switches ment for? (Dialogs). - rangerxxll - 17.03.2013

I'm just wondering what switch statements actually do, I never really knew. I'll be using a lot of dialogs in my next game mode, and maybe they can be useful.

Thank you.


Respuesta: What are switches ment for? (Dialogs). - Parka - 17.03.2013

switch:

A switch statement is basically a structured if/else if/else system (similar to how for is a structured while). The easiest way to explain it is with an example:
pawn Код:
new
    a = 5;
switch (a)
{
    case 1:
    {
        // Won't be called
    }
    case 2:
    {
        // Won't be called
    }
    case 5:
    {
        // Will be called
    }
    default:
    {
        // Won't be called
    }
}



Re: What are switches ment for? (Dialogs). - mastermax7777 - 17.03.2013

u can do cases like case 1,2,3,4 or 1..4
doesnt matter .. so its better to use this sometimes then old if statements


Re: What are switches ment for? (Dialogs). - JaKe Elite - 17.03.2013

also i will add more information.

switch is faster than if, else if..


Re: What are switches ment for? (Dialogs). - rangerxxll - 17.03.2013

Thank you, appreciated.


Re: What are switches ment for? (Dialogs). - rangerxxll - 17.03.2013

So I was experimenting with dialogs, to see what would be the easiest way to make them. Would this be the proper, effective way? I'm just here to clarify if it is or not.

PS: It does work in-game. No bugs.

pawn Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == MAIN_DIALOG && response)
    {
        switch(listitem)
        {
       
            case 0:
            {
                ShowPlayerDialog(playerid,IN_WEAPONS, DIALOG_STYLE_LIST, "Weapons","Option 1\nOption 2","Select","Return");
            }
            case 1:
            {
                ShowPlayerDialog(playerid,IN_TELEPORTS, DIALOG_STYLE_LIST, "Teleports","Option1\nOption 2","Select","Return");
            }
        }
    }
    if(dialogid == IN_WEAPONS)
    {
        switch(listitem)
        {
            case 0:
            {
                if(!response) return ShowPlayerDialog(playerid, MAIN_DIALOG, DIALOG_STYLE_LIST, "Main Dialog.", "Weapons\nTeleports","Select","Cancel");
                SendClientMessage(playerid, -1, "You selected option 1.");
            }
            case 1:
            {
                if(!response) return ShowPlayerDialog(playerid, MAIN_DIALOG, DIALOG_STYLE_LIST, "Main Dialog.", "Weapons\nTeleports","Select","Cancel");
                SendClientMessage(playerid, -1, "You selected option 2.");
            }
        }
    }
    if(dialogid == IN_TELEPORTS)
    {
        switch(listitem)
        {
            case 0:
            {
                if(!response) return ShowPlayerDialog(playerid, MAIN_DIALOG, DIALOG_STYLE_LIST, "Main Dialog.", "Weapons\nTeleports","Select","Cancel");
                SendClientMessage(playerid, -1, "You selected option 1.");
            }
            case 1:
            {
                if(!response) return ShowPlayerDialog(playerid, MAIN_DIALOG, DIALOG_STYLE_LIST, "Main Dialog.", "Weapons\nTeleports","Select","Cancel");
                SendClientMessage(playerid, -1, "You selected option 2.");
            }
        }
    }
    return 1;
}



Re: What are switches ment for? (Dialogs). - Vince - 17.03.2013

You should put the dialogids in a switch. That's the main reason of slowness. In an if-if-if structure the expression has to be evaluated over and over again.