Overview:
As per the full code when the Area2D
enters the enemy body
it signals:
func on_Sword_body_entered(body):
if body.name == "PlayerTwo":
is_in_body = true
print(str(isinbody, "inbody"))
Which sets the is_in_body
bool to true
and prints this information (giving a false positive diagnostic), but another function is signaled when the Area2D
"exits" — and for whatever reason this signal
is emitted immediately:
func on_Sword_body_exited(body):
if body.name == "PlayerTwo":
is_in_body = false
Thus by the time _physics_process
starts processing its code the is_in_body
bool has already been set to false
.
func _physics_process(delta):
if Input.is_action_just_pressed("attack") and can_attack == true:
$AnimationPlayer.play("swing")
can_attack = false
attacktimer.start()
swingtimer.start()
if $AnimationPlayer.currentanimation == "swing":
is_attacking = true
print(str(isattacking, "attacking"))
if is_attacking == true and is_in_body == true:
print("attacksuccessful")
get_node("/root/TestLevel/CanvasLayer/FightUI/Player2Health").value -= 20
Solution:
The end solution as we finally found was to put the is_in_body = false
into the final attacking branch: if is_attacking == true
, and delete the _body_exited
signal.
This made it so the player couldn't spam multiple attacks if they hit whilst also fixing the issue at hand. Final revision:
if is_attacking == true and is_in_body == true:
is_in_body = false
print("attacksuccessful")
get_node("/root/TestLevel/CanvasLayer/FightUI/Player2Health").value -= 20
Actual Issue:
Unknown — but it's possible that the incorrect triggering of the exit signal
function is an engine bug, or perhaps the circular CollisionShapes of the Sword and the Enemy combined is causing the geometry to somehow register as both entering and exiting simultaneously.
If you need to use the _body_exited
collider signal for something else, try using rectangular or polygon shapes instead of circles.
Anyway, good luck and have fun with the rest of your project!