Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | PIZZA_ALERT |
TLDR; is there a line of code that can allow me to call a variable from an instance in a different scene (avoiding global if possible)?
I have a scene called “Stats.” This scene is just a single blank node with an attached script, also called “Stats,” which houses variables such as “current_health” and “max_health” as well as signals like “health_changed” and “no_health” and functions like “set_health.” The idea with this is that if I have multiple kinds of scenes in my game needing common stats with different values at different times (like enemy health and player health), I can have 1 scene + script that manages all the base stats and just slap that scene into these other scenes as a child instance, and adjust a slider or two in the Inspector menus. I hope this makes sense. Please let me know if not.
I have a scene called “Player” which is a KinematicBody2D node with an attached script also called “Player” and a bunch of child nodes under it which aren’t relevant here.
I have a scene called “HealthUI.” This scene is a control node + attached script also called “HealthUI” and a child label node which I want to fill up with text reading “HP = XXXX” (where the X’s are the actual numeric health value of the player). The idea with this scene is to have a simple HUD/UI scene available for pasting into any room I want it in. This is a very ugly and basic way of displaying health, but it’s just to prototype for the time being.
I have a scene called “World” which is just my basic first room that I’ve made with the “Player” and enemies to run around in. “HealthUI” and “Player” are children scenes within this “World” scene.
I want the “XXXX” value inside the “HealthUI” scene to be a variable called “current_hearts,” a numeric value equal to the variable “current_health” in the “Stats” instance within the “Player” scene’s script. I want this to work regardless of whatever scene I put “HealthUI” into. I know about global variables and all that, but, if possible, I would just like to be able to be consistent with how my Stats variables are used and called thruout the game whether I am referring to Player stats, Enemy Stats, or Hell, maybe there’s a door I gotta beat down at some point and that has stats.
So, same as the TLDR above, is there a line of code that can allow me to call a variable from an instance in a different scene?
What I have right now:
Stats (parts relevant to health):
# Variables
export(int) var max_health = 1
onready var current_health = max_health setget set_health
# Functions
signal no_health
signal health_changed(value)
func set_health(value):
current_health = value
emit_signal("health_changed", current_health)
if current_health <= 0:
emit_signal("no_health")
Player (parts relevant to health):
func _ready():
animationTree.active = true
swordHitbox.knockback_vector = Vector2.DOWN
stats.connect("no_health", self, "queue_free")
func _on_Hurtbox_area_entered(area):
stats.current_health -= area.damage
tween.interpolate_property(sprite, "modulate",
Color(1,0,0), Color(1,1,1),
Tween.TRANS_QUAD, Tween.EASE_IN)
tween.start()
hurtbox.start_invincibility(1)
hurtbox.create_hit_effect()
func _on_Stats_no_health():
queue_free()
HealthUI:
# Variables
onready var label = $Label
var player = preload("res://Player/Player.tscn")
var instance = player.instance()
var stats = instance.stats
var current_hearts = stats.current_health setget set_current_hearts
var max_hearts = stats.max_health setget set_max_hearts
# Functions
func _ready():
add_child(instance)
self.max_hearts = stats.max_health
self.current_hearts = stats.current_health
stats.connect("health_changed", self, "set_hearts")
func set_current_hearts(value):
current_hearts = clamp(value, 0, max_hearts)
if label != null:
label.text = "HP = " + str(current_hearts)
func set_max_hearts(value):
max_hearts = max(value, 1)
i admit , i didnt understand your whole situation but adressing the script directly like
onready var health-bar-variables = get_node(“/root/Level1/CanvasLayer”)
or finding the script with get_parent._get_child._get_node() should be enough
horsecar123 | 2022-03-07 00:34