Connecting signal from another scene

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

Hi, I have a main scene, player scene and npc scene.

I want to connect Area2d body entered to the player in main scene, so that he can interact with him and start a dialogue.

How can I do this? I tried a couple of times but it didnt work.

What node type is the player or the NPC scene? Code snippets?

Dlean Jeans | 2019-06-17 13:04

:bust_in_silhouette: Reply From: soulldev

You can add Player to a Global Script:
1- Make a new script and name it Global.gd
2- In Global.gd, make a new variable and call it Player, so your Global.gd will look like:

extends Node
var Player

3- Go to Project->Project Settings->Auto Load and add Global.gd to the list

3- In your Player’s _ready() function, add :

Global.Player = self

Now, just connect the body_entered signal to your current node script and within your current node script, call the Player function you want, like:

_on_Area2D_body_entered(body)
    Global.Player.my_func(body)

Or you can use the connect method:
In your current node ready function, add:

connect("body_entered", Global.Player, "my_func", [body])

thanks, that was really helpful, but I get an error:
Parser Error: Identifier not found: body through the connect method

szaross | 2019-06-17 15:06