[Tutorial] Player Variables vs Normal Variables.
#21

Quote:
Originally Posted by Y_Less
View Post
How many more opinions do you want? All the evidence CLEARLY shows that normal variables are better - it's not even close! There is no competition: if you know what data you are going to be storing in advance, use variables, and for your use case it seems that you do know.

Also, why would you want to move to a filterscript if it works? That just seems like hassle for no gain.
I feel like using a filterscript allows me to more easily organize my code. I'm a compartmentalization freak. As for the speed at which the variables are processed in relation to the PVars, it is pretty astounding. I also find it easier to work with variables as far as programming goes.

Is there any way to organize my code (or compartmentalize) outside of filterscripts?

Thanks in advance.
Reply
#22

Quote:
Originally Posted by Y_Less
View Post
The only valid use I've found so far for PVars was to store a timer ID which almost never appeared, in fact there was a good chance it would never be used in a mode, so having the required several thousand was just a waste of space. However if you're regularly writing and reading a variable you're best off going with normal variables.
  • PVars can be shared/accessed across gamemode scripts and filterscripts, making it easier to modularise your code.
  • PVars are automatically deleted when a player leaves the server, meaning you don't have to manually reset variables for the next player who joins.
  • No real need for complex enums/player info structures.
  • Saves memory by not allocating pawn array elements for playerids which will probably never be used.
  • You can easily enumerate and print/store the PVar list. This makes both debugging and player info storage easier.
  • Even if a PVar hasn't been created, it still will return a default value of 0.
  • PVars can hold very large strings using dynamically allocated memory.
And I would only use normal variables for the player enum I'm using in my GM (so I can load the data directly into it using sscanf 2.0) or if speed plays an important role (OnPlayerUpdate / timers).
+ PVars are way easier to work with.

But yeah, I guess it depends on the person whos scripting.
Reply
#23

That list of points that Garsino posted was originally written by me as an explanation for why the PVar system was created.

Quote:
Originally Posted by Y_Less
View Post
So can properties, and they've been in SA:MP since 0.1.
And that's exactly where the idea for PVars came from. Pawn properties have a big limitation though: you can't easily use them as arrays. In SA-MP's case, you can't easily bind them to a particular player id. And even if you could, you would have to write extra code to reset these every time a player joined/parted the server.

The real power of PVars is that it makes dealing with player data extremely simple. When a player joins the game and logs in, you can get a list of their PVars from a database or file and load them. During gameplay you can update their PVar values, which might include things like player stats etc and use those values for your script logic. You can even add new PVars to their list during gameplay. When the player exits the server, you can dump all of their PVars (I created an example function Util_CreatePVarList() in test_cmds.pwn, which comes with the server) back to the file or database. Now you have a system for persistent player data storage which requires a lot less code than creating fake structs with enums or constantly querying a database.

Not all data in your script is player-specific. In these cases you have no use for PVars. PVars weren't actually designed for that purpose. PVars were created to store player-specific information that you would want to save and share across scripts. Anything related to player stats is a good example.

There is something missing though. Even though I created a function to dump the list of a player's PVars to a string, I didn't create the opposite function to load the player's PVars from a string. The whole system might have made a little bit more sense if I had done that.

There is a performance tradeoff for using PVars over arrays in pawn, but that tradeoff is nothing compared to what would happen if you had a poorly designed player data storage system that relied on querying a database or reading from files.

If you look back at the some of the first SA-MP scripts that had persistent player data, like GF, you'll see that using fake structs and .INI files eventually becomes a big mess which requires a lot of extra script just to store and retrieve player information. If you want to add something new to the player data, you have to go and update the enum, update the .INI data saving/loading procedures, make sure that information is reset when the player join/exits the server.

PVars can make the above very easy. Are your PVars loaded/saved when the player logs in and leaves the server? Want to create a new experience level?
pawn Code:
SetPVarInt(playerid, "fishing_level", 100);
That's all you have to do. If a player didn't have any fishing experience and you did:
pawn Code:
GetPVarInt(playerid, "fishing_level");
it would just return 0. So basically you've defined a new persistent player stat with a single line of code.
Reply
#24

yeah
Reply
#25

Quote:
Originally Posted by BobbMarley
View Post
yeah
Bumping 2011 topic with "yeah " yeah
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)