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