Table methods for each player?

So currently I’m working on the data system for my project, and I currently have the table linked to methods.
So there are currently 2 approaches to this:

  1. Have the methods contain a parameter Player that would direct it to the player data to edit it.
  2. Have each player table contain the methods.

So Which of these two am I better off with?

I use method 2, using a meta-tabled custom ‘PlayerData’ class. This is because it looks neater to me, and I enjoy the extra functionality of OOP when I delve into more complex use cases.

There really isn’t a right answer, and either would work. The first one would probably be more ‘efficient’, because metatable calls are more expensive (assuming you are using metatables), but it shouldn’t make a practical difference unless you’re going crazy.

I would stick to how you manage these sorts of things elsewhere. I tend to use OOP as often as I can, because I have a very object-oriented programming mindset after solely using C# for a few months. It’s a matter of preference.

2 Likes

My use case is something around this below.
However note that in total I have about 10-12 functions, not just one.

local PlayerData = {
	Inventory = {
		Equipment = {
		};
		Consumables = {
		};
		Miscellaneous = {
		};
};

function PlayerData.Inventory:AddItem(ItemID, Quantity)
--Stuff Here
end

EDIT: Forgot to mention, about the OOP usage, I don’t use it in the inventory table, as I work with a clone to see if there is enough space to add, or enough items to remove, and then set the self to the modified table.
However self is used in the Equip, Unequip, Currency manipulation, Buff, Debuff, Quest manipulation.

I find it a lot easier to just use OOP, because I can simply index self.Player and self.Inventory without having to jump through extra hoops. = less development time, prettier code.

Here’s an example of some stuff I do in my PlayerData class



function PlayerSave:LoadSaveGame(save_game_id)
	if not self.Player then
		return
	end
	
	save_game_id = tostring(save_game_id)
	
	if self.CurrentSaveGameId == save_game_id then
		return
	end
	
	local save_game = self.SaveGames[save_game_id]
	if not save_game then
		return
	end
	
	save_game = SaveGame:New(save_game)
	
	self.SaveGames[save_game_id].LastPlayed = os.time()
	self.CurrentSaveGameId = save_game_id
	
	GridHandler.BaseHandler:Load(self.Player)
	self.SavegamesLastUpdated = tick()
	
	Network:Send(
		"notify",
		self.Player,
		"Savegame "..save_game.Name.." has been loaded!"
	)
end

function PlayerSave:UnloadSaveGame()
	if not self.Player then
		return
	end
	GridHandler.BaseHandler:Unload(self.Player)
	self.CurrentSaveGameId = nil
	self.SavegamesLastUpdated = os.time()
	self:Send()
end

function PlayerSave:GetCurrentSaveGameFolder()
	local save_game_id = self.CurrentSaveGameId
	if not save_game_id then
		return
	end
	return self.SaveGames[save_game_id]
end

function PlayerSave:Send()

	if not self.Player then
		return
	end
	
	self.LastDataReplication = tick()
	
	Network:Send(
		"send PlayerSave", 
		self.Player, 
		self:SerialiseProfile(false, true)
	)
	
end
1 Like

I totally agree. Also thanks for reminding me to add self.Player to the table as I basically forgot to do that :thinking:.

1 Like