Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | CraftyManiac | |
Old Version | Published before Godot 3 was released. |
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
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
CraftyManiac | 2016-12-15 07:06