Structuring different abilities effects on enemies for combat system

I’m making a combat system, there are different kind of enemies and different behaviours every time they get attacked. Now you can easily achieve this with if statements, but I want to completely avoid this as it is extremely disorganized. Here’s the draft code:

function attack(enemy)
	if enemy.Class == "Tank" then -- I want to avoid this
		-- damage gets halved
		return
	end

	-- continue
end

Any help is profusely appreciated

1 Like

Sadly, I don’t think this is possible in lua, in C# you can use switch cases for this however this is not implemented into lua.

My way of handling this sort of logic is by using a generic stats object that is assigned to each combatant (players + npcs).

function attack(enemy)
	local stats = enemy:GetStats()
	local damageMultiplier = stats.Defence -- or something similar
	
	-- for more complex behaviour I would add listeners to the enemy which should be run when it is attacked
	enemy.Attacked:Fire()
	-- continue
end

One idea would be to have a dictionary of names and multipliers and simply do damage * dictionary[name].

So you meant assign event listeners in different enemy types ? That would work unless I’m using OOP and handle all enemies within a single module script (The only aspect that is handled discrepantly is the attack method).

Yeah when you define the specific enemy type / class you can define what to do when the attack event is fired. Then all that needs to be shared across an abstract enemy is the existence of an Attacked event