How do I get a var shown on my game screen

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

Lets say I have a var Fuel for example , fuel is = 1000 , how do I show this var on screen , like how its printed in the console !? Any help is appreciated!

:bust_in_silhouette: Reply From: jgodfrey

Basically, you need to place a GUI control in your scene to display the score value. Likely, you’ll just need a Label control.

With that added. you can display a value in the control by setting the text property of the label.

So, to put your value of your fuel variable in the label, it’d look something like this:

$Label.text = str(fuel)

Depending on your game design (and scene layout), the trickier part might be in getting the value of fuel available to the script that has access to the new Label control. If the variable and the Label are part of the same scene, that’s pretty easy.

Otherwise, you’ll need to somehow pass the value from one place in your game to the other. That can be done via a signal or by directly calling a function or setting a property in some other script. The details of that will vary based on your game.

Okay I got it working, now my problem is this, my fuel basically goes down in decimals so it would be at 10 then show 10.9929 or w.e I just want the display to show the whole number not the decimal, my var is export (float) var fuel = 10 , I thought with it being a float it would show whole number what am I doing wrong

StsDevSquad | 2020-12-06 01:19

A float does indeed support real numbers (so, with a decimal value). If you only want to support whole numbers in your fuel value, it should be an int instead.

However, it’s no uncommon to use a float variable (as in your case), but display the value as an int. The easiest way to do that is:

$Label.text = "%d" % fuel

That’ll format the value as an integer (so, no decimal portion).

jgodfrey | 2020-12-06 01:43

So this is what I got set_text(String(“Fuel”) + " = " + String(fuel1.fuel)) , how would I implement it into this line of code, and my second problem which I cant solve for the life of me is adding a timer to how fast the fuel goes down heres my sample code

if Current_Gear== gears[1]:
yield(get_tree().create_timer(1.0), “timeout”)
fuel -= 0.5
grav_machine = null

how would I go about implementing the timer to the fuel reduction should I make a function for fuel reduction ?

StsDevSquad | 2020-12-06 01:50

The basics of the string formatting would be:

$Label.text = "Fuel = %d" % fuel

You should probably start a new question for that 2nd thing as it’s a completely different topic.

jgodfrey | 2020-12-06 02:19