The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

Based of my first question
https://godotengine.org/qa/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!

Godot version 3.3.4 stable
in Engine by (30 points)

2 Answers

0 votes

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 _unhandledinput this function musted be called separated to update this value. So there are two ways of updating the value.

by (30 points)
0 votes

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)
by (526 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.