[Tutorial] Dialog tips
#1

Hey! I'm gonna give you some tips about dialogs.

1) Make defines for dialog ID's.
Sometimes you can forget a ID for a dialog when you have a big script, and then you need to find it back.
THAT is pretty annoying, even when it's a less big script. Solution: When you make a gamemode, make defines immediately when you start making the gamemode. Here's a little part of my upcoming RL gamemode:
pawn Code:
#define DIALOG_REGISTER (1)
#define DIALOG_LOGIN    (2)
Now I can simply use ShowPlayerDialog(playerid, DIALOG_REGISTER... and the 'if(dialogid == DIALOG_REGISTER)' (I actually use switch/case..). But this can be easier if you have alot of dialog ID's.
Then I use the function "ShowDialog". See below

2) A function to call dialog's easily
I've made (because I'm a bit lazy) a function. Instead of:
pawn Code:
ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, "Register", "Account not registered.\nPlease type a password", "Register", "STFU");
I can just use:
pawn Code:
ShowDialog(playerid, DIALOG_REGISTER);
How to do this?
Make a function with two parameters: playerid & dialogid. The playerid is needed to show the dialog to that player (you should know that) and the dialogid ... well for the dialog.
I also made a 3rd parameters: extrastring[] = "". This is to add extra text to the default string (I'll explain that later).
However, now you need to check which 'dialogid' is given (when using the function). You can just use 'if(dialogid == DIALOG_REGISTER)' , but I prefer switch:
pawn Code:
switch(dialogid)
{
    case DIALOG_REGISTER: ...
}
Then, after a 'case' , put the ShowPlayerDialog code into it. Example:
pawn Code:
case DIALOG_REGISTER: ShowPlayerDialog(playerid, dialogid, DIALOG_STYLE_INPUT, "Register", "Account not registered.\nPlease type a password", "Register", "STFU");
Then close the whole function (the switch and the function itself) so you can make it work.
And about the 'extrastring[] = false' - I'd use that, because when someone eg. use a wrong password, or a too long password, I can add the text "Wrong password!" below the old string. That's so I don't need to make a whole new define etc for one dialog. How? Well like this:
pawn Code:
//With the 'new str[100];'
case DIALOG_REGISTER:{
        format(str, 100, "Account not registered.\nPlease type a password.\n%s", extrastring);
        ShowPlayerDialog(playerid, dialogid, DIALOG_STYLE_INPUT, "Register", str, "Register", "STFU");
}
So, when you need to call the normal register dialog, you can now use ShowDialog(playerid, DIALOG_REGISTER);. If the player'd put a too long password, I can add the text "Too long password length!" like this:
ShowDialog(playerid, DIALOG_REGISTER, "{FF0000}Too long password length!");

Well that was it actually. Just to have an easier way to call the dialogs, and a define, so you don't need to refind them all the time. Here's an example of the "whole" script:

pawn Code:
#include <a_samp>

#define DIALOG_REGISTER     (1)
#define DIALOG_LOGIN        (2)
#define MAX_PASS_LENGTH     (50)

public OnPlayerConnect(playerid)
{
    if(!UserFileExists(playerid)) //Fake function. No need to make a script that checks if a file exists
        ShowDialog(playerid, DIALOG_REGISTER);
    else
        ShowDialog(playerid, DIALOG_LOGIN);
    return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch(dialogid)
    {
        case DIALOG_REGISTER:
        {
            if(strlen(inputtext) > MAX_PASS_LENGTH)
                return ShowDialog(playerid, DIALOG_REGISTER, "{FF0000}Too long password length!");
        }
        case DIALOG_LOGIN:
        {
             if(strlen(inputtext) > MAX_PASS_LENGTH)
                 return ShowDialog(playerid, DIALOG_LOGIN, "{FF0000}Too long password length!");
             if(strcmp(inputtext, UserPasswordFile(playerid))) //Another fake function (UserPasswordFile)
                 return ShowDialog(playerid, DIALOG_LOGIN, "{FF0000}Wrong password!");
        }
    }
    return 1;
}

stock ShowDialog(playerid, dialogid, extrastring[] = false) //Why not extrastring[] = ""? Well, that didn't work ;)
{
    new str[500]; //lol 500? Yes- I don't know max dialog length + I use dialogs with big texts. Then it'll be too short
    switch(dialogid)
    {
        case DIALOG_REGISTER:
        {
            format(str, 500, "This account isn't registered!\nPlease type a password to register.\n%s", extrastring);
            ShowPlayerDialog(playerid, dialogid, DIALOG_STYLE_INPUT, "Register", str, "Register", "Kick me");
        }
        case DIALOG_LOGIN:
        {
            format(str, 500, "This account is registered!\nPlease type the password to login.\n%s", extrastring);
            ShowPlayerDialog(playerid, dialogid, DIALOG_STYLE_INPUT, "Login", str, "Login", "Kick me");      
        }

        default: /*So, invalid ID*/ ShowPlayerDialog(playerid, dialogid, DIALOG_STYLE_MSGBOX, "Error", "ERROR: Wrong dialog ID", "OK", "");
    }
    return 1;
}
It's possible that I made some mistakes in the script (eg. a typo), but this ain't a copy-paste tutorial. It's not even a script tutorial, actually. I just gave some tips you could use, and I just putted some scripts in it.

- Kevin
Reply
#2

Usefull tutorial, Good though
Reply
#3

Thanks, VERY usefull! Got a lot of problems with dialogs :P

-J
Reply
#4

The "ShowDialog" trick is actually pretty usefull. I think I will use it
Reply
#5

im writing a tut in the dutch section bout dialogs too. (jantjuh inspired me to it. )

nice tut too man!
i like the ShowDialog trick very much. but it'll slow your script too. it checks for wich dialogid, calls a extra function.
id still prefer the old fashion way
Reply
#6

Quote:

but it'll slow your script too. it checks for wich dialogid, calls a extra function.

Damn I wanted to add that but forgot it I guess. However I'll add that too soon :P (My comp is lagging and I have slow internet. This laggs too , and so I won't think about what should happen when I edit a big post. :/
Reply
#7

Quote:
Originally Posted by legodude
View Post
im writing a tut in the dutch section bout dialogs too. (jantjuh inspired me to it. )

nice tut too man!
i like the ShowDialog trick very much. but it'll slow your script too. it checks for wich dialogid, calls a extra function.
id still prefer the old fashion way
It will slow down your script, yes. But to be honest thats not anything I would be worried about.
Reply
#8

Usefull, thanks for the release.
Reply
#9

By the way, about the slowdown: It's a very small slowdown. You can't really notice that ingame, especially when you're new / not the gamemode maker.
Reply
#10

Quote:
Originally Posted by legodude
View Post
nice tut too man!
i like the ShowDialog trick very much. but it'll slow your script too. it checks for wich dialogid, calls a extra function.
id still prefer the old fashion way
In a few years, you will maybe save a second. If that's very important in your life ... well than use your way.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)