Better way to do character based abilities?

Hello, I am creating a game that has abilities based on the character I’m using OOP and when a method of the object is called it runs code like this (btw I haven’t wrote the actual code I just imagine it could be done)

if abilityName == "TimeFreeze" then
      -- function to freezetime

elseif abilityName == "Heal" then
      -- function to heal

elseif abilityName == "SuperJump" then
     -- function to super jump
-- KEEPS ON LIKE THIS FOR MANY LINES OF CODE
end

I’m pretty sure there is a better way to do this but I don’t know how thanks for any help.

Store your abilities in a module e.g.

local Abilities = {
	Heal = {
		CallOut = "Wow Magic Heal Thing", -- Example value
		LevelMultiplier = 1.2, -- Just another example value
		Activate = function()
			
		end,
	}
}

return Abilities

Then simply require the module, check the abilityname exists e.g. Abilities[abilityName] then pcall it

if Abilities[abilityName] then
	xpcall(Abilities[abilityName].Activate, function()
		warn("Error Occured")
	end)
end
1 Like

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