My damage formula is returning zero for some reason

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Spiky_Potato

So I have reworked my script and have run into a different problem, my damage formula seems to be spitting out nothing. My Magic damage formula, which is exactly the same save for the variables used, does seem to work correctly though. If anyone has an ideas, please let me know.

func WeaponAttack(User, Target):
	Damage = null
	Damage = ceil(User.Attack * (100 / (100 + Target.Defense)))
	Target.Health = Target.Health - Damage
	DialogueBox.text = "The " + User.Name + " attacked the " + Target.Name + ", it dealt " + str(Damage) + " damage."
	yield(get_tree().create_timer(0.2),"timeout")
	emit_signal("next_player")

func MagicAttack(User, Target, SpellElement):
	Damage = null
	Damage = ceil(User.MagicAttack * (100 / (100 + Target.MagicDefense)))
	CheckElementResistance(Target, SpellElement)
	Target.Health = Target.Health - Damage
	DialogueBox.text = "The " + User.Name + " cast a " + User.Element + " spell at the " + Target.Name + ", it dealt " + str(Damage) + " damage."
	yield(get_tree().create_timer(0.2),"timeout")
	emit_signal("next_player")
:bust_in_silhouette: Reply From: hilfazer

It could be that Target.MagicDefense is of type float while Target.Defense is int.
In which case the following expression will result in float:

(100 / (100 + Target.MagicDefense))

while this one in int:

(100 / (100 + Target.Defense))

because of the following rules:
int / int == int
int / float == float

Now, if Target.Defense is greater than zero the value of (100 / (100 + Target.Defense)) will be rounded to 0.

To avoid integer division you can do this in the denominator:

 (100.0 + Target.Defense)

It’s probably a good idea to do that change in both of your functions.

Again, you are my savior. Thanks so much, I had no idea that it mattered whether it was a float or int when using math. I guess it does make sense though. Thanks again.

Spiky_Potato | 2021-09-07 13:44