How can you check if a body has entered an instance of an area2d node?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dragon11705

So I am making a 2d platformer game where the player shoots thorns that kill enemies on contact. I am preloading the thorn scene from the player script and having it make an instance of the thorn. The thorn then moves via its own script, but I have a problem. I need it to detect when a body in the group ‘Enemy’ (enemies) enters the area2d, but I don’t know how to connect the area2d’s on_body_entered signal to the player script since the only way I know to connect it is via the inspector signal list which is only available in the thorn scene. There is no thorn in the same scene as the player until an instance is created, so is there a way to connect a signal of an instance or node from another scene to the player script? Here is some more information:
Sample of Player code:

   var Thorn = preload('res://Scenes/Thorn.tscn')
    func _physics_process(delta):
    	if Input.is_action_just_pressed("Shoot") and !shooting:
    		var thorn = Thorn.instance()
    		get_parent().add_child(thorn)
    		thorn.position = $Position2D.global_position
    		shooting = true
    		$ShootTimer.start()

Sample of Thorn code:

signal Shot_Enemy

func _on_Thorn_body_entered(body):
if body.is_in_group('Enemy'):
	emit_signal('Shot_Enemy')

What I need to do is somehow connect the ‘Shot_Enemy’ signal of Thorn to the player or enemy script.

Nevermind it took me a while but I found a solution. I just have a variable set to true in the thorn script when it hits an enemy, and then check if the variable is true from the player script.

Dragon11705 | 2020-01-25 19:24

:bust_in_silhouette: Reply From: potoroo84

You could use the connect() function, which is basically like connecting signals but on code.

What i’d do is on the Thorn script, under the _ready() function, write the connect() signal

func _ready():
  connect('Shot_Enemy', get_parent(), '_on_Thorn_Shot_Enemy')

Then write the function on the player script:

func _on_Thorn_Shot_Enemy():
  #code

so whenever the Thorn is created, it connects the ‘Shot_Enemy’ signal to its parent, which in this case should be the player.

It’s not the best solution… (It only works if the player is the only parent of Thorn) but i used it many times, and it shouldn’t affect the Thorn scene.