I can't create a node using code

4.1.1

I’m trying to create my own grid map. To begin with, I decided to add one cell through the script, but upon startup the cell is not created.

Code:

extends Node3D

var cell = preload("res://scenes/cell.tscn")

func _ready():
	var cellInstance = cell.instantiate()
	cellInstance.position = self.position
	add_child(cellInstance)

Set position after adding the child

add_child(cellInstance)
cellInstance.position = self.position
1 Like
@onready var cell = preload("res://scenes/cell.tscn")
2 Likes

I’m really curious now, why is @onready needed here? Preloads are handled at compile time, so they should be available right from the start. Heck, most my preloads are even in a const like this:

const MyType = preload("my_type.gd")

(Which has the added benefit that you can use it as a type hint)

I mean it was marked as solved, so the issue is apparently fixed. But still this has me confused because to me the initial code looks perfectly fine.

I wasn’t quite sure but there are cases in G4.x when const reported a problem and @onready went without problems… but I’m not completely proficient in 4.x as I’m still using 3.x version… if this is incorrect or unclear, feel free to edit it according to how it really should be…

I mean you obviously solved the question, so I won’t edit it. But that’s good to know, I’ll keep an eye on that because that seems like an engine bug to me.

it will probably be some HACKY solution. and maybe we just accidentally discovered a long-existing bug in the code, which will allow such a variant