• Godot Help
  • Invalid call. Nonexistent function 'update_life' in base 'Nil'.

Hello everyone! That's magic to me:

I got 3 scenes, Main, Player and HUD

Inside player, I have a signal when the score should change and when we lost hp, then it down.
Afterthat this signals come to Main where this script reffers to HUD and changes helthbar and score.

The magic is- this completely similar signals and lines of code, but first works well, but second is broken and gave error.
Invalid call. Nonexistent function 'update_life' in base 'Nil'.

onready var hud = $HUD
func _on_enemy_died(_score):
	score += _score
	hud.update_score(score)
func _player_took_damage(_val):
	hud.update_life(_val)

To make it work, I changed path to direct way, like this:

func _player_took_damage(_val):
	$HUD.update_life(_val)

Error disappeared, but I still don't understand, how it works??

  • If that change fixed the problem, it means that the node HUD is not yet "ready" when that script's _ready() function is called, but the node HUD is ready when the _player_took_damage() function is called.

If that change fixed the problem, it means that the node HUD is not yet "ready" when that script's _ready() function is called, but the node HUD is ready when the _player_took_damage() function is called.

    You could declare an export var like this, and set it in the Inspector:
    export var hud: Node

    But if you want to do it completely from a script, you will need to use get_node() or its shortcut $ somewhere, or use load() or preload() to load a scene file (.tscn) and instance it as a node.