Trying to access and change via groups values of elements

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

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?

If HealthBar is the name of a child node of UI but is not declared as a property in the UI script, then you need to call UI.get_node(“HealthBar”) to access the HealthBar

How would I do that? I have no UI script, but would doing that allow me to code it like I was expecting?

Now when I hit that call for adjust_health, it’s saying…

Invalid Call. Nonexistent function 'adjust_health' in base 'Node2D'

I tried changing the call to CharacterBody2D, but that didn’t help.

func _on_body_entered(body: CharacterBody2D) -> void:
	print ("Contact")
	player.adjust_health(-1)

EDIT: Nevermind. I saw that this was extending Area2D. So I changed it to that and it’s at least not erroring. visibilly, but now I get

emit_signalp: Error calling from signal 'body_entered' to callable: 'Area2D(slime_area.gd)::_on_body_entered': Cannot convert argument 1 from Object to Object.