how to get it put the number of coins

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

My HUD scene is made
canvasLayer2D (renamed HUD) the root node with the hud.gd (see below)
Panel
(with TextureRect which put an image of the coin on the screen)
(with X which is a label used to put an X on the screen)
Coin (which is RichTextLabel)

my hud.gd script
extends CanvasLayer

var coins = 0
var rng = RandomNumberGenerator.new()

func _ready():
$Coin.text = String(“coins”)

func _on_coin_collected():
coins = coins + 1
_ready()

func _physics_process(delta):
pass

func _on_QB_random_coins():
rng.randomize()
var random_coins = rng.randi_range(1,10)
coins = coins + random_coins
_ready()

in Godot 3.4.4. it work it
put then image of my coin an X followed by the total of coins
each time I added 1 to coins it would go from 1 to 2 to 3 etc…

but in Godot 4
its the word coins up (and does not change)

:bust_in_silhouette: Reply From: jgodfrey

That code can’t have worked exactly as posted, in Godot v3.x or Godot 4. The main issue is here:

$Coin.text = String("coins")

That’s where (I assume) you’re trying to get the current value of your coins variable to show in the HUD.

However, since you’ve wrapped the coins variable name in quotes, that’s just the string coins - which is why you only see the unchanging coins string in the HUD.

I assume you want this:

$Coin.text = String(coins)

Note, I’ve removed the quotes. That’ll take the value of the coins variable (an integer), convert it to a string variable, and display it in the HUD.

One more thing. You probably don’t want to call the _ready() function from your code. That’s intended to be called by the engine. If you need to call on some common code in both _ready() and other parts of the script, you should place that common code in a function and call that function from the places where you need it (including _ready())

MR jgodfrey

thank you for trying to help
it is now working in Godot 3.4.4
but not in Godot 4.0 Alpha 14[1]

rodell2020 | 2022-07-21 14:42

here is my coin.tscn/script

coins script

here is my HUD tscn enter image description here
here is my hud scriptenter image description here

rodell2020 | 2022-07-21 14:57

Those image links are broken. But, rather than posting images, just cut/paste the actual code. Just make sure you format it for display in forum. To do that:

  • Paste the code into your forum message
  • Select it
  • Press the { } button in the forum editor’s toolbar

Or, provide a link to the project that someone could take a look at.

I will say that I’m not using Godot 4 ATM, so I may not be much help there.

jgodfrey | 2022-07-21 15:51