+1 vote

Hi,
I'm trying to create a map from tiles. Here is my tile scene:
enter image description here

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])
        add_child(currTile)

    var camera = Camera.new()
    camera.translation = Vector3(0, 1, 5) # just placing the camera in to see something
    add_child(camera) 

Now, the problem, when I use currTile = ttile.instance() this is the output:
enter image description here
note you can see Remote on the left side.

Now if I use the way I want currTile = EmptyTile.new():
enter image description here
As you can see now, the MeshInstance is not added. What is happening? I want to use .new().

Godot version 3.3.3
in Engine by (249 points)

1 Answer

+1 vote
Best answer

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.

by (1,646 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.