OOP Zombies with different abilities?

I’m trying to make an OOP based zombie system, but I’m not sure how I could make it so that zombies have different abilities. Would I have to make separate classes for each zombie and use inheritance? or is there a better way of doing this system?

For example, a normal zombie in my game will just spawn and attack the player, but let’s say I want to have a zombie that has it’s own unique ability that affects it’s mechanics. I’m not sure how I could do something like this, like previously said, should I just use inheritance and make a seperate class for each zombie?

Perhaps you could look into composition.

I hope this helps!

1 Like

I’m still a little confused but basically, for example, I could have a “normal zombie” class, then I can make a class like “boomer zombie” that can borrow specific methods from “normal zombie” class for example, normal zombie could have a :startPathfinding() method which the “boomer zombie” class could borrow from? Or am I still confusing composition with inheritance? Perhaps if you could provide a short example of how my system could work, that would help greatly!

These are OOP subclasses, there are resources out there you can search for tutorials and examples: OOP Inheritance.

1 Like

This is what I was thinking, sorry if it was a bit rushed:


-- This is the zombie super class, the other class will inherit from it
local Zombie = {
	ability = Basic.new(); -- this is the zombie's ability, it's its own seperate object
}
Zombie.__index = Zombie

function Zombie.new() 
	local self = setmetatable({}, Zombie)
	return self
end

function Zombie:UseAbility()
	self.ability:Initiate() -- we initiate the ability
end

function Zombie:Pathfind()
	-- moving...
end

return Zombie

And say, we want another zombie that moves like our normal zombie, but has a cooler ability.

local Bombie = {
	ability = kaboom.new(); -- the ability is different, so it will initiate
       -- kaboom instead of basic
}
Bombie.__index = Bombie

function Bombie.new()
	local self = setmetatable(Bombie, Zombie) -- we inherit all the methods
-- from our normal guy here
	return self
end

I also want to add that this is basically saying that Bombie IS Zombie, but HAS a different ability, it uses inheritance and composition (I’m not sure if its recommended to be using both, you’ll have to do your own research on that)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.