I’m wondering what is an effective way of handling NPC behavior. Let’s assume I have a module script that would contain base functions of a NPC such as spawning one, pathfinding, and so on.
The issue is that let’s say I want two NPCS that uses the functions from said module script but have completely different attacks. For example, NPC 1 exclusively uses melee attacks while NPC 2 exclusively uses ranged attacks.
How would I go handling NPC 1 or 2’s behavior? Would I use a server script that would require the module script or use another module script? I’d also like to see how you would format/handle such.
I would say have a new module script for attacks and handle it all on the server script but have the visual effects on the client (use remote events and :FireClient())
I’m assuming you are thinking that I’m working on a fighting game which I’m not. Secondly, all NPCs will have unique gimmicks and attacks which containing all of them inside of a module script wouldn’t work.
For example I would have a NPC that would throw explosives and plant bombs while I could have other NPCS fly, shoot, charge, and so on. I wouldn’t think putting their attacks on a module script would work.
I don’t have any experience but I think that people have individual modules that load different attacks and stuff like that.
Like if one of your NPCs had melee attacks, you’d require a Melee module. And if that NPC could also use a gun, then load that corresponding module.
It’s kinda like building up multiple classes with the same component modules. I like to think of this like legos. Like you can switch parts and stuff.
Like let’s say you wanted one of your NPCs to have access to a pistol:
local Gun = require(path.to.gun)
function NPC.new()
local self = setmetatable({}, NPC)
self.Gun = Gun.new("Pistol")
end
Then if you need the NPC to fire their weapon, you can easily do so
other
You could also make a function in the NPC class itself to make it cleaner
function NPC:Shoot(…)
if not self.Gun then return end
-- some logic I guess
end
NPC.Gun:Shoot(…)
I’d probably load the modules in the NPC one.
And I’d suggest attaching new objects when you’re making an NPC. And add parameters to know which ones to attach
local Gun = too lazy
local HandCombat = too lazy
function NPC.new(params)
local self = setmetatable({}, NPC)
self.Gun = params.Gun and Gun.new(…)
self.HandCombat = params.HandCombat and HandCombat.new(…)
-- you get it
end
You’re essentially telling me Composition, which is what I was thinking. Anyway, I’m gonna assume I should probably write the AI in a server script while using functions from the module script?
There could a better way but I really can’t think of any right now.