Trying to calculate damage but always getting 0

Godot Version

Latest stablerelease

Question

I'm trying to calculate damage accounting for defense and critical strike but the function is always returning 0 and I can't understand why

Here’s the function :

func calculate_damage(damage:int, defense: int, crit_chance: int, crit_multiplier: float) -> int:
	print("dmg: "+str(damage)+" defense: "+str(defense)+" crit_multi: "+str(crit_multiplier)+" crit_chance: "+str(crit_chance))
	
	var multiplier: float = 1.0
	if crit_chance > 0:
		if randi_range(1,100) <= crit_chance:
			multiplier = crit_multiplier
			print("Critical strike!")
	
	var base_damage = damage*(damage/(damage+defense))
	print("base dmg: "+str(base_damage))
	var total_damage = base_damage * multiplier
	print("total dmg: "+str(total_damage))
	
	print("calculated dmg: "+str(total_damage))
	return round(total_damage)

Here’s the output I’m getting in-game :

dmg: 10 defense: 10 crit_multi: 2.0 crit_chance: 0
base dmg: 0
total dmg: 0.0
calculated dmg: 0.0
new hp: 100

I checked through some other prints and the error is in this function but everything seems fine to me so I’m confused.

You try to calculate decimal precision equations with int, when you should use float instead.
E.g. 10/20 is 0.5, but because you do that calculation on int and not float, the result is floored to 0 instead, so all your further calculations are also 0.