You need to fetch the Player
instance first, and connect its signal into WarpAtBoundaries
instance:
onready var player = load("Player.gd").new()
func _ready():
player.connect("player_moved", self, "_on_player_moved")
self
is WarpAtBoundaries
instance, and because it doesn't define player_moved
signal, you get the described error.
As you stated, you have your Start
scene which acts as a main scene I suppose. If you already instance a player in that scene:
onready var player = $player # or get_node("player") in Godot 2
Or in a singleton like Characters
:
const Player = preload("player.gd")
var player = Player.new()
And then in your Start
scene:
Characters.player.connect("player_moved", self, "_on_player_moved")
Depending on whether the character can be removed from the scene, you need to connect the signal dynamically if you want to add new player. For that, the SceneTree
(fetched with get_tree()
) has node_added(node)
signal which can be connected to your Start
scene like this:
func _ready():
get_tree().connect("node_added", self, "_on_node_added")
func _on_node_added(node):
if node is Characters.Player:
var player = node
player.connect("player_moved", WarpAtBoundaries, "_on_player_moved")
The reference to the player will be always kept in the singleton unless you explicitly queue_free()
it or replace it with the new player.
This might be over-complicated but it really depends how big your game is going to be.