What are switches ment for? (Dialogs).
#1

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

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

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

also i will add more information.

switch is faster than if, else if..
Reply
#5

Thank you, appreciated.
Reply
#6

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

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


Forum Jump:


Users browsing this thread: 1 Guest(s)