Where should I connect the signal?

Godot Version

4.2.1

Question

Hello everybody! I’m making a block trap that disappears after a while, it’s fine along the way, I can’t figure out where to send the signal, to the block itself or to the player? I sent back and forth, but nothing worked.

extends RigidBody2D


# Time to disappear in seconds
var disappear_time: float = 3.0

func _on_body_entered(body: Node) -> void:
	# Start the timer to disappear
	await get_tree().create_timer(disappear_time).timeout
	# Deleting the node
	queue_free()

func _ready() -> void:
	# # Subscribe to the body_centered signal
	self.body_entered.connect(_on_body_entered)

Define “class_name Player” and “block” function in the player script then…

Try this:

extends RigidBody2D

# Time to disappear in seconds
var disappear_time: float = 3.0

func _on_body_entered(body: Node) -> void:
   if body is Player:
	   # Start the timer to disappear
	   await get_tree().create_timer(disappear_time).timeout

      #block player
      player.block()
	  # Deleting the node
	   queue_free()

func _ready() -> void:
	# # Subscribe to the body_centered signal
	self.body_entered.connect(_on_body_entered)

It means when player enter the area, it will block player and delete itself after disappear_time. I do not think you need to any signal for this purpuse, but if you want I can show it.


I got a lot of errors, and what would “Define “class_name Player” and “block” the function in the player script, and then…”?

Press CTRL + S, to fix it. or try to arrange it in proper tabs ( by deleting the spaces)
and add this code after extends, in player script:

class_name Player

and normally create a function namely “block”, in it.