+1 vote

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 :)

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

Oliver

in Engine by (42 points)

3 Answers

0 votes

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.

by (680 points)
0 votes

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 :/ 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.

by (715 points)
edited by

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

0 votes

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.

by (332 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 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 webmaster@godotengine.org with your username.