[Include] [BETA] users.inc - Fast and EASY user system
#1

This is my 1000th post, so I figured I might as well make it interesting.

Just like bacon, this user system is awesome and easy to deal with! One of the main features of this is it will auto-magically load from and save data into variables!

Brief, fully-working example on what I mean:
pawn Код:
new g_LogInCount[MAX_PLAYERS];

public OnGameModeInit() {
    // This will make the variable automatically load and save for all players
    BindUserVar("login-count", g_LogInCount);
}

public OnPlayerLogIn(playerid) {
    // Increase by 1
    g_LogInCount[playerid] += 1;

    new buf[128];

    format(buf, sizeof(buf), "* Welcome! You have logged in %d time(s).", g_LogInCount[playerid]);

    SendClientMessage(playerid, 0xFFFFFFFF, buf);
}
You can use many different kinds of variables; supported structures:
  • Normal arrays
  • "char arrays"
  • y_bit arrays
  • rBits arrays
  • String arrays
  • Two-dimensional arrays
Example of how you do that:
pawn Код:
new
             g_Array[MAX_PLAYERS],
             g_CharArray[MAX_PLAYERS char],
    BitArray:g_y_bitArray<MAX_PLAYERS>,
       Bit16:g_rBitsArray<MAX_PLAYERS>,
             g_StringArray[MAX_PLAYERS][64],
             g_2dArray[MAX_PLAYERS][10]
;

public OnGameModeInit() {
    // The 1st argument is the name of the user var; it can be pretty much anything.
    BindUserVar("array"      , g_Array);
    BindUserVar("char-array" , g_CharArray);
    BindUserVar("bit-array"  , g_y_bitArray);
    BindUserVar("16bit-array", g_rBitsArray);
    BindUserVarString("string-array", g_StringArray);
    BindUserVarArray ("2d-array"    , g_2dArray);
}
That's right, there's nothing else you need to do! Those arrays would now be saved&loaded for each player, and new players will have them filled with 0.

Fully working example script
This script will save and load weapons, score, and playtime! It also has dialogs for log-in and registration.
Download: http://pastebin.com/LyXtkM7S

Functions
pawn Код:
// Self-explanatory
 bool:IsPlayerLoggedIn(playerid)
 bool:IsPlayerRegistered(playerid)

// Fires the callback OnPlayerLogIn or OnPlayerFailLogIn when done
      AuthPlayer(playerid, password[])

// Fires the callbacks OnPlayerRegister then OnPlayerLogIn when done
      RegisterPlayer(playerid, password[])

// This is called automatically when a player disconnects
// but you might want to save the data more often (in case of a crash).
      SavePlayerUserData(playerid)

//    BindUserVar*
      BindUserVar(key[], variable[])
      BindUserVarArray(key[], variable[MAX_PLAYERS][])
      BindUserVarString(key[], variable[MAX_PLAYERS][])

//    GetUserVar*
      GetUserVarArrayInt(playerid, key[], index)
Float:GetUserVarArrayFloat(playerid, key[], index)
      GetUserVarInt(playerid, key[])
Float:GetUserVarFloat(playerid, key[])
      GetUserVarString(playerid, key[], output[], output_size = sizeof(output))

//    SetUserVar*
      SetUserVar(playerid, key[], value)
      SetUserVarArray(playerid, key[], iIndex, {_, Float}:xValue)
      SetUserVarFloat(playerid, key[], Float:fValue)
      SetUserVarInt(playerid, key[], iValue)
      SetUserVarString(playerid, key[], /*const*/ szValue[])
Callbacks
pawn Код:
// This is called when a player's name info is loaded (almost right after OnPlayerConnect).
public OnPlayerNameInfoLoad(playerid, bool:is_registered, bool:is_logged_in)

// This is called when a player was successfully registered.
public OnPlayerRegister(playerid)

// This is called when a player logs in (also called after a player registers).
public OnPlayerLogIn(playerid)

// This is called when a player enters the wrong password.
public OnPlayerFailLogIn(playerid)

// This is called right before the user data will be saved.
public OnPlayerUserDataSave(playerid)
Download
Note: This include requires Whirlpool (get it here).

Do you understand that this is a beta and that it requires whirlpool? Yes / No.
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)