Problems with repeated enemy attacks

Godot Version

4.2

Question

I greet everyone and write again with the hope that someone will help. I wrote this code
func _on_attack_body_entered(body: Node3D) → void:
animation_player.play(“attack”)
if body.is_in_group(“igrok”) && body.has_method(“hurt”):
body.hurt(hit_point)
print(“hite”)
emit_signal(“hurt”)
So that the enemy would attack the hero during the course of the attack zone. However, how to make sure that he continues to attack while the character is inside the zone. I kind of tried to create a timer, but it still attacked only during the course of 1 time. , I don’t have any ideas thanks in advance.

Set a flag in the _on_attack_body_entered and then poll that in _process

func _process(delta):
   if amAttacking: foo()

Also setup the body_exited to reset the flag.

Thanks for the answer, but if I write in the process function, then I can’t add a body. And how to replace the body in this process, I don’t even know

  • I tried to write a little differently without body, but it still throws the invalid call. nonexistent function hurt in base float error

I tested that on just print(“hite”) and yes, in general everything works. How can I send my signal now if it says that it is missing from the database for all options

make boolean variables and save the current target body, like

var can_attack:bool=false
var is_attacking:bool=false
var target_body=null

then in _physics_process function

func _physics_process(delta):
	if can_attack:
		if not is_attacking:
			is_attacking=true
			animation_player.play(“attack”)
			if target_body:
				if target_body.is_in_group(“igrok”) && target_body.has_method(“hurt”):
					target_body.hurt(hit_point)
					print(“hite”)
					emit_signal(“hurt”)
			await get_tree().create_timer(animation_player.get_current_animation_length()).timeout
			is_attacking=false

then in
func _on_attack_body_entered(body: Node3D) → void: function:

func _on_attack_body_entered(body: Node3D) → void:
	target_body=body
	can_attack=true
	

will need to handle when body exited too

func _on_attack_body_exited(body: Node3D) → void:
	can_attack=false
	target_body=null

the entered and exited body methods will probably need to check if the body is the player or not first before assign it to target_body variable

1 Like

Yes, thank you very much, I realized that I should have added var target_body=null. But it still throws the error nonexistent function “hurt” in base Nill

does your player has hurt method? which doesnt make sense, because the condition already check if there’s hurt method

this probably the case too, there are other bodies also entered enemy’s attack collision. not just player

I completely copied your method and yes everything works, so I did not understand something again, thank you very much, I will figure it out

1 Like

I did everything in general and everything works, but I found an error that if another object of the RigidBody3D type is in the attack zone during the attack and the player leaves the attack zone, a crash occurs with the error nonexistent function is in group. there are some tips on what to do . I seem to have written if target_body.is_in_group(“igrok”) && target_body everywhere.has_method(“hurt”)

I apologize for the trouble I fixed, I deleted at the exit if target_body.is_in_group(“igrok") && target_body everywhere.has_method(“hurt”) . Thanks again

1 Like