Godot Version
V4.3.stable
Question
I do not know why my code is failing at line 26 here saying that MeshNode is Nil.
@tool
extends Node3D
class_name TerrainNode
enum TerrainType {NORMAL_TERRAIN, ROUGH_TERRAIN, WATER_TERRAIN, POISON_TERRAIN, LAVA_TERRAIN}
@export var CurrentTerrain = TerrainType.NORMAL_TERRAIN
@export var TerrainMesh = load("res://Scenes/Primary/TerrainBlocks/PlaceHolder/PHTerrainNormal1.tscn"):
get:
return TerrainMesh
set(Mesh):
TerrainMesh = Mesh
UpdateMesh()
@onready var MeshNode = $PhTerrainNormal2
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func UpdateMesh() -> void:
var NewMesh = TerrainMesh.instantiate()
MeshNode.queue_free()
MeshNode = NewMesh
add_child(MeshNode)
Maybe you should only call UpdateMesh if it is not set to null
set(Mesh):
TerrainMesh = Mesh
if MeshNode:
UpdateMesh()
That didn’t change anything unfortunately, same error in the same place. MeshNode specifically will not let me queue_free().
You must be calling UpdateMesh elsewhere too then.
You could add a similar check to the function itself
func UpdateMesh() -> void:
var NewMesh = TerrainMesh.instantiate()
if MeshNode:
MeshNode.queue_free()
MeshNode = NewMesh
add_child(MeshNode)
export is called after onready, so when youre referencing an onready var from an export, the export var doesnt exist yet. at least thats what i can tell at a glance
This will stop the error, but will not allow me to queue_free the old mesh node. The whole idea is to have a way to quick swap out the mesh node being used in this scene.
I finally got it. I set up the node to make it spawn the mesh node itself instead of grabbing the one that is placed there in editor. Using this it works naturally.