I tried using this code for random obstacle generation.
export (Array, PackedScene) var obstacles
var pos : Vector2 = Vector2()
func add_random_obstacle(pos : Vector2) -> Node2D:
return add_obstacle(floor(rand_range(0, len(obstacles))), pos)
func add_obstacle(type : int, pos : Vector2) -> Node2D:
assert(type >= 0 and type < len(obstacles))
var new_obstacle : Node2D = obstacles[type].instance()
new_obstacle.position = pos
I created a ProgressionTester.tscn which is a Node2D node and added the code. It works perfectly fine. Random obstacles are being instanced. But I tried adding the same code in Enemy.tscn which is a Position2D node ; But I am getting assertion error and the obstacles are not being instanced. Can you please take a look in the project below ?
[ I have added the ProgressionTester.tscn in Tests folder and Enemy.tscn in the Enemy folder in the project below. ]
Here is the Project