Godot Version
Godot 4.3.steam
Question
So I have separate scenes for my player, UI and slime (the enemy). I have the following code in the Slime_Area2d
extends Area2D
@onready var player = get_tree().get_nodes_in_group("Player_Group").pop_front()
func _on_body_entered(body: Node2D) -> void:
print ("Contact")
player.adjust_health(-1)
print (player.Health)
I thought making a group was going overboard, but this was how someone showed me to call functions in different areas. Don’t judge me.
So this does indeed call the function in the player.gd file.
@onready var UI = get_tree().get_nodes_in_group("UI_Group").pop_front()
func adjust_health(amount):
Health += amount
if Health > MaxHealth:
Health = MaxHealth
UI.Health_Bar.value = Health
The problem comes at the last line. I get the error…
Invalid Access to property or key 'Health_Bar' on a base object of type 'Node2D'
This confuses me. From my previous programing experience, selected UI.Health_Bar (the name of the sub-node in UI) .value would be where I set the value of the health of the player. Also, why would I not have access to Health_Bar when it’s in the group?
The UI is in the group and it’s pulling all the subgroups into the array. Right? Isn’t that what the groups do?
Can someone please explain what is happening here?