[Include] PVars.inc - Automatic PVar saving!
#1

PVars.inc

What is this?
The per-player variable system in SA-MP is mainly used for simple data storing. However, PVars aren't that much useful, except for sharing the data across scripts, but there's "CallRemoteFunction" for that. Some people really only use them just for the storing, and nothing else!

That being said, I've decided to code this library that introduces automatic PVar saving! This makes them much more useful and also allows easier control over user data.

Note that I've released something similar over 2 years ago, but since I requested my topics to be removed, I've decided to recreate it using SQLite.

Features
  • Saving and loading on the fly! You don't need any other additional code.
  • Multiple script support. You can use this in multiple scripts!
  • Easier user data management.
The PVars will automatically save when they are modified and load when a player connects to the server.

Note: "DeletePVar" will remove the value from the database and "SetPlayerName" will automatically readjust the records in the database to the new player's name.

Callbacks
There are two callbacks:

pawn Код:
public OnPVarAttemptLoad(playerid);
This callback is called before a player's PVars are loaded. Returning 0 under this callback will prevent the PVars from loading automatically.

pawn Код:
public OnPVarsLoaded(playerid, count);
This callback is called after a player's PVars have loaded successfully.

Functions

pawn Код:
native LoadPVarsForPlayer(playerid);
Loads the player's PVars. This is useful for login systems, where you can return 0 under "OnPVarAttemptLoad" and then load them when a player logs in.

pawn Код:
native SetPVarAutoSave(playerid, name[], bool:status);
Allows you to disable saving for specific PVars. Setting to "false" will prevent the PVar from being saved.

Exceptions
There might be a few PVars in your script that you don't want to save at all. This can be fixed by disabling auto save for that specific variable.

Example (under OnPlayerConnect):

pawn Код:
// "TempVar" will not be saved or loaded.
SetPVarAutoSave(playerid, "TempVar", false);
Download
http://pastebin.com/6GwvDwx0
Reply
#2

Nice one, Emmet.
Reply
#3

Useful for testing, nice job Emmet.
Reply
#4

_emmet does it once again! great work emmet
Reply
#5

This really deserves a bit more attention! Of course it can be upgraded (which I've done a little), but it's pretty thoughtful.
Reply
#6

Very useful...
Reply
#7

It would be pretty badass if you extended this. Like instead of being totally automatic you could make everything manual. Like SavePlayerPVars and LoadPlayerPVars. Or something like multiple modes, a different mode for each type of file. It already has sqlite, you could add mysql and yini support. Adding the ability to specify where the file is saved would be a little better too.
Reply
#8

That's a great idea. It's nice to see people actually using this.

I plan on adding something like:

pawn Код:
// Save the PVars in a MySQL database.
SavePVarsForPlayer(playerid, e_SAVE_TYPE_MYSQL, "pvars/mysql/", connectionHandle = 1);
// Save the PVars in a SQLite database.
SavePVarsForPlayer(playerid, e_SAVE_TYPE_SQLITE, "pvars/sqlite/");
// Save the PVars in an INI file.
SavePVarsForPlayer(playerid, e_SAVE_TYPE_INI, "pvars/ini/");
And then the same syntax for LoadPVarsForPlayer. The default mode would be e_SAVE_TYPE_SQLITE of course.

This could actually be the beginning of something great for PVars.
Reply
#9

I can't stress how good this include is for people like me, that think saving/loading is a huge pain in the ass.
I might be using this in the future to save and load stats, however is this ineffecient/a bad way of saving stuff?
If not, then I don't see a reason to use y_ini or anything else to save player stats anymore..

GOOD WORK!
Reply
#10

Quote:
Originally Posted by Sellize
Посмотреть сообщение
I can't stress how good this include is for people like me, that think saving/loading is a huge pain in the ass.
I might be using this in the future to save and load stats, however is this ineffecient/a bad way of saving stuff?
If not, then I don't see a reason to use y_ini or anything else to save player stats anymore..

GOOD WORK!
Shouldn't be inefficient at all. SA-MP forums sends about 10 queries for each page refreshed. Sites like ******** send hundreds of thousands of queries per second.

And thanks by the way!
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)