Using OOC without directly indexing a table

Would it be possible to make an OOC type code without directly indexing the table? My problem is that I need to be able to call a function every time an index is called on a table, I could put this in every function but that’s repetitive and doesn’t seem like a good habit.

Here’s my current code that repeats the same function “UpdateData()” a bunch

local Bank = {}
local BankHandler = {}
local PlayerData = require(script.Parent.Parent:WaitForChild("PlayerData"))

function BankHandler:UpdateData()
	if PlayerData and PlayerData[self.Ownership] then
		PlayerData[self.Ownership].Bank = self.Amount
	else
		warn("Users data not found!")
	end
end

function BankHandler:Clear()
	self.Amount = 0
	self:UpdateData()
	return 0
end

function BankHandler:Set(amount)
	self.Amount = amount
	self:UpdateData()
	return self.Amount or 0
end

function BankHandler:Remove(amount)
	self.Amount = self.Amount - amount
	self:UpdateData()
	return self.Amount or 0
end

function BankHandler:Add(amount)
	self.Amount = self.Amount + amount
	self:UpdateData()
	return self.Amount or 0
end

function Bank.New(player, amount)
	local usersBank = setmetatable({
		Amount = amount,
		Ownership = player,
	}, {
		__index = BankHandler
	})
	return usersBank
end

return Bank

Is it possible to do something like this?

_index = function(self, index)
   -- index function here (but it must have the arguments as well)
   self:UpdateData()
end

Not sure what you mean by that.

Yes. Not sure what your issue or question is?

__index = BankHandler

My question is how could I do this with arguments as well.

Huh?

Still not following, what arguments would that be, could you elaborate or show an example of what you mean?

Alright…

So currently I set the metatable to have an index of BankHandler, what I’d rather do is run a function which will run the index function

Example:

_index = function(self, index)
   -- index function here (but it must have the arguments as well)
   self:UpdateData()
end

So if I call Add, it’ll run that function and then add money to the users account as it should. After that I’d like to update the users data.

Currently I am updating the data in each function which seems to be inefficient, where I’d rather just update it once.

So you want to do something like this?

local usersBank = setmetatable({
    Amount = amount,
    Ownership = player,
}, {
    __index = function(self, i)
        local func = BankHandler[i]
        if not func then
            return nil
        end
        return function(self, ...)
            local ret = func(self, ...)
            BankHandler:UpdateData()
            return ret
        end
    end
})
return usersBank

Not sure if this is what you mean tho. Please be more clear.

2 Likes

You posted that snippet already and yes you could do something like that, I’m not following what the issue is or what arguments you’re referring to.

If you’re talking about object oriented programming, the correct acronym would be OOP rather than OOC, might help avert confusion in future!

2 Likes

lua doesnt do inline functions. if you are concerned about ‘performance’ in a place where it doesnt even matter (not like the bank stuff is gonna be called hundreds of times per second) then just don’t use OOP altogether i suppose