So I’m trying to make a layout for ranged class and I want to know whats a good way to override the functions if a certain class needs to make modification on the base functions like using custom callbacks.
function BanditArcher:Reward()
end
function BanditArcher:Destroy()
end
function BanditArcher:Shoot()
end
function BanditArcher:Retreat()
end
function BanditArcher:MoveToPlayer()
end
function BanditArcher:FindNearestPlayer()
end
function BanditArcher:gameLoop()
end
so like making proxies, well, i have been messing with that same thing for quite a bit so i can help a bit
normally, i store my new functions inside a table, and i make the code loop through that table and check if the base code has a similar index, if it does, overriding it is as simple as setting the base’s value at that index to the new table’s value
Kinda like this
for index, v in pairs(newFunctions) do
if code[index] then
if typeof(code[index]) == typeof(v) then
code[index] = v
warn(`overwrote {index}`)
else
warn(`Type mismatch for {index}, keeping original function.`)
end
else
code[index] = v
warn(`wrote {index}`)
end
end
it basically just loops through the table and replaces values, it’s not really complicated, but it can be tricky
I use this method that mimics classes and inheritance.
local BaseObject = {}
BaseObject.__index = BaseObject
function BaseObject.new()
local newObject = setmetatable({}, BaseObject)
return newObject
end
local ChildObject= setmetatable({}, {__index = BaseObject})
ChildObject.__index = ChildObject
function ChildObject.new()
local childObject = setmetatable(BaseObject.new(), ChildObject)
return childObject
end
local ChildOfChildObject= setmetatable({}, {__index = ChildObject})
ChildOfChildObject.__index = ChildOfChildObject
function ChildOfChildObject.new()
local childObject = setmetatable(ChildObject.new(), ChildOfChildObject)
return childObject
end
Having a method in the childObject overrides the methods in the parent object. Likely inefficient but looks nice so I use it.