This is a great solution i really never found something this nice for inheritence, however i can’t seem to access methods in the super class from the sub class or the server script that creates an instance of the sub class:
Super Class:
local AI = {}
AI.interface = {}
AI.schema = {}
AI.metatable = {__index = AI.schema}
local errorSignature = "{AI 2.0} - ERROR : "
-- Local Functions --
local function clearTable(table)
for k in pairs (table) do
table [k] = nil
end
end
-- Constructor --
function AI.prototype(self)
-- Physical Propieties --
self.Rig = nil
self.Humanoid = nil
self.PrimaryPart = nil
return self
end
function AI.interface.new()
return setmetatable(AI.prototype({}),AI.metatable)
end
-- Class Functions --
function AI.schema.isDead(self: AI)
return self.Humanoid.Health <= 0
end
type AI = typeof(AI.prototype()) & typeof(AI.schema)
export type Type = AI
return AI
Sub Class:
local ServerScriptService = game:GetService("ServerScriptService")
local ServerPackage = ServerScriptService.ServerPackage
local TiltAt = require(ServerPackage.Entities.TiltAt)
local AI = require(script.Parent)
local E_AI = {}
E_AI.interface = {}
E_AI.schema = {}
E_AI.metatable = {__index = E_AI.schema}
local errorSignature = "{Entity AI 2.0} - ERROR : "
local warnSignature = "{Entity AI 2.0} - WARN : "
function E_AI.prototype(self,Rig:Model,Stats)
-- Physical Propieties --
self.Rig = Rig or error(errorSignature.."A Rig is required to use the AI Class.")
self.Humanoid = self.Rig:FindFirstChild("Humanoid") or error(errorSignature.."The Rig must have an Humanoid.")
self.PrimaryPart = Rig.PrimaryPart
self.Head = self.Rig:FindFirstChild("Head") or nil
return self
end
function E_AI.interface.new(Rig:Model,Stats)
local self = setmetatable(AI.interface.new(),E_AI.metatable)
E_AI.prototype(self,Rig,Stats)
return self :: E_AI
end
function E_AI.schema.MoveTo(self: E_AI,position: Vector3)
print(self:isDead())
self.Humanoid:MoveTo(position)
end
type E_AI = AI.Type & typeof(E_AI.prototype()) & typeof(E_AI.schema)
return E_AI.interface
Script:
local AI = EntityAIClass.new(workspace.Entities.Joe,{})
print(AI:MoveTo(Vector3.new(0,0,0)))
print(AI.isDead())
Both method calls print out an error: