0 votes

Hey there, I'm trying to implement a resource bar. I need a resource lets call it energy. I've created a scene with type Node(named it Game) attached another scene to it called Player with a Node2d. Under that a Canvas Layer -> Control -> Progressbar.

I have created a script and attached it to Player (node2d) with setget for variables such as current_power, max_power with the intention of changing these values when an object takes away power, or an object increases max_power. What's the best way for the variables in Player to change the values in the Progress Bar node?

Do I make another script on the Progress Bar and call the get_current_power and get_max_power to adjust the values in func _process(delta). Or do I push the values from my Player script to the Progress bar? What would be the correct way and how would I go about doing that?

Also can the setget functions be called from outside of my Player scene? For example if I add more scenes to my Game scene, would those other nodes communicate?

This is what I have in my Player script:

extends Node2D

var current_power setget set_curr_power, get_curr_power
var max_power setget set_max_power, get_max_power 

func _ready():
    set_process(true)

func set_curr_power(newvalue):
    current_power = newvalue
func get_curr_power():
    return current_power

func set_max_power(newvalue):
    max_power = newvalue
func get_max_power():
    return max_power
in Engine by (30 points)
retagged by

Ok so this seems to work. except it's not really using the getters and setters at the moment.. I think that those will become useful if they work outside of my player node. The other problem is when I hit the space bar to increase the power levels it will continue past my min and max values set for the progress bar. I need to find a way to cap that.any thoughts?

extends Node2D

var current_power =75 setget set_curr_power,get_curr_power
var max_power = 100 setget set_max_power, get_max_power 

onready var progress = get_node("CanvasLayer/Control/ProgressBar")

func _ready():
    set_process(true)
    set_process_input(true)
    progress.set_max(max_power)

func _process(delta):
    progress.set_value(current_power)
    current_power -= delta * 2

func _input(event):
    if event.is_action_pressed("ui_accept"):
        current_power +=  5

func set_curr_power(newvalue):
    current_power = newvalue
func get_curr_power():
    return current_power

func set_max_power(newvalue):
    max_power = newvalue
func get_max_power():
    return max_power

1 Answer

0 votes

Ok so this code seems to do the trick.a couple of lines latter and I have added a cap to the progress bar. I still haven't answered the question about the setget functions if they will work outside of my Player node.

extends Node2D

var current_power =75 setget set_curr_power,get_curr_power
var max_power = 100 setget set_max_power, get_max_power 

onready var progress = get_node("CanvasLayer/Control/ProgressBar")

func _ready():
    set_process(true)
    set_process_input(true)
    progress.set_max(max_power)

func _process(delta):
    progress.set_value(current_power)
    if current_power > 0:
        current_power -= delta * 5
    print(current_power)

func _input(event):
    if event.is_action_pressed("ui_accept"):
        if current_power < max_power:
            current_power +=  5

func set_curr_power(newvalue):
    current_power = newvalue
func get_curr_power():
    return current_power
by (30 points)
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 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 webmaster@godotengine.org with your username.