10.07.2011, 00:02
(
Last edited by Kush; 30/07/2011 at 05:48 PM.
)
Login and Register System - Dialogs - Using SII.
I've seen numerous people, wanting to switch from the older Dini file saving system to a newer one, such as Y_INI, djSON and even Fini. Most of them understand the Dini format, and examples, even tutorials of the newer systems sometimes help, but to some of the newer 'developers' this can be a struggle. I've created this tutorial to help those wishing to upgrade' to a newer system, a better system.
What is this?
Step 2
I've seen numerous people, wanting to switch from the older Dini file saving system to a newer one, such as Y_INI, djSON and even Fini. Most of them understand the Dini format, and examples, even tutorials of the newer systems sometimes help, but to some of the newer 'developers' this can be a struggle. I've created this tutorial to help those wishing to upgrade' to a newer system, a better system.
What is this?
This is a simple tutorial on how to make Login and Register system with dialogs using SII.
Step 1
Firstly, your going to need to download the SII include. You can find it here: https://sampforum.blast.hk/showthread.php?tid=58458. Once downloaded, place it into your Pawno > include folder.Step 2
Add the include at the top of the script.
pawn Code:
#include <SII>
Step 3
Let's define some dialogs:
pawn Code:
#define DIALOG_REGISTER 2000
#define DIALOG_LOGIN 2001
Step 4
Let's add some colors:
pawn Code:
#define WHITE "{FFFFFF}"
#define RED "{F81414}"
#define GREEN "{00FF22}"
#define LIGHTBLUE "{00CED1}"
Step 5
Create a new variable, somewhere below your defines.
pawn Code:
new gPlayerName[MAX_PLAYERS][MAX_PLAYER_NAME];
Step 6
Next, lets add an enum, also known as an enumeration.
pawn Code:
enum pInfo
{
pPass,
pScore,
pCash,
pAdmin
}
new PlayerInfo[MAX_PLAYERS][pInfo];
An enumeration stores many things in a variable. This is extremely efficient and effective, instead of creating a set of different variables.
Step 7
Lets create a stock function:
pawn Code:
stock getINI(playerid)
{
new account[64];
format(account,30,"Users/%s.ini",gPlayerName[playerid]);
return account;
}
The stock function is going to load the user file.
Step 8
We'll be using the native 'fexist' function to search for our file. Parameters are set to our stock function which we've created. If the file exists, you will receive a 'Login' dialog. If it doesn't, you will receive a register dialog.
pawn Code:
public OnPlayerConnect(playerid)
{
GetPlayerName(playerid, gPlayerName[playerid], MAX_PLAYER_NAME);
if (fexist(getINI(playerid)))
{
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT,""WHITE"Login",""WHITE"Type your password below to login.","Login","Quit");
}
else
{
ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT,""WHITE"Registering...",""WHITE"Type your password below to register a new account.","Register","Quit");
}
return 1;
}
Step 9
Go to your OnDialogResponse callback and add this in.
pawn Code:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch( dialogid )
{
case DIALOG_REGISTER:
{
if (!response) return Kick(playerid);
if (response)
{
if(!strlen(inputtext)) {
ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, ""WHITE"Registering...",""RED"You have entered an invalid password.\n"WHITE"Type your password below to register a new account.","Register","Quit");
}
if(INI_Open(getINI(playerid))) {
INI_WriteString("Password",inputtext);
INI_WriteInt("Score",1);
INI_WriteInt("Cash",5000);
INI_WriteInt("Admin",0);
INI_Save();
INI_Close();
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT,""WHITE"Login",""WHITE"Type your password below to login.","Login","Quit");
}
}
}
case DIALOG_LOGIN:
{
if ( !response ) return Kick ( playerid );
if( response )
{
if(!strlen(inputtext)) {
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, ""WHITE"Login",""RED"You have entered an invalid password.\n"WHITE"Type your password below to login.","Login","Quit");
}
if(INI_Open(getINI(playerid))) {
INI_ReadString(PlayerInfo[playerid][pPass],"Password",20);
if(strcmp(inputtext,PlayerInfo[playerid][pPass],false)) {
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, ""WHITE"Login",""RED"You have entered an incorrect password.\n"WHITE"Type your password below to login.","Login","Quit");
}
SetPlayerScore( playerid, INI_ReadInt("Score" ) );
ResetPlayerMoney( playerid );
GivePlayerMoney( playerid, INI_ReadInt( "Cash" ) );
PlayerInfo[playerid][pAdmin] = INI_ReadInt("Admin");
INI_Close();
}
}
}
}
return 1;
}
case DIALOG_REGISTER:
Instead of using the 'if' statement to define my dialogs, I've used cases as they seem to take less space and are supposedly 'faster'. The (!response) is the function if the first Button hasn't been clicked, it will then kick the player.
The if(!strlen(inputtext)) explains if nothing has been entered into the dialog (input), you would then be prompted to another dialog which shows you 'Incorrect Password'.
If all goes well, the function INI_Open is then executed which loads and opens the Userfile. Once open, other functions 'INI_WriteString' 'INI_WriteInt' can be called. It is then finished by the 'INI_Save' function which saves the values which has been set and/or strings which has been entered. Finally, 'INI_Close' closes the Userfile.
Once finished, you will then be prompted to the 'Login' dialog.
case DIALOG_LOGIN:
The only thing which is happening (with the exception of the Userfile being opened and closed) the 'INI_ReadInt' and 'INI_ReadString' function is called. When the player logs in, it loads the players 'stats' or so to say.
Example: If the player had, $1000 dollars before he disconnected, the function 'INI_ReadInt' would read that value, which would be called with GivePlayerMoney to load the player's Money value.
Step 10
Finally, we'll need to save the player stats, when he disconnects.
pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
if(INI_Open(getINI(playerid))) {
INI_WriteInt("Score",PlayerInfo[playerid][pScore]);
INI_WriteInt("Cash",GetPlayerMoney(playerid));
INI_WriteInt("Admin",PlayerInfo[playerid][pAdmin]);
INI_Save();
INI_Close();
}
return 1;
}
I've chose the OnPlayerDisconnect callback to save player 'stats' because this creates MUCH less lag as opposed to the OnPlayerUpdate.
Downloads:
SII Include - https://sampforum.blast.hk/showthread.php?tid=58458
Las Venturas CNR - The man who showed me Dini was shit!