Uninitialized local var

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sbenitezb
:warning: Old Version Published before Godot 3 was released.

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
			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.

:bust_in_silhouette: Reply From: kidscancode

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.

Damn stupid error. Thank you :slight_smile:

sbenitezb | 2017-05-16 19:47