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.