Log Milk
Log Milk

Reputation: 3

What is the ideal way to change the value of a progress bar with on-screen button press (Godot)

I'm new to coding and Godot and I need help with changing the value of my progress bar from the script inside my button node. The error I get is 'get_node: (Node not found: "/root/ProgressBar" (absolute path attempted from "/root/Node2D/Button").)' I want every time the button is '_pressed()' to increment health by 10 which would change 'value' in ProgressBar to that value.

extends Button

var health= 0

func _ready():
    pass

func _process(delta):
    pass

func _pressed():
    health += 10
    print(health)
    get_node("/root/ProgressBar").set_value(health)

Upvotes: 0

Views: 6147

Answers (2)

Theraot
Theraot

Reputation: 40295

The path /root/ProgressBar is unlikely to be correct. Usually, as a child of root, you would have the current scene, and as child of that something else (So the path could be something like /root/MySceneName/ProgressBar). And relative paths work too… Usually, we would solve this by means of figuring out the correct path…

But things have changed (For Godot 3.5 and newer). What you are going to do is open the script while the scene is open, and then drag - with CTRL pressed - the Control you want to access (i.e. the ProgressBar) on script, outside of any method (func).

By doing that Godot will generate a line of code that looks something like this:

onready var progress_bar: ProgressBar = $"Path/To/ProgressBar"

In other words, Godot will figure out the path you need, and set up a variable you can use. Godot will set the variable to the Node you dragged as part of the initialization of you script. Assuming all goes well.

You might get an error saying that the script is not used in the scene. Perhaps you have the wrong script open, or you use the script in multiple places and Godot got confused… Either way, close the script and open it from the scene, and it should work.

And, of course, you can use the variable to set its properties, for example:

progress_bar.value = health

You might also be interested in scene unique names. On the contextual menu of the Nodes in the Scene panel you can select "% Access As Scene Unique Name", this will allow you to access the Node with this syntax:

$"%ProgressBar"

Regardless of where it is in the scene tree. Except, be aware that the name must be unique for that scene (you won't be able to do that with two Nodes that have the same name).

This has the advantage that if later you change where the Node is in the scene tree (for example to add some Container to organize your Controls), you don't have to update the path to where you use it. Instead it should continue to work, as long as you don't change the name.

Let that be yet another reason to pick good Node names.

And yes, dragging the Node to the script also works with these.

Upvotes: 1

Bugfish
Bugfish

Reputation: 1729

The problem with your path is, that your missing the name of your main node in it. So let's say your node tree looks like this:

  • MainNode
    • ProgressBar
    • Button

To get the ProgressBar from anywhere you would need to use get_node("/root/MainNode/ProgressBar")

Upvotes: 0

Related Questions