I need help with a signal going from one scene to another

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

I am having some problems with my game. You see the thing is that I am making a game (like flappy bird) where the person needs to jump through the obstacles to get points. well the thing is that I am trying to get the current player score to appear and the high score also to appear. The thing is that I need to put the current score and the high score from the main game scene into another game scene (the game over scene). here is the code (parentheses have been added by me but are not part of code, except strings).

Main Game Script:

"extends Node2D

var body = alien
var game_over_scene = preload(“res://Scenes/GameOver.tscn”)

signal current_score(points)
signal high_score(high_points)

onready var alien = get_node(“Alien”)
onready var timer_replay = get_node(“Timer”)
onready var label = get_node(“First_Control/Control/Score”)

var points = 0
var state = 1

const PLAYING = 1
const LOST = 2

func _ready():
pass

func game_over():
var game_over_try = game_over_scene.instance()
get_tree().get_root().add_child(game_over_try)
get_tree().change_scene_to(game_over_scene)

func die():
print("Points before release ", points)
alien.apply_impulse(Vector2(0,0), Vector2(0,1000))
get_node(“BackgroundAnim”).stop()
state = LOST
timer_replay.start()
get_node(“Sounds/SoundHit”).play()

func _pointage():
points += 1
label.set_text(str(points))
get_node(“Sounds/SoundScore”).play()

func _on_Timer_timeout():
emit_signal(“current_score”, points)
game_over()"

and the game over scene script (it is attached to the label which will change its number):

"extends Label

var game

func _ready():
var scene = load(“res://Scenes/Game.tscn”)
game = scene.instance()
game.connect(“current_score”, self, “_on_current_score”)

func _on_current_score(points):
print(“Signal Received”)
set_text(points)"

Even though the label is supposed to change it does not change. Please help me. Thx

I noticed in the gameover() function there is an underscore, I’m not sure if removing it would help:

func gameover():
    var gameovertry = gameoverscene.instance()
    gettree().getroot().addchild(gameovertry)
    gettree().changesceneto(gameover_scene). 

→ change in last line to “gameoverscene”

Also, what happens when you go to the gameover scene? What do you see there vs what you expect?

friskybear | 2023-05-29 19:42

if you want your game score to go from your game scene to the game over scene using a autoloaded global script would be easier. You dont have to use signals here I think. Just save your score to a variable in the global script then in your game over scene script read it from there. I dont think your current setup will work.

Enfyna | 2023-05-29 20:00

Thank you for your response Enfyna but just a question. How do I create a global script. Do you mean like global variables? Sorry I am a beginner program trying to achieve my first actual quality game.

Awsomewolf2 | 2023-05-29 20:08

Thank you. I will check that out frisky.

Awsomewolf2 | 2023-05-29 20:08

You can read this about Singletons it might help: Singletons (Autoload) in Godot

friskybear | 2023-05-29 20:11

Singletons (AutoLoad) — Godot Engine (3.4) documentation in English
Yes a global script is where you can store your global variables. In this documentation it shows you how to create a global script and how you can change a scene with it in the Global.gd section. You only need to create a Global.gd as shown and add a variable like var score = 0 then you can access this variable on every script like Global.score. For example
In your game script you can increase the value like Global.score += 1

Enfyna | 2023-05-29 20:17

I actually need help on something which is that the _pointage function adds a point to the points variable. The only problem is that the other scene does not display the updated version after playing the game. Thx for your help.

Awsomewolf2 | 2023-05-29 20:52

Did you set up a global script with a points variable? If you did then in the pointage func doing a Global.points += 1 and in your label script setting the text as str(Global.points) should work. I dont see any reason that it wouldnt.

Enfyna | 2023-05-29 21:01

Thank you for all your help Enfyna. I have successfully fixed it.

Awsomewolf2 | 2023-05-29 21:23

:bust_in_silhouette: Reply From: klabri2104

extends Node2D

var alien
var gameoverscene = preload(“res://Scenes/GameOver.tscn”)

signal currentscore(points)
signal highscore(high_points)

onready var timerreplay = $Timer
onready var label = $First_Control/Control/Score

var points = 0
var state = 1

const PLAYING = 1
const LOST = 2

func _ready():
alien = $Alien

func gameover():
var gameovertry = gameoverscene.instance()
get_tree().get_root().add_child(gameovertry)
get_tree().change_scene_to(gameoverscene)

func die():
print("Points before release: ", points)
alien.apply_impulse(Vector2.ZERO, Vector2(0, 1000))
$BackgroundAnim.stop()
state = LOST
timerreplay.start()
$Sounds/SoundHit.play()

func pointage():
points += 1
label.text = str(points)
$Sounds/SoundScore.play()

func onTimertimeout():
emit_signal(“currentscore”, points)
gameover()


extends Label

func _ready():
var game = get_node(“/root/Game”)
game.connect(“currentscore”, self, “_on_current_score”)

func _on_current_score(points):
print(“Signal Received”)
text = str(points)

What is this answer doing? Can you please elaborate what you changed and why?

friskybear | 2023-05-29 20:30