Heya Everyone!!
I’m working on an entity system which controls the enemies in the game. Since I’m fairly new to OOP, I’ve decided to use this DevForum post as a stepping stone for my system.
How the system works is that there is a module script named EntityHandler
and, as said in the title, two more module scripts named MeleeEntity
and RangedEntity
respectively. EntityHandler is essentially the base of both the two aforementioned module scripts, containing both the spawning and despawning of the entity alongside universal movement stuff like pathfinding.
MeleeEntity and RangedEntity is actually the module scripts to give specific functionality to the enemies (Sorry if the wording feels weird). For example, RangedEntity would contain all the functions of a ranged entity such as firing or some type of evade ability like strafing or backing away.
The issue is that I’m stuck on trying to require the two entity modules since doing will cause a “Cycle Module Dependecy” error. How would I fix that?
EntityHandler
--[[SERVICES]]--
local ServerStorage = game:GetService("ServerStorage")
local Debris = game:GetService("Debris")
--[[ENEMY FOLDERS & REQURED ENTITY SCRIPTS]]--
local EnemiesFolder = ServerStorage:WaitForChild("Enemies")
local CommonEnemiesFolder = EnemiesFolder.Commons
local BossesFolder = EnemiesFolder.Bosses
--[[MODULE]]--
local EntityModule = {}
EntityModule.__index = EntityModule
--//Spawning & Despawning
function EntityModule:SpawnEntity(EntityName)
--//Finding the entity.
local RequestedEntity = CommonEnemiesFolder:FindFirstChild(EntityName) if not RequestedEntity then
RequestedEntity = BossesFolder:FindFirstChild(EntityName) if not RequestedEntity then
warn("Cannot find: "..EntityName..".")
return
end
end
local self = setmetatable({}, EntityModule)
self.Entity = RequestedEntity:Clone()
self.Humanoid = self.Entity:FindFirstChildWhichIsA("Humanoid")
self.Entity.Parent = game.Workspace.AliveEnemies
--//Assigning enemy type functionality & despawn function
self:DespawnEntity()
return self
end
function EntityModule:DespawnEntity()
if self.Humanoid.Health >= 0 then
Debris:AddItem(self.Entity,5)
end
end
--//General Movement
function EntityModule:SearchNearbyTargets()
--//TBA
end
function EntityModule:Pathfind(Target)
--//TBA
end
function EntityModule:Circle(EntityModule)
--//TBA
end
return EntityModule
RangedEntity
/MeleeEntity
(They’re almost the same as the time I’m writing this.)
--[[REQUIRED MODULES]]--
local EntityHandler = require(script.Parent)
--[[MODULE]]--
local RangedEntityModule = {}
setmetatable(RangedEntityModule,RangedEntityModule)
RangedEntityModule.__index = EntityHandler
--What I'm planning to do here is to make the EntityHandler use this function to get the Entity model via the Body.
function RangedEntityModule:OnReady(Body)
--//TBA
end
function RangedEntityModule:FireAtTarget()
--//TBA
end
return RangedEntityModule