How do I get this health to work?

Godot 4.3

Hello! I’m working on an enemy health bar, but for some reason nothing happens when attacking them? Need some help.

func _ready() -> void:
	max_value = 3
	min_value = 0

# Called when the node enters the scene tree for the first time.
func _process(_delta: float) -> void:
	if value == 0:
		queue_free()


func _on_radius_area_entered(area: Area2D) -> void:
	if area.is_in_group("Owie 1"):
		value -= 1
	if area.is_in_group("Owie 2"):
		value -= 2

The health bar code itself seems okay, you need nothing more than setting the value to update the visual.
The issue if probably the _on_radius_area_entered not being called, or the two conditions inside returning false all the time, never leading to the value decrements.

Add some prints inside the function and conditions, something like this:

func _on_radius_area_entered(area: Area2D) -> void:
    print(area.is_in_group("Owie 1"))
    print(area.is_in_group("Owie 2"))

and I believe you will quickly see where there’s an issue. Then, do the appropriate fix (which should be fairly easy as it’s a very simple code, but feel free to ask otherwise!).

Nothing happened…not even in the syntax

If the prints are not showing up in the console, that means they’re not called, meaning the function _on_radius_area_entered itself is not called.
Have you connected that function to some signal, somewhere?

Well I figured something out that worked with the rest of my enemies.

I put [queue_free()] on the process in the health script, what I needed to do was put it in the main script.

but in doing so, the enemy just disappears the second it loads the debug, how do I fix this?

Really hard to say, what is the “main script”? What does your script look like now? Did you solve the connected function not printing?

In general I think you can avoid having a _process here in general, only queue_free when the health is lowered past zero.

i tried that, it didn’t make him disappear, only not die

extends ProgressBar

func _ready() -> void:
	max_value = 3
	min_value = 0

# Called when the node enters the scene tree for the first time.
func _process(_delta: float) -> void:
	pass


func _on_radius_area_entered(area: Area2D) -> void:
	if area.is_in_group("Owie 1"):
		value -= 1
	if area.is_in_group("Owie 2"):
		value -= 2
	print(area.is_in_group("Owie 1"))
	print(area.is_in_group("Owie 2"))


func _on_changed() -> void:
	if value == 0:
		$"..".queue_free()
func _on_radius_area_entered(area: Area2D) -> void:
	if area.is_in_group("Owie 1"):
		value -= 1
	if area.is_in_group("Owie 2"):
		value -= 2
	print(area.is_in_group("Owie 1")) # does this print anything?
	print(area.is_in_group("Owie 2")) # does this print anything?

	# check value here, as it's *after* the value has been changed
	if value <= 0: # make sure to check *less than* or equal to zero
		get_parent().queue_free()

# what is this function connected to? Seems unrelated
func _on_changed() -> void:
	if value == 0:
		$"..".queue_free()

It does not print anything except for “true”, “false” when I hit it.

I tried your code, and he just disappears again :T

Then it’s printing the value of if the area is within the specified groups. This may be easier to read:

print("In group owie1: ", area.is_in_group("Owie 1"))
print("In group owie2: ", area.is_in_group("Owie 2"))

Back to the question at hand, this is your initial post:

Now you say they disappear, which seems to me like an improvement; I would expect an attacked enemy to disappear.

What do you expect/want to happen versus what is really happening? Please be as descriptive as possible.

Well it disappear the second the game loads, so when I walk over to their location, it’s not there…I don’t know how this happens, all my other enemies have the exact same code and for some reason it’s not working with this one

How is this enemy different from all the others then? Are they using the same scene?

Sounds like your value starts at zero then, _on_changed probably triggers the first frame as data is loading it’s changing plenty of properties.

You reminded me to set the health to 3, thank you, it works now!