[Follow Up] How would be I able to use self variables from a base class to a subclass?

Heya Everyone!!

This is a simple follow up from a previous post I’ve made here. I didn’t want to drag it from there so I’ll do a follow up.
For anyone wondering from the previous post, I was trying to access self.Robotxian from the base class, that being ROBOTXIAN_MAIN. Luckily, I was able to get an answer telling me that I wasn’t actually returning self and thus being unable to use it. The fix was simply just adding self to the table inside of the constructor I am using.

The issue now is that I’m trying to use classesSelf (Check the post to what I’m talking about) to use functions inside of the subclass (Which is BUILDER_CLASS) alongside trying to use classesSelf inside of said functions (Such as a Build function.)

Keep in mind that I’m still new to OOP alongside making this post in 11 PM, so I’m pretty sleepy.

ROBOTXIAN_MAIN (Just in case anyone needs it)

--[[INFO]]--
--//ROBOTXIAN_MAIN is the base class of both the 'Builder' and 'Fighter' class.
--//This contains essential functions for both class to work such as pathfinding or even creating Robotxians in the first place.

--[[SERVICES]]--
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")

--[[FOLDERS]]--
local Events_Folder = ReplicatedStorage:WaitForChild("EVENTS")
local Preset_Player_Models_Folder = ServerStorage:WaitForChild("PRESET_PLAYER_MODELS")

--[[MODULE]]--
local ROBOTXIAN_MAIN_FUNCTION = {}
ROBOTXIAN_MAIN_FUNCTION.__index = ROBOTXIAN_MAIN_FUNCTION

--//Spawning and despawning the robotxian.
function ROBOTXIAN_MAIN_FUNCTION.CreateNewRobotxian()
	local self = {}
	
	--//Just to be safe, I'd assume  this is where I'd spawn them in.
	self.Robotxian = ServerStorage.PRESET_PLAYER_MODELS.R6:Clone()
	self.Robotxian.Parent = game.Workspace
	
	return setmetatable({self = self}, ROBOTXIAN_MAIN_FUNCTION)
end


--//Pathfinding
function ROBOTXIAN_MAIN_FUNCTION:Pathfind()
	--//TBA
end

return ROBOTXIAN_MAIN_FUNCTION

BUILDER_CLASS

--[[INFO]]--
--//The 'Builder' class is one of the two classes a robotxian can have.
--//As the name says, robotxians with the class will be able to spawn in parts and modify them to their liking.
--//Additionally, robotxians will also be able to apply both decals and a handful of special effects such as Fire or Sparkles.
--//Generally, robotxians with the class will just spawn in random parts but will occasionally start building actual stuff such as a small house or a poorly made robloxian.

--[[BASE CLASS]]--
local ROBOTXIAN_MAIN = require(script.Parent.Parent.ROBOTXIAN_MAIN)

--[[MODULE]]--
local BUILDER_CLASS_MODULE = setmetatable({}, {__index = ROBOTXIAN_MAIN})
BUILDER_CLASS_MODULE.__index = BUILDER_CLASS_MODULE

function BUILDER_CLASS_MODULE.CreateNewBuilderRobotxian()
	local Robotxian = ROBOTXIAN_MAIN.CreateNewRobotxian()
	local RobotxianSelf = Robotxian.self
end

function BUILDER_CLASS_MODULE:Build()
	--//Trying to use self here.
end

return BUILDER_CLASS_MODULE

Simple, just use self.
Here you are basically inheriting the Robotxian class.

What you do is, just assign this to self.

local BUILDER_CLASS_MODULE = setmetatable({}, {__index = ROBOTXIAN_MAIN})
BUILDER_CLASS_MODULE.__index = BUILDER_CLASS_MODULE

function BUILDER_CLASS_MODULE.CreateNewBuilderRobotxian()
    -- Now you inherit it
    local self = ROBOTXIAN_MAIN.CreateNewRobotxian()
    self.builder = "cool"

    print(self)
    -- Shows all of robotxian class + self.builder = "cool" + self:Build() 

    return setmetatable(self, BUILDER_CLASS_MODULE)
end

Also, self is just a table.
No need to do {self = self}, just self, ROBOTXIAN_MAIN_FUNCTION

return setmetatable({self = self}, ROBOTXIAN_MAIN_FUNCTION)

Also this, which is called “Class-Level inheritance”
basically here you’re inheriting from the base class itself, not from instances created by it…
→ local ROBOTXIAN_MAIN_FUNCTION = {}
Yes, it’s basically requiring the module, but you’re putting it INSIDE BUILDER_CLASS_MODULE, so if you do local Robotxian = BUILDER_CLASS_MODULE.CreateNewRobotxian(), it will actually run!

local BUILDER_CLASS_MODULE = setmetatable({}, {__index = ROBOTXIAN_MAIN})

PERSONALLY I don’t like doing it like that
I recommend simply just doing this.

This is also called “Object-level inheritance”

local BUILDER_CLASS_MODULE = {}
BUILDER_CLASS_MODULE.__index = BUILDER_CLASS_MODULE

-- in the CreateNewBuilderRobotxian function
local Robotxian = ROBOTXIAN_MAIN.CreateNewRobotxian()

And last thing, Luau technically doesn’t have OOP.
tables and metatables simulate it, sorta similarly
So things like Object-level inheritance, Class-level inheritance, don’t actually exist on Luau, they’re only patterns using tables and metatables.

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