How would I make a "Class" "Inherit" from another class with modules?

Hi, I’m using a lot of modules right now, and I tried to make a class with one. After that, I made another class, that was supposed to inherit from the original one. Here’s how my code looks.

Original class:

local instanceobj = {}
local hitboxobj = require(script.Parent.HitboxObject)

instanceobj.__index = instanceobj

function instanceobj.new()
	local self = setmetatable({}, instanceobj)
	
	self.Position = Vector2.zero
	self.Velocity = Vector2.zero
	
	self.Hitboxes = {}	
	
	return self
end

function instanceobj:Example()
	print("working!")
end

return instanceobj

Child class

local playerobj = {}
local instanceobj = require(script.Parent.InstanceObject)

setmetatable(playerobj, instanceobj)

playerobj.__index = instanceobj or playerobj

function playerobj.new()
	local self = setmetatable({}, playerobj)
	
	self.Damage = 0
	self.Brawler = ""
	
	return self
end

function playerobj:Walk()
	print("working")
end

return playerobj

For some reason, I just can’t seem to access items from both of the classes at the same time. It’s either the super, or the base class. Help is appreciated.

I’m curious, what do you think this line does? I think the error is probably related to that.

Well, I think you’re the actual person I got it from, because I was looking up some posts a few hours ago, and I saw someone that looks like you say this. I guess it just sets the index to the instanceobj, so if it finds something there, it returns, but if it doesn’t, it looks in playerobj?

1 Like

Wait a minute, I think I got it working.

local playerobj = {}
local instanceobj = require(script.Parent.InstanceObject)

setmetatable(playerobj, instanceobj)

playerobj.__index = playerobj

function playerobj.new()
	local self = setmetatable(instanceobj.new(), playerobj)
	
	self.Damage = 0
	self.Brawler = ""
	
	return self
end

return playerobj
local instanceobj = {}
local hitboxobj = require(script.Parent.HitboxObject)

instanceobj.__index = instanceobj

function instanceobj.new()
	local self = setmetatable({}, instanceobj)
	
	self.Position = Vector2.zero
	self.Velocity = Vector2.zero
	
	self.Hitboxes = {}	
	
	return self
end

return instanceobj

I used the instanceobj class to create a new class, then overwrote it’s metatable and set it to the baseclass (playerobj), and then set the index of playerobj to itself, then set the metatable of it to instanceobj, then set the instanceobj’s index to itself! it works!

Just say your latests comments, this reply is to the old one, sorry xD

Lol that’d be a fun coincidence.

That’s not what it does however. At the time when it reaches that line, it evaluates instanceobj or playerobj and stores the value of that expression in playerobj.__index. a or b evaluates to a if it’s truthy and b otherwise. instanceobj is clearly always thruthy, so it would be exactly the same as writing just

playerobj.__index = instanceobj

That means each playerobj instance has a MT of {__index = instanceobj}, which is fine for accessing Example but doesn’t work for acessing Walk.

Try

local ClassA = {}
function ClassA.new()
    return setmetatable({}, {__index = ClassA})
end
function ClassA:DoIt() return "Defined in A" end
function ClassA:DoOther() return "Defined in A" end



local ClassB = setmetatable({}, {__index = ClassA})
function ClassB.new()
    return setmetatable({}, {__index = ClassB})
end
function ClassB:DoIt() return "Defined in B" end
function ClassB:DoThird() return "Defined in B" end



local a = ClassA.new()
local b = ClassB.new()
print(a:DoIt()) --A
print(b:DoIt()) --B
print(a:DoOther()) --A
print(b:DoOther()) --A
print(b:DoThird()) --B
1 Like

Thanks for the info. I guess I thought that Lua would some how evaluate the expression differently based on the context.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.