I can not create my own signals

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PerduGames
:warning: Old Version Published before Godot 3 was released.

I can not create my own signals, I’m doing so, I have a script called “global.gd” which is a singleton created there in autoLoad:

extends Node

var score = 150 setget setScore
signal changeScore

func _ready():
	pass
		
func setScore(_score):
	score = _score
	emit_signal("changeScore")

I created a label and a script embedded in it like this:

extends Label

func _ready():
	global.connect("changeScore", self, "_on_changeScore")
	
func _on_changeScore():
	set_text(str(global.score))

But the text in the Label does not change, I already tested in the script of the label putting:

func _ready():
        set_text(str(global.score))
	global.connect("changeScore", self, "_on_changeScore")

And it’s okay so far, I believe it’s the wrong signal, any idea what it might be?

:bust_in_silhouette: Reply From: YeOldeDM

Where is the code where you’re setting global.score to a new value? I only see where you’re getting the variable. The setter will only be called when you set the variable.

I start it at 150, however it does not change the value in the Label during the game.

PerduGames | 2017-07-14 20:48

TIP: If it works better for you, you can have your scoreChanged signal pass the score along with it. Then your label can set its text without having to look outside its own scope.

extends Node
signal scoreChanged( to )

func setScore( _score ):
    score = _score
    emit_signal( "scoreChanged", score )

And on the receiving end:

extends Label

func _ready():
    global.connect("scoreChanged", self, "_on_scoreChanged")
    global.score = global.score  #kickstart the setter
func _on_scoreChanged( to ):
    set_text( str( to ) )

YeOldeDM | 2017-07-14 20:53

I realized that it is “global.score = global.score” that makes it work in both ways, why do I have to assign it? If it already starts at 150?

PerduGames | 2017-07-14 20:58