The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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!

in Engine by (28 points)

1 Answer

+1 vote

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.

by (22,674 points)

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

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

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 CurrentGear== gears[1]:
yield(get
tree().createtimer(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 ?

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.

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 Frequently asked questions and 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 [email protected] with your username.