In my oop module the character has a basic move they do when they don’t have something equipped. How can I override that if the character picks up something for example? Would it be good to make a seperate script or should I just include a if statement before the oop function in the module script
if condition == true then
player:function()
-- do stuff
elseif condition == false then
-- more stuff for finding out what the player is holding
end
end
Well… it depends on how you implement everything in the end, but you could probably get away with just an if statement for now. When things get more complex, you could add a state-checking function to your OOP module, and use the result of that function to determine what to do next.
local myModule = {}
myModule.__index = myModule
-- .new(), etc. etc...
function myModule:CheckState()
-- replace with your own stuff
if self.Humanoid.Jump == true then
return "Jumping"
else
return "NotJumping"
end
end
Its essentially if the player has nothing equipped they do basic punches if they have a hammer or etc. They use the logic and animations for that weapon. Also the equipped item is not a tool it will just be an object welded to the player so the Module script has to handle the changing of functionality.
So each weapon has its separate module with its different functions and when another item is equipped it destroys the current combat module and adds the new combat module?
That’s how I’d do it, except you don’t need to destroy modules, just the OOP object. You can keep the module somewhere it can be reused., like serverstorage or replicatedstorage.
If you have multiple tools in a hotbar or something, you can instead add a function inside the tool’s module to unequip/disable the tool when you equipment a different one, instead of destroying the module or objects and making a new one each time.
You got the basic idea, but you should have one base-class for weapons.
i.e a base class for a gun like
local GunBase = {}
GunBase.__index = GunBase
function GunBase.init()
local self = setmetatable({}, GunBase)
self.config = {} -- Default configs here
return self
end
function GunBase:Fire()
-- Shooting logic here
end
function GunBase:Reload()
-- Reloading logic here
end
-- etc etc
Then like @Cure_Carbon said, put these modules somewhere that they can be re-used.
Not necessarily the best approach. But the way I’m doing it in my stuff is by having input layers that get stacked. Where a weapon for example creates a layer and puts it in the input manager. The input manager of my game takes any input and travels down the layers in reverse order added and calls the functions related to it. My input system then expects a value back saying if the input should fall through to the next layer or if it wants to absorb the input. (It throws errors if you violate a fundamental assumption of the system like try to yield the task during an input handler call). It also has stubs for a priority system for customizable logic that doesn’t quite fit cleanly in the layers but I never finished it because I’m not using it.