0 votes

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 onbodyentered 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()
            thorn.position = $Position2D.global_position
            shooting = true

Sample of Thorn code:

signal Shot_Enemy

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

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

asked Jan 25 in Engine by Dragon11705 (29 points)

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.

1 Answer

0 votes

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():

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.

answered Jan 25 by potoroo84 (16 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.