I want to learn a simple way to constantly check a value and update an element accordingly

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

Hello all,

I’m really in love with this engine now, but my programming skills (I programmed Python ages ago) are not up to speed yet.
I’d really appreciate your help.

  1. I have created a global.gd file which is autoloaded
  2. It contains a variable for the score = 0
  3. When my character hits an enemy the score is increased by +1

… all works fine (I check all that stuff with print).

But how do I update the element on screen while the game is running?
I’m thinking of something like a “polling” mechanism… but I guess its even easier :slight_smile:

Thanks guys
I’ll be posting more about my game soon!

Oliver

:bust_in_silhouette: Reply From: genete

There are multiple solutions…

One of them maybe is to create a callback on that element you want to update (a Label for example) and connect it to a signal that is emitted when the score is increased (user signal). Then the callback should receive the new score or directly access to global.gd and to the updated score.

:bust_in_silhouette: Reply From: duke_meister

edit: changed my answer completely

edit 2: sigh. had to change it again because unfortunately setScore won’t be triggered when global.gd changes it :confused: Realised this because I’m scratching my head working on my own app that had this problem.


You could use setget in combination with a custom signal. At least this will trap everywhere that score is changed, so you only need to emit your signal once.

#global.gd
var score = 0 setget setScore

signal score_changed

# called when something **external** changes score    
func setScore():
    emit_signal("score_changed", score)

#game.gd
func ready():
    global.connect("score_changed", self, "on_score_changed")

func on_score_changed(s):
    get_node("Label").set_text(str(s))
func enemy_was_hit():
    update_score(1)

func player_was_hit():
    update_score(-1)

func update_score(amount):
    global.score += 1

Note: haven’t executed this.

Thanks for the insight.
Unfortunately my game crashes.
Let me look at the side scroller demo, it has a scor counter in it!

oliverruehl | 2016-05-20 12:44

:bust_in_silhouette: Reply From: jackmakesthings

There are a few ways to go about this. To do it “polling-style”, you could have something on your Label’s process or fixed process callback, a la:

# label script

func _fixed_process(delta):
  if get_text() != str(global.score):
    set_text(str(global.score))

I’m not sure if this would get inefficient over time, however. It might.

I’d encourage you to keep playing around with signals - that’s probably a better approach. Something like this:

# global.gd

extends Node

var score
signal score_updated(score)

func update_score(score):
  self.score = score
 emit_signal("score_changed", score)

# label script

extends Label

func on_score_change(score):
  set_text(str(score))

Then you just have to connect global.score_changed with label.on_score_change, from either script or via the editor.