Update Score-Label (with Singleton) on_button_pressed

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

Hi!

I created a Score-Singleton which updates score-labels using signals when 4 different texture-buttons are pressed. When running the scene, this is exactly what the Score-labels (BeziehungScore, KontrolleScore and UnterrichtScore) are doing (in the background), but the updated values won’t show up in the Score-label instance. In the end, this is not a problem at all, since I only want to show the final score (= current values of the Score-labels) in an end-screen (like a high score screen). But I’m really curious why the labels are not visible when running the scene. Any ideas?

Main Scene:

extends Control

const score = preload(“res://Score.tscn”)

onready var handwriting_sound = $HandwritingSound
onready var main_option1 = $MainOption1
onready var main_option2 = $MainOption2
onready var main_option3 = $MainOption3
onready var main_option4 = $MainOption4

var Beziehung_score = 0
var Kontrolle_score = 0
var Unterricht_score = 0

func _ready():
var show_score = score.instance()
self.add_child(show_score)

Score.connect("update_BeziehungScore", self, "_update_Beziehung_score")
Score.connect("update_KontrolleScore", self, "_update_Kontrolle_score")
Score.connect("update_UnterrichtScore", self, "_update_Unterricht_score")

func _on_HandwritingSound_finished():
handwriting_sound.stop()

func _on_SchoolBellSound_finished():
main_option1.show()
main_option2.grab_focus()
main_option2.show()
main_option3.show()
main_option4.show()

func _on_MainOption1_pressed():
Score.emit_signal(“update_UnterrichtScore”, 10) # +10 UnterrichtScore (Pünktlichkeit)
print(Unterricht_score)

func _on_MainOption2_pressed():
Score.emit_signal(“update_KontrolleScore”, -10) # -10 KontrolleScore
print(Kontrolle_score)

func _on_MainOption3_pressed():
Score.emit_signal(“update_BeziehungScore”, 10) # +10 BeziehungScore, - 10 Unterricht
Score.emit_signal(“update_UnterrichtScore”, -10)

func _on_MainOption4_pressed():
Score.emit_signal(“update_KontrolleScore”, 10) # +10 KontrolleScore , -10 Unterricht
Score.emit_signal(“update_UnterrichtScore”, -10)

func _update_Beziehung_score(Beziehung_points):
Beziehung_score += Beziehung_points
Score.Beziehung_score.text = str(Beziehung_score)

func _update_Kontrolle_score(Kontrolle_points):
Kontrolle_score += Kontrolle_points
Score.Kontrolle_score.text = str(Kontrolle_score)

func _update_Unterricht_score(Unterricht_points):
Unterricht_score += Unterricht_points
Score.Unterricht_score.text = str(Unterricht_score)

Score-Singleton:

extends Control

onready var Beziehung_score = $Beziehung/BeziehungScore
onready var Kontrolle_score = $Kontrolle/KontrolleScore
onready var Unterricht_score = $Unterricht/UnterrichtScore

signal update_BeziehungScore(Beziehung_points)
signal update_KontrolleScore(Kontrolle_points)
signal update_UnterrichtScore(Unterricht_points)

The buttons are TextureButtons btw. I’ve tested “normal” buttons in another project with a similar code, and there, the values are updating on screen.

cadam | 2020-11-29 01:41

I get the following errors for the lines:

Score.connect(“update_BeziehungScore”, self, “_update_Beziehung_score”)
Score.connect(“update_KontrolleScore”, self, “_update_Kontrolle_score”)
Score.connect(“update_UnterrichtScore”, self, “_update_Unterricht_score”)

W 0:00:00.540 The function ‘connect()’ returns a value, but this value is never used.
<C+±Fehler> RETURN_VALUE_DISCARDED
Main.gd:19

Is there something I miss with the signals?

cadam | 2020-11-29 12:36

Ok. I’ve found out that my background-image (a TextureRect-node) is blocking the autoload-singleton from showing in the scene. Isn’t that strange?
Is there a workaround, or how can I prevent this from happening? :slight_smile:

cadam | 2020-11-30 11:44

And now I’ve found the solution. I created a Canvas Layer, put the TextureRect as a child of Canvas Layer, and changed the Layer-index to -2.
Solved! :slight_smile:

cadam | 2020-11-30 12:11