Godot Version
4.2.2
Question
Hello, I have a question about on body entered. I have a level designed and I only want the lifebar of the boss (HUD) element to show up once the character enters the area of the level where the boss is. To do this I set up an area 2D called Boss encounter and in the level script I put this code:
@onready var canvas_layer = $CanvasLayer
func _on_boss_encounter_body_entered(body):
if(body is hero):
print("entered boss area");
canvas_layer.visible = true;
else:
print("other things in area");
#pass
I thought this should work. If hero enter the collision area of âBoss Encounterâ and the character is hero which it most definitely is, then display HUD element. This thing is the level is loading up the Health bar at the start of the level and says that the âheroâ has already âentered the boss areaâ. And then where the hero enters the boss area it prints it again.
However, if I do this with the on_area_entered:
func _on_boss_encounter_area_entered(area):
var character = area.get_parent();
if (character is hero):
print("Hero character in boss");
canvas_layer.visible = true;
#pass
It seems to work fine.
So, what mysterious voodoo am I not understanding here correctly?
As always any help is greatly appreciated.
Regards.
HI!
I think for clearification you shouldmake 2 signals.
func _on_boss_encounter_body_entered(body):
if body is hero:
canvas_layer.visible = true
func _on_boss_encounter_body_exited(body):
if body is hero:
canvas_layer.visible = false
Then he exited the area, where sill be no error that hero has already entered.
1 Like
You also can use of low level Geometry2D methods to test if point inside
ignoring Z axis if world is plane
Geometry2D.is_point_in_polygon(point: Vector2, polygon: PackedVector2Array)
Not sure why your body_entered
connection triggers twice without some scene tree information. Maybe you could print or breakpoint to find out what is triggering the first call?
Your Hero has a Area2D child node that can trigger the area_entered
signal, the other body_entered
trigger node must not.
3 Likes
I would guess that your boss is added to the tree overlapping the hero, and then its position is moved. Making a node not visible does not cancel the area signals. As @gertkeno said, use a breakpoint to find out when it is triggered exactly. How do you create the boss? Do you instantiate, add_child and then set the position? If so set the position before adding it as a child.
The other possibility is that the boss is detecting himself. Check your layers and layer masks. (EDIT: Although I suppose the boss would have to be a hero class too so it probably isnât this).
2 Likes
Perhaps once the boss is activated you cannot just ârun awayâ and have the boss de-activate. It would depend on the mechanics of the game design.