SA-MP Forums Archive
[Include] PVars.inc - Automatic PVar saving! - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+---- Forum: Includes (https://sampforum.blast.hk/forumdisplay.php?fid=83)
+---- Thread: [Include] PVars.inc - Automatic PVar saving! (/showthread.php?tid=545163)



PVars.inc - Automatic PVar saving! - Emmet_ - 06.11.2014

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 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


Re: PVars.inc - Automatic PVar saving! - sammp - 06.11.2014

Nice one, Emmet.


Re: PVars.inc - Automatic PVar saving! - JacobEdwards - 06.11.2014

Useful for testing, nice job Emmet.


Re: PVars.inc - Automatic PVar saving! - daniscape - 29.11.2014

_emmet does it once again! great work emmet


Re: PVars.inc - Automatic PVar saving! - Crayder - 04.05.2015

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


Re: PVars.inc - Automatic PVar saving! - hamzajaved780 - 05.05.2015

Very useful...


Re: PVars.inc - Automatic PVar saving! - Crayder - 05.05.2015

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.


Re: PVars.inc - Automatic PVar saving! - Emmet_ - 05.05.2015

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.


Re: PVars.inc - Automatic PVar saving! - Sellize - 05.05.2015

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!


Re: PVars.inc - Automatic PVar saving! - Emmet_ - 05.05.2015

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!