State Machine : referencing an instanced ProgressBar via get_node() from one script to another

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By zevia333

Hi there

So I have a state machine node tree. There is a master node, with five child nodes.
state machine, with child nodes player turn, enemy turn, player update ui, enemy update ui, and battle over

There is a master control node on top of all of these nodes, that instances everything that takes place in the scene. It also instances ProgressBars in the first script off of a loaded Enemy node via get_node(), because the enemy is not part of the local scene tree, but is its own unique scene.

My question is, how do I reference the ProgressBar i instantiated in this first script on the Control node, in the “enemy_update"ui” state, which is in a script on a different node in the tree?

Here is the relevant part of the script on the Control node

    func add_enemies(enemy_scene_path: String, marker1: Marker2D, marker2: Marker2D, marker3: Marker2D):
	var enemy_scene1 = preload("res://scenes/enemy/enemy1.tscn")
	# Create an instance of the enemy scene for marker1
	var enemy_instance1 = enemy_scene1.instantiate()
	enemy_instance1.position = $Enemy1.position
	var enemy_health_bar1 = enemy_instance1.get_node("HealthBar") as ProgressBar

Have you considered signals? From the description, I gather the ‘update ui’ part can just broadcast changes while something else listens and updates the bar. Having nodes know about one another leads to dependency issues and highly coupled nodes. A signal (or two) severs these dependencies.

spaceyjase | 2023-04-11 15:45

Seems like the right solution, I’m just not sure how to create a signal for the ProgressBar, because it’s instantiated on a different node, and is a child of a node from a different scene

zevia333 | 2023-04-11 18:50

You don’t, you only need the name of the signal and that can be abstracted away through the use of an event bus. Check this out: The Events bus singleton · GDQuest

spaceyjase | 2023-04-11 18:57

Killer, I’ll dig into this. Much appreciated

zevia333 | 2023-04-11 18:59