Heya there.
I’m working on my first OOP Module for a fan recreation of a game called “Noobs vs Zombies: Relish Reborn”. The OOP Module essentially just handles the AI of the NPC’s. I’m using a module script from this DevForum post as reference.
The issue is that, since I’m new to OOP, I don’t exactly know what I’m doing exactly. What are these module scripts doing?
BaseNPC (Base Module)
--[[MODULE]]--
--//Actual module script stuff
local BaseNPC = {}
BaseNPC._index = BaseNPC
function BaseNPC.New(NPC_Character, NPC_Role, NPC_Type, Spawn_Location)
-- Creating a new NPC
local NewNPC = {}
setmetatable(NewNPC, BaseNPC)
NewNPC.Model = NPC_Character
NewNPC.Role = NPC_Role
NewNPC.Type = NPC_Type
NewNPC.SpawnLocation = Spawn_Location
return NewNPC
end
return BaseNPC
MeleeNPC (Subclass)
--[[SERVICES]]--
local PathfindingService = game:GetService("PathfindingService")
local CollectionService = game:GetService("CollectionService")
local Debris = game:GetService("Debris")
--[[MODULE]]--
--//BaseNPC
local BaseNPC = require(script.Parent)
--//MeleeNPC
local MeleeNPC = {}
MeleeNPC._index = MeleeNPC
setmetatable(MeleeNPC,BaseNPC)
--//Creating the NPC
function MeleeNPC.CreateMeleeNPC(NPC_Character, NPC_Role, NPC_Type, Spawn_Location)
--//Creating new NPC & Positioning it.
local NewMeleeNPC = BaseNPC.New(NPC_Character, NPC_Role, NPC_Type, Spawn_Location)
setmetatable(NewMeleeNPC,MeleeNPC)
local Character = NewMeleeNPC.Model:Clone()
local Humanoid = NewMeleeNPC.Model:FindFirstChildWhichIsA("Humanoid")
local PrimaryPart = NewMeleeNPC.Model.PrimaryPart
Character.Parent = game.Workspace
PrimaryPart:PivotTo(Spawn_Location.CFrame * CFrame.new(0, 3, 0))
end
return MeleeNPC
Think of OOP like a hierarchy, where you shove functions and variables under an object. The stuff you’ve got here is modular programming combined with OOP, and probably not required if its your FIRST module/project.
(I am a little confused, assuming the module scripts are separate from the phrase “first module”)
I’m interpreting term module in “first module” as “first project”.
take the line below for example.
local Humanoid = NewMeleeNPC.Model:FindFirstChildWhichIsA("Humanoid")
The object here is the NewMeleeNPC, and encapsulated under it, is the model, and then the Humanoid.
For my first project, I wrote a bank-account “simulation”
Object being Bankaccount, then have all these little things encapsulated under it rather than stored globally.
I’d look towards something like that if you’re a beginner. It becomes pretty intuitive at points.
Now, the nice thing about doing OOP in Roblox is EVERYTHING is object-oriented. Everything in your workspace are objects, with properties.
Ever seen someone change the color of a brick with a script? Thats OOP
Brick.Color = ...
hope this helps,
2 Likes
About the part where you are confused and assuming the module scripts are separate from the phrase first module, this is what I mean I guess?
Okay, If you need to write an OOP project, and It’s your first one, I wouldn’t implement modular programming, as it’s just another level of complexity. Look into the basics, I sent some stuff in my previous response.
I’m hoping you know how modular programming works, if not here’s a brief description.
- You write code in the module script
- You can reference that script in other scripts as a variable
- You can call functions variables or properties from this module script in your other scripts
You mentioned you’re a beginner to OOP, so again, I would start with something simpler.
Try rewriting any project at all, a simple one-
WITH the implementation of OOP.
You can start by categorizing all your variables and functions under different objects.