Hello everyone,
I'm working on a project in Godot where I need to replace a node (Battlefield) with a new instance and maintain the references in other parts of the code. Currently, I'm running into an issue where the BattleManager script breaks because the BF reference becomes null after freeing the old Battlefield node.
Here is the relevant part of my code:
battlescene.gd:
extends Node2D
class_name BattleScene
@onready var BF : BattleField = $Battlefield
@onready var BM = $BattleManager
func _ready():
pass
func change_battlefield(to_BF:BattleField):
var player = BM.player
BF.units.remove_child(player)
BF.free()
to_BF.name="Battlefield"
add_child(to_BF)
to_BF.units.add_child(player)
BattleManager.gd:
extends Node
var player
@onready var player_data = preload("res://Scenes/Units/player/player.tres")
@onready var BF : BattleField = get_node("../Battlefield") #I want this reference to update when changing battlefield
@onready var AM = get_node("../AnimationManager")
@onready var BS = $BattleState
@onready var turn_queue = []
and here is my Scene Tree

The issue occurs after I call BF.free() in change_battlefield(), causing the BF reference in BattleManager to become null. How can I properly replace the Battlefield node without breaking the references in other scripts? I tried using signals, but I didn't get the results I wanted.
Any help or pointers would be greatly appreciated!