How would I run an entity subclass module?

Heya Everyone!!

I’m working on an entity module and I currently have two classes. A BaseEntity class and a Starvlud (Enemy/Entity) subclass. I’m trying to make it so whenever an entity that uses the subclass spawns, I want the module to run.

BaseEntity

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

--[[FOLDERS]]--
local EntityFolder = ServerStorage:WaitForChild("Entities")
local RegularFolder = EntityFolder.Regular
local SpecialFolder = EntityFolder.Special

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

--//Creating the entity
function BaseEntity:CreateEntity(EntityName)
	--//Finding entity
	local RequestedEntity = RegularFolder:FindFirstChild(EntityName) if not EntityName then
		RequestedEntity = SpecialFolder:FindFirstChild(EntityName) if not EntityName then
			warn("CANNOT FIND ENTITY: "..EntityName)
			return
		end
	end
	
	--//Creating entity
	local self = setmetatable({}, BaseEntity)
	
	self.Entity = RequestedEntity:Clone()
	self.Humanoid = RequestedEntity:FindFirstChildWhichIsA("Humanoid")
	self.Entity.Parent = game.Workspace

	return self
end

return BaseEntity

Starvlud

--[[SUPERCLASS]]--
local BaseEntity = require(script.Parent)

--[[MODULE]]--
local StarvludModule = setmetatable({}, BaseEntity)
StarvludModule.__index = StarvludModule

function StarvludModule:SetupEntity()
	local self = setmetatable(BaseEntity:CreateEntity(), StarvludModule)
	
	return self
end

--//Starvlud will be using a custom "wandering" function.
--//This'll actually make Starvlud try to walk in front of the player.
function StarvludModule:TryAmbush()
	--//TBA
end

--//Otherwise, he'll just use the premade functions in BaseEntity
function StarvludModule:TestFunction()
	--//TBA
end

return StarvludModule

Just make “subclass” index inside table and apply code accordingly to that
There no any reason to creating separate behavior metamethod for that
Metatable OOP by itself is already a sort of wrapper so wrapping this even further is just insanity.
Also applying metatable in the end like:

function StarvludModule:SetupEntity()
	local self = {}
	
	return setmetatable(BaseEntity:CreateEntity(), StarvludModule)
end

Would be a bit better for perfomance.
Non the less i dont think OOP even suits entity systems in general becouse entity systems are probably the most functional programming thing possible to exist.
Entity systems are more of a singletons with array rather than classes or OOP

Can you clarify? I’m confused.

1 Like

you are already making a class…
What is the purpose of making class for a class?
Just put value regarding behavior type somewhere

Looking back, I was overcomplicating myself. I’ll be using something like this and just assign behaviors to their respective entity. Thanks for helping.

1 Like