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