I am trying to create a basic level generator that will simply generate map for my game.
It is a very basic game, spaceShooter like, where player moves up the screen on a map that is proceduraly generated.
The idea is that I have couple of scenes created as pieces(terrain.tscn with Terrain.gd script) of the entire map.
LevelGenerator class(responsible for map generation) has a variable defined as arrays where you can put terrain pieces that are supposed to be spawned when game starts. Then I instanciate terrains in Ready() function:
#LevelGenerator.gd
class_name LevelGenerator extends Node2D
@export var mapSegments: int = 10 #number of TileSets to repeat
@export var tilesToGenerate: Array[PackedScene]
func _ready():
#await get_tree().create_timer(0.0).timeout #TODO
createLevel()
func createLevel():
for i in min(tilesToGenerate.size(), mapSegments):
var lvl = tilesToGenerate[i].instantiate()
get_parent().add_child(lvl)
lvl.z_index = -999
lvl.global_transform = Transform2D(0, Vector2(0, i * 490 * -1))
For some reason when I run the game terrain scenes are not instanciated.
When I change the code by adding await function befor createLevel() it works. Even if it’s set to wait 0 sec.
You need to load() or preload() the scenes before you can instantiate them. Your array is probably holding some kind of reference to the scenes, not the actual PackedScene data. Personally, I’d use a text file with the filenames instead of an exported array in the editor. Or an array of paths, instead of scenes.
Thanks for replies. Adding load and call_deffered actually works. Couldn’t use preload since it requires path to be fixed String value.
Nevertheless I still am wondering why it was not working. Many people are showing this kind of assigment (using @export and then drag and drop) as dependency injection examples (spawning bullets etc.). In addition it actually was working fine with just adding some delay before. Which would suggest some sort of resource load order issue.
Scene instancing is not the same as Node instancing. There is a lot to unpack there, metaphorically and literally. It’s not a simple thing and you can learn more about it in the manual, but I don’t think it’s worth your time learning the internals. Just know it is not quite the same. Maybe worth a bug report/feature request on the git, tho.