23.03.2011, 14:35
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:
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:
I can just use:
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:
Then, after a 'case' , put the ShowPlayerDialog code into it. Example:
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:
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:
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
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)
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");
pawn Code:
ShowDialog(playerid, DIALOG_REGISTER);
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: ...
}
pawn Code:
case DIALOG_REGISTER: ShowPlayerDialog(playerid, dialogid, DIALOG_STYLE_INPUT, "Register", "Account not registered.\nPlease type a password", "Register", "STFU");
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");
}
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;
}
- Kevin