0 votes

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")
Godot version 3.4
in Engine by (24 points)

1 Answer

0 votes
Best answer

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.

by (2,298 points)
selected by

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.