0 votes

Hello, I'm new to Godot,
I'm not sure about how to use class_name, I try to use it, but when I play the game, there is nothing. Here is my GDScript (some singletons are used, this code works if I don't use class_name and I preload the script on the root node):

extends Spatial
class_name Scenario

# dimensions
export var xdim : int
export var zdim : int
var xzdim : int


# lib of tiles
const Tile = preload("res://Assets/MapTiles/Tile.tscn")


# scenario tiles
var scenarioTiles : Spatial = Spatial.new()


# RandomNumberGenerator
var rng : RandomNumberGenerator = RandomNumberGenerator.new()

func _ready():
    pass


func _init(_xdim, _zdim):
    xdim = _xdim
    zdim = _zdim
    xzdim = xdim*zdim

    add_child(scenarioTiles)

    rng.randomize()

    var camera = Camera.new()
    camera.translation = Vector3(0,10,0)
    camera.rotation_degrees = Vector3(-90,0,0)
    add_child(camera)


    generateTiles()


# generates all the tiles of the scenario and add them to scenarioTiles node
func generateTiles():

    var position : Vector3
    var nodeToAdd : Node
    for xi in range(xdim):
        for zi in range(zdim):
            if zi%2 == 0:
                position = Vector3(xi*General.v0.x, 0, zi*General.v1.z)
            else:
                position = Vector3(xi*General.v0.x+General.v0.x/2, 0, zi*General.v1.z)
            nodeToAdd = Tile.instance()
            nodeToAdd.translation = position
            nodeToAdd.hexTranslation = Vector3(xi, 0, zi)
            nodeToAdd.changeCenter(MapTiles.centerMaterialKeys[0])
            scenarioTiles.add_child(nodeToAdd)

The main scene has this structure: GameManager the root node (Node not Spatial), and as a child I add Scenario and I set xdim=5, zdim=5 on the Inspector. When I run the scene, nothing is shown (the Scenario has a Camera as a child).

I would like to use class_name for "res://Assets/MapTiles/Tile.tscn", but first I want to solve the above problem.

Godot version 3.2.3
in Engine by (217 points)

1 Answer

+1 vote
Best answer

Is the camera set as current?

by (1,371 points)
selected by

It works when I execute! There is one last question, at the editor, the Scenario is not shown, but everything works when the game executes. Is there another problem or that behavior is attached to a tool and not a class_name?

What do you mean with "the Scenario is not shown?"
Are you talking about the scene tree, or the node properties?

I mean that, for example, when you add a MeshInstance and set a Mesh, you can see the mesh in the editor without executing the game. My question is that if the Scenario should be shown in the editor too or if there is a way to do so.

oh i understood: the editor does not load the script, so if you have a node with a script that change any property of the node, you'll see that property changed only after you play the project.

In this case, your script creates the tiles starting from an empty Spatial, therefore the tiles are going to be loaded only when the script will be executed (so the editor looks empty)

Useful tip: when you play the project, 2 new buttons appear: Local and Remote, just above the tree structure on the left.
If you click on Remote, you'll see the three scene that is been generated and currently used. You can click on the node and see/modify their properties in real time

I understand, thanks you so much!

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 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 webmaster@godotengine.org with your username.