Is there a way to either refresh a node or update it?

Godot Version

4.3

Question

Hi, I am working on a JRPG game and I am trying to add some randomness to encounters. Right now I have some basic functionality where when the player collides with an enemy, a battle scene initiates with a random amount of enemies. However I use an onready variable to load in the Vbox of enemies before populating them. This is causing all the helper functions to refer to the original array of get_children(), which is 0 instead of the new array of just added children. I was wondering if I could somehow either update the array in the helper functions or pause the onready until after the populate function is called? I will provide the codeblocks for each of the scripts below

Any help is appreciated, Thanks!

battle UI script:

class_name Battle extends Control

@onready var _options_menu: = $Options
@onready var _enemies_menu : = $Enemies
@onready var _enemy_button := preload("res://scenes/enemy_button.tscn")
signal node_populated

func _ready() -> void:
	_populate()
	
	print (str(_enemies_menu.get_child_count()) + "child count")
	_options_menu.connect_buttons_to_obj(self)
	_options_menu.focus_button(0)
	_enemies_menu.connect_buttons_to_obj(self)
	
	
func _populate() -> void:
	
	var enemy_num := randi_range(1, 5)
	for enemy in range(enemy_num):
		_enemies_menu.add_child(_enemy_button.instantiate())
	
func _on_Options_button_pressed(button :BaseButton) -> void:
	match button.text:
		"Fight":
			
			_enemies_menu.focus_button()
		_:
			print("pee")

func _on_Enemies_button_pressed(enemy_button :EnemyButton) -> void:
	enemy_button.heal_hurt(-1)
	_options_menu.focus_button()
	for enemy in _enemies_menu.get_children():
		enemy.focus_mode = 0

Helper functions script:

class_name Menu extends Container

@export var can_focus := true
@export var focus_after_exit := true
var index := 0

@onready var _buttons := get_children()

func _ready() -> void:
	var _buttons := get_children()
	print(str(_buttons))
	for button in _buttons:
		button.connect("focus_entered", Callable(self, "_on_button_focus_entered").bind(button))
		

func connect_buttons_to_obj(obj : Object) -> void:
	
	for button in _buttons:
		if can_focus:
			var callable = Callable(obj, "_on_" + name + "_button_pressed" )
			button.pressed.connect(callable.bind(button))
		else:
			var callable = Callable(obj, "_on_" + name + "_button_pressed" )
			button.pressed.connect(callable.bind(button))
			button.focus_mode = 0
			
func focus_button(n: int = index) -> void:
	
	index = clampi(n, 0, get_child_count())
	if !can_focus and ! focus_after_exit:
		for button in _buttons:
			button.focus_mode = 2
		_buttons[n].grab_focus()
		
	else:
		_buttons[n].grab_focus()
		
	

func _on_button_focused(button : BaseButton) -> void:
	index = button.get_index()

I would just send the buttons to the helper function along with the connection object:

func _populate() -> void:
	
	var enemy_num := randi_range(1, 5)
	for enemy in enemy_num:     #<<< no need to use range here
        var enemy_button :=  _enemy_button.instantiate()
		_enemies_menu.add_child(enemy_button)
        _enemies_menu.connect_buttons_to_obj(enemy_button, self)
func connect_button_to_obj(button, obj : Object) -> void:
		if can_focus:
			var callable = Callable(obj, "_on_" + name + "_button_pressed" )
			button.pressed.connect(callable.bind(button))
        ...etc
1 Like