Setting new value in label text of global variable

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

Based of my first question
https://forum.godotengine.org/121992/using-global-variables-autoload

Script ‘GlobalVariables’ with global variables

extends Control #Interface Player
#PLAYER BASIC ATTRIBUTES (F.E. NO CLASS DEFINED)
var PlayerLevel	= 1			# Shows the players current level.
var PlayerEXP = 0			# Shows the players current experience.
var PlayerHealth = 40		# Shows the players health.
var PlayerMana = 40		# Shows the players mana.

These variables are basically for being changed - means, that they will rise/fall with certain events. So I tried to increase the player’s level by one by pressing a fixed key defined by a InputEventKey

extends Label

#DEBUG COMMANDS
func _unhandled_input(event):
	if event is InputEventKey:
		if event.pressed and event.scancode == KEY_KP_0:
			GlobalVariables.PlayerLevel += 1
			print("+ 1 Level to PlayerLevel")

The print command get displayed in the console - basically works - but is not changed in the label itself.

I assume that there is a setget function required, to update the latest to the newest int. Is that right?

How do I set up the setget function (which will be required also for item amounts, that withdraw or adds)? I guess, that you are checking the current int in the label and setting it to the latest - but how do you do that?

Many thanks in advance!

:bust_in_silhouette: Reply From: Fyrion

I found the resolution as it is quite near.

You have to change function

func _unhandled_input(event):
if event is InputEventKey:
    if event.pressed and event.scancode == KEY_KP_0:
        GlobalVariables.PlayerLevel += 1
        print("+ 1 Level to PlayerLevel")

As function to func _process(delta) which gets called every frame to update the values defined. If you use the func _unhandled_input this function musted be called separated to update this value. So there are two ways of updating the value.

:bust_in_silhouette: Reply From: ponponyaya

If you don’t mind to use custom signal.
Yes, you can use setget function to emit a signal, then you can use this signal to update your Label text.

for example:

[In global variables script]

signal updatePlayerLevel # custom signal

var PlayerLevel = 1 setget set_PlayerLevel
...(skip)

func set_PlayerLevel(value):
	PlayerLevel += value
	emit_signal("updatePlayerLevel")

[In Label script]

extends Label

...(skip)

func_ready():
	GlobalVariables.connect("updatePlayerLevel", self, "_on_updatePlayerLevel")

...(skip)

func updatePlayerLevel():
	text = str(GlobalVariables.PlayerLevel)