Godot is not instanciating the child of my node via ".new" but it does when packed scened are used

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

I’m trying to create a map from tiles. Here is my tile scene:

The script is really easy, I’m just starting, but the idea is to add more thing in the future:

class_name EmptyTile
extends Spatial

Now the “game manager” is a Control node with the script:

extends Control

var sizeX : int = 11
var semiSizeX : int = sizeX%2
var sizeZ : int = 11
var semiSizeZ : int = sizeZ%2

var ttile = preload("res://Assets/Tiles/EmptyTile/EmptyTile.tscn")

func _init():
	var tilesTranslations : Array = []
	for i in range(sizeX):
		for j in range(sizeZ):
			tilesTranslations.append(Vector3(-semiSizeX + i, 0, -semiSizeZ + j))
	var currTile : Spatial
	for i in range(tilesTranslations.size()):
		currTile = ttile.instance() #EmptyTile.new() # here is the problem, see bellow
		currTile.translation = tilesTranslations[i]
		currTile.name = var2str(tilesTranslations[i])
	var camera = Camera.new()
	camera.translation = Vector3(0, 1, 5) # just placing the camera in to see something

Now, the problem, when I use currTile = ttile.instance() this is the output:

note you can see Remote on the left side.

Now if I use the way I want currTile = EmptyTile.new():

As you can see now, the MeshInstance is not added. What is happening? I want to use .new().

:bust_in_silhouette: Reply From: sash-rc

Your EmptyTile class is just basically a Spatial. So EmptyTile.new() creates Spatial.
If you want to have all that stuff stored in the “EmptyTile.tscn”, you need to load and instance that particular scene.

Just to be clear: this is not scenes/nodes attached to a script (that would magically load them) - just the other way around: you attach script to some Node (and it possible to attach the same script to different Node instances and even of different (compatible) classes) to alter and extend their behavior.