How to set a variable at start of the game to display starting value

Godot Version

4.2.2

Question

Hi there! I’m learning Godot as I’m new to making games and just trying to learn more with the scripting. I’d like to know how to display a variable at the very start of the game before it starts changing. Currently I use a label that is just text with the starting number as just plain text (Entering this in the Text box for the label in Inspector).

I would not be surprised if I’m over complicating this, but still, I’d like to know in general how I’d do this.

My game has a coin variable that I set to 0 in my coin script and this coin script has a function to add +1 to the variable when a coin is collided with by the player. For the HUD, I have a Node that then has a child node for CanvasLayer, then within that there’s a child nod for Control, and then a child node for Label.

image

And then I have a script that tied to the HUD Display node that has the function for increasing the variable by 1 via a function called add_coin(): and based on the value of the coin it displays the coin number as well as setting font color

When a coin is collided with a player, there’s a script it. The coin is set as an Area2D node. The coin script is set to where the HUD Display node is set as an @onready variable and then the function for _on_body_entered(): there’s a print for noting coin was got and then run the add_coin() function in the HUD Display script.

So - for the HUD Display, the label at the start of the game is “Coins: 0” as just text that is set in the Inspector tab for the Label node.

Instead of just having the label start with all text for “Coins: 0”, how do I get it where it always starts with the what the coin variable is set to? Currently when I try to put coin_count.text = "Coins: " + str(coincount) before the function, I get an error about “Unexpected Identifier” in class body. I’ve tried If/Elif/Else statement with the HUD Display script and that shows the 0 value, but then doesn’t update until the value goes to 2 (not sure why this is a thing either).

Is there a way or a function that runs once when the game is started where it sets my variable to 0, and then the label is able to be what I have when that variable is increased?

Or is it best practice to pre-define the label like I have currently and just deal with it?

The easiest that I could think of is doing something like this

var coincount = 0 :
	set(val):
		coincount = val
		coin_count.text = str(coincount)

@onready var coin_count = $CanvasLayer/Control/CoinCount

If you want to have your variable do something else when its value changes, then you can use getter and setters.

This way you can just forget about the label, knowing that when you set your coincount will also set the HUD text

1 Like

Thanks for the assistance! So what is set(val): and the cointcount = val doing? I don’t see it used after you set(val) so I’m just trying to understand it as I may need to use the coin CoinCount in other scenes (like a menu that would also show this data)?

Sorry for asking a lot - I just want a bit more understanding so I know I’m using it correctly and don’t have to hopefully come back to ask more questions about it.

Where can I read about “getter and setters”? I’m guess you mean stuff like get_node() and what you provide with set(val). I was following a tutorial on something similar to this where they used get_node() but when I try using it, I constantly get debug errors.

You can read about getters and setters in the official documentation (I’ve linked the relevant section of the page, but in case your browser just opens to the top of the page, scroll down to Properties (setters and getters)).

The set(val) block is code that automatically gets called whenever anyone tries to set the coincount variable - instead of setting it directly, it calls the set function on the new value.

1 Like

This isn’t working on what I’m looking for here. The HUD doesn’t update until a coin is picked up and then when the 2nd coin is picked up, it actually makes the label disappear.

However - if I’m understanding correctly - if I want an effect to happen what the value changes, I’d still want to use set/get going forward instead of basic if coincount is <variable> then <do this>?

I also tried setting this on main scene for the game under the func _ready(): but it’s just not starting. So maybe pre-setting text is what I have to do?

Coin_not_showing_at_start

I just ended up cheating with my test game - attached a script to the RichTextLabel and used a global variable to hold the value

func _process(delta):
	text = ("Score " + str(global.score))

What am I missing here with the RichTextLabel? The text still doesn’t show at the start before a coin is picked up.

extends RichTextLabel

var coinnumber = 0
@onready var coin_number = $"."

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
	coin_number = ("Coins: " + str(coinnumber))
1 Like

Using coin_number = is just setting a variable.

I’m using text = as that adjusts the text box as per the right hand side/Inspector panel.

Here’s the ammo display from another game:

func _process(delta):
	text = ("Ammo " + str(global.magazine) + "\n" + "Bombs " + str(global.bombs))

I’m also using global variables to make things easier to manage

1 Like
  1. Create autoload node called global.
  2. add var coin = 0 in the global script.

Is there a way or a function that runs once when the game is started where it sets my variable to 0

if your scene is level1.tscn, override the _enter_tree() function and set global.coin = 0
5.

and then the label is able to be what I have when that variable is increased?
at _ready() and _on_body_entered() put the code:
$CanvasLayer/Control/CoinCount.text = str(global.coin) to update the lael

I created a signpost with text yesterday - just in case this code helps.

extends Sprite2D

var player_close = false

func _ready():
	pass # Replace with function body.

func _process(delta):
	if player_close == true and Input.is_action_just_pressed("action"):
		$RichTextLabel.text = "Welcome to the Unchanted Forest"
		await get_tree().create_timer(2).timeout 
		$RichTextLabel.text = "Where none of your dreams come true"
		await get_tree().create_timer(2).timeout 
		$RichTextLabel.text = ""
			
func _on_area_2d_body_entered(body):
	if body.name =="Player":
		player_close = true
		
func _on_area_2d_body_exited(body):
	if body.name =="Player":
		player_close = false

Yes but you have an action to show something on press. What do you have set for the sign text at the start if you’re not pre-filling the Text attribute in Inspector?

It’s empty - but if I take out the if statement at the start it would print continually.

It’s a very simple scene.

Screenshot from 2024-08-08 09-27-08

I finally found a tutorial that covered this. You were right pdhales2 and mcoswel - I learned more about Global variables and once I set up a designated script for Autoload in Project Settings, I was able to get the text to display.

1 Like

That’s great news. Keep up the good work. :smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.