A bit embarrassed to admit this, but I just figured out how inheritance works and I’m trying to figure out how you can access the variables of the superclass. So the player class extends from CharacterBase and trying to figure out how I can access and overwrite the variables (i.e: use default or not)
local CharacterBase = {}
CharacterBase.__index = CharacterBase
function CharacterBase.new()
print("CharacterBase.new()")
local self = setmetatable({}, CharacterBase)
self.name = "Konjointed"
self.age = "19"
return self
end
function CharacterBase:Destroy()
print("CharacterBase:Destroy()")
end
function CharacterBase:Respawn()
print("CharacterBase:Respawn()")
end
return CharacterBase
local RepStorage = game:GetService("ReplicatedStorage")
local Knit = require(RepStorage.Packages.Knit)
local BaseClass = require(Knit.Modules.CharacterBase)
local PlayerClass = setmetatable({}, BaseClass)
PlayerClass.__index = PlayerClass
function PlayerClass.new()
print("PlayerClass.new()")
local self = setmetatable({}, PlayerClass)
return self
end
function PlayerClass:Destroy()
print("PlayerClass:Destroy()")
end
function PlayerClass:Respawn()
print("PlayerClass:Respawn()")
end
function PlayerClass:Test()
print(self.name)
end
return PlayerClass
I’d implement __index a bit differently for superclasses
local __index = function(self, index) -- gets value from static data or checks in parent
local value = getmetatable(self).__staticData[index]
if value == nil then -- searches parent for value if not in this class
-- this has an issue when value is meant to be nil, but that's rarely the case
local parent = self.parent
if parent then
return parent[index]
end
else
return value
end
return nil
end
local personStaticData = {}
local personMetaTable = {
__index = __index,
__staticData = personStaticData,
}
local function newPerson(name) --new function for a Person object; this would just be new in a module script
local self = setmetatable({
parent = nil, -- Person has no superclass
name = name, -- Person has a nonstatic field, name
}, personMetaTable)
return self
end
function personStaticData:getName()
return self.name
end
function personStaticData:say(message)
print(self.name, "says, \"" ..message .. "\"")
end
local studentStaticData = {}
local studentMetaTable = {
__index = __index,
__staticData = studentStaticData,
}
local function newStudent(name, class)
local parent = newPerson(name) -- setup the parent
local self = setmetatable({
parent = parent,
class = class,
}, studentMetaTable)
return self
end
function studentStaticData:getClass()
return self.class
end
-- override method
function studentStaticData:say(message)
print("AAAAAA")
end
--- example driver ---
local person = newPerson("Steve") -- create a Person
print(person:getName())
person:say("hi")
print()
local student = newStudent("Tom", 5) -- create a Student
print(student:getName()) -- call a method from Person
student:say("hey") -- call a method from the student that overwrote the method from Person
print(student:getClass()) -- call a method from Student
print()