New using class_name: I don't get the expected result

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

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.

:bust_in_silhouette: Reply From: Andrea

Is the camera set as current?

Sorry, I modificated the code and I forgot the camera. If I set it as current, nothing is shown. In fact, I get the error E 0:00:00.803 _create_instance: Condition "r_error.error != Variant::CallError::CALL_OK" is true. Returned: __null <C++ Source> modules/gdscript/gdscript.cpp:127 @ _create_instance()

abelgutierrez99 | 2021-01-03 10:40

Does this error point to a specific line of the code?

Andrea | 2021-01-03 14:23

The error is not pointed to any line. The game runs, but there is nothing, I suppose due to the error.

abelgutierrez99 | 2021-01-04 09:10

so, i copypasted your code in my project and it worked.
But i’ve cut out some lines cause i didnt know what values to use, see below. Hopefully you can start from here and understand which parts are causing the issue

extends Spatial
class_name Scenario

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


# lib of tiles
const Tile = preload("any_mesh_scene")


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


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

func _init():
	xdim = 0
	zdim = 0
	xzdim = xdim*zdim

	add_child(scenarioTiles)

	rng.randomize()

	var camera = Camera.new()
	camera.translation = Vector3(0,50,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(0,0,0)
	var nodeToAdd : Node
	for xi in range(xdim):
		for zi in range(zdim):
			pass
	nodeToAdd = Tile.instance()
	nodeToAdd.translation = position
	scenarioTiles.add_child(nodeToAdd)

Andrea | 2021-01-04 12:10

The problem was at func _init(_xdim, _zdim):, it works if I substitute it by func _init(): and remove xdim = _xdim and zdim = _zdim.

The next problem is that if I set xdim and zdim in the inspector, nothing is shown in the editor. Moreover, it only works well if xdim and zdim are given in the code, not in the inspector (the values given in the inspector are being ignored or I don’t know).

abelgutierrez99 | 2021-01-05 09:23

try using this when declaring an export variable:

export (int) var xdim

you can also add information, like min(0), max(100) accepted value, increase step (1), and set a preset value (5)

export (int, 0,100, 1) var xdim=5

Andrea | 2021-01-05 13:06

I tried it but nothing changes. The values given in the inspector are being ignored. The final code is:

extends Spatial
class_name Scenario
# dimensions
export (int, 0, 100, 2) var xdim
export (int, 0, 100, 2) var zdim
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():
	xzdim = xdim*zdim
	
	add_child(scenarioTiles)
	
	rng.randomize()
	
	
	var scenarioCamera = ScenarioCamera.new(Vector3(0,4,0),
	Vector3(-90, 0, 0),
	true,
	1,
	1,
	1,
	1)
	add_child(scenarioCamera)
	
	generateTiles()


# generates all the tiles of the scenario and add them to scenarioTiles node
func generateTiles():
	var position : Vector3
	var nodeToAdd : Node
	for zi in range(zdim):
		for xi in range(xdim):
			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)

And is added as a child of a Spatial node from the editor. (scenarioCamera extends from Camera)

abelgutierrez99 | 2021-01-05 18:49

it could be that when init() is called, the variable from the editor are not written down in the memory yet.
maybe, try moving generateTiles() to ready()?

Andrea | 2021-01-05 19:05

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?

abelgutierrez99 | 2021-01-06 10:03

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

Andrea | 2021-01-06 13:32

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.

abelgutierrez99 | 2021-01-07 11:01

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

Andrea | 2021-01-07 13:26

I understand, thanks you so much!

abelgutierrez99 | 2021-01-07 21:37