0 votes

Hi, I have the following code that's failing with error

Invalid operands 'Nil' and 'float' in operator '<='.

func apply_damage():
    var rem = 0.0
    var pureDamage = baseDamage * damageMultiplier * accuracy

    if health == null: return false
    if shield != null:
        rem = shield.take_damage(get_parent(), pureDamage * shieldPenetrationMultiplier)
    if rem <= 0.0 and armor != null:
        rem = armor.take_damage(get_parent(), pureDamage * armorPenetrationMultiplier)
    if rem <= 0.0:
        if health.take_damage(get_parent(), pureDamage) <= 0.0:
            # Target is dead
            release_target()
            return false
    return true

It fails the check where it says 'if rem <= 0.0 and armor != null:'. For some reason variable rem is not initialized... The debugger show 'rem' as empty.

in Engine by (15 points)

1 Answer

+1 vote
Best answer

On this line:

rem = shield.take_damage(get_parent(), pureDamage * shieldPenetrationMultiplier)

You are assigning a value to rem of whatever the take_damage() function returns. It seems it's not returning anything, that's why your error says that rem has no value when you try to compare it.

by (21,920 points)
selected by

Damn stupid error. Thank you :)

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.