Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Yasin |
I tried using this code for random obstacle generation.
extends Node2D
export (Array, PackedScene) var obstacles
var pos : Vector2 = Vector2()
func _ready():
assert(len(scenes))
add_random_obstacle(pos)
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
add_child(new_obstacle)
return new_obstacle
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
it might be that, the equivalent of obstacles
in enemy_node which is spawnReference
, is not an array, so you are calling len() on a single object, which create the error
Andrea | 2020-05-27 08:21
I added array; Now I am not getting any errors but the Enemy(Position2D) node is moving back and forth as I set the animation player loop. The obstacles are not instanced.I think I have done some mistake in connecting the signal of Timer node in the Enemy.gd. Can you please take a look in that script
Here is the corrected Project.
Here is the Image.
Yasin | 2020-05-27 18:09