Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | inTech | |
Old Version | Published before Godot 3 was released. |
Hey everyone
I read about signal and I find the concept to be interesting and a good thing to add into my game. I read the tutorial here and followed the code snippet and implant it inside my project.
Instead of giving all my codes (which is a mess), I am just going to show part of the code.
A Label node that will appear when the user pause or lose a game.
extends Label
signal restart_game;
func _ready():
set_process_input(true);
func _input(event):
if(event.is_action("ui_accept") && get_tree().is_paused()):
emit_signal("restart_game");
get_tree().set_pause(false);
KinematicBody2D node - player
extends KinematicBody2D
var startX = get_pos().x;
var startY = get_pos().y;
func _ready():
set_process_input(true);
connect("restart_game", self, "restartGame");
func restartGame():
print("Restarting game ... ");
set_pos(Vector2(startX, startY));
With this, I am expected to see the player node go back to its original position. I am also expected to see “Restarting game …” in the debugging console but there isn’t.
The Label node is inside another scene where it is instanced inside the main scene. The KinematicBody2D node is also inside yet another scene where it is also instanced inside the main scene. Which mean there is a total of three scenes - level.tscn, player.tscn and pause.tscn.
So I thought the problem might be with the logic of my program so I try to run the following code and I expect an endless prints, but there is nothing in the debugging console.
extends Label
signal restart_game;
func _ready():
set_process(true);
set_process_input(true);
func _process(delta):
emit_signal("restart_game");
func _input(event):
if(event.is_action("ui_accept") && get_tree().is_paused()):
get_tree().set_pause(false);
Edited: After some testing, it seem that the emit_signal function works with the following code.
extends Label
signal restart_game;
func _ready():
set_process(true);
set_process_input(true);
connect("restart_game", self, "restartGame");
func _process(delta):
emit_signal("restart_game");
func _input(event):
if(event.is_action("ui_accept") && get_tree().is_paused()):
get_tree().set_pause(false);
func restartGame():
print("restarting game ... ");
This mean that the emit_signal() function is working as intended but somehow my KinematicBody2D node from another scene cannot receive the signal.
I think that the problem fall within the “connect(“restart_game”, self, “restartGame”);” code. To be more specific, the second argument. I can’t find any documents about connect() function though.