Saving per-player data
#1
Hello, my first question here. 

How can I set and get random per-player (cPlayer) data. I want to use it eg to store current effects of a player (when overriding them) or a position or something. A bit like just a random space to save data tied to a player

Maybe there is an obvious solution, but thanks, anyway
Reply
Thanks given by:
#2
Perhaps you can open a player's json file to add/change stuff. Every player has one in the players folder. Since we just got a JSON serializer and deserializer it shouldn't be too hard.
Reply
Thanks given by:
#3
(02-18-2016, 07:56 AM)NiLSPACE Wrote: Perhaps you can open a player's json file to add/change stuff. Every player has one in the players folder. Since we just got a JSON serializer and deserializer it shouldn't be too hard.

Rather than editing server files, wouldn't it be cleaner if the plugin has its own json file? Each object in that file would have a player name for identification, and that player's plugin data. This is better because if I ever delete the plugin's folder, it won't leave junk behind. Also, all you have to do for resetting the plugin is deleting the plugin's data files, instead of editing the server's player files.
Reply
Thanks given by:
#4
I would prefer writing a separate file for each player, but how to store any data just in memory? (and accessing it, with the corresponding cPlayer object)
Reply
Thanks given by:
#5
(02-18-2016, 05:14 PM)Schwertspize Wrote: I would prefer writing a separate file for each player, but how to store any data just in memory? (and accessing it, with the corresponding cPlayer object)

-- Global storage table
PlayerStorage = {}  

PlayerUUID = my_cPlayer_object:GetUUID()

-- Create new player storage, if not created already
if PlayerStorage[PlayerUUID] == nil then
	PlayerStorage[PlayerUUID] = {}
end

-- Save player data
PlayerStorage[PlayerUUID].effect = "whatever";
PlayerStorage[PlayerUUID].etc = 42814

-- Check and read player data
if PlayerStorage[PlayerUUID].effect ~= nil then
	-- Data exists
else
	-- Data does not exist
end

-- Delete specific player data
PlayerStorage[PlayerUUID].effect = nil

-- Delete all player data
PlayerStorage[PlayerUUID] = nil
Reply
Thanks given by:
#6
Another possibility is to use a class to store per-player data. We use it in for example WorldEdit: https://github.com/cuberite/WorldEdit/bl...rState.lua
Reply
Thanks given by:
#7
Rather than addressing by playername, you should address by UUID, other than that, this is most likely the best approach so far. Also, it allows you to serialize the data to a DB when you decide to make it permanent at a later point in the development.
Reply
Thanks given by:
#8
@xoft I updated this accordingly.
Reply
Thanks given by:
#9
Zzzzzz playsrName = GetUUID .... But thanks, I will use it
Reply
Thanks given by:
#10
And that's how bugs are bornTongue
Thanks. Updated.
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)