So I am running into a bit of an issue here spawn multiple RayCast2D Turrets along the very top of the level scene. I currently have a Path2D and PathFollow2D nodes in the level scene, running along the very top edge of the level, running from 0 to the width of the room.
I’m currently following the Code Your First Complete 2D Game with Godot tutorial. I would like to spawn turrets randomly but not using a timer, rather through a bit of math. Basically if the randomly chosen value is between -1 and 100 and rounded down to zero, spawn the turret
I have some GML (version 8.1) that I’m trying to implement in GDScript but I am unsure on how to translate it.
GML Code:
if (floor(random(100-1))=0)
instance_create(x,y,obj_turret);
randomly chosen value is between -1 and 100 and rounded down to zero,
but I assume what you are trying to do is spawn a turret when the random float number between 0.0 and 100.0 is less than 1.0 (rounds down to 0).
The Godot code for that would look like this:
@onready var rng = RandomNumberGenerator.new()
func maybe_spawn_turret():
rng.randomize()
if rng.randf_range(0.0, 100.0) < 1.0:
var instance = obj_turret.instance()
instance.position = Vector2(x, y)
add_child(instance)
Thank you! I messed around with the code a bit and I was able to get the turrets to spawn. I did however end up having to use a timer, because the turrertspawner function only ran once
here is the code that works
func _TurretSpawner():
rng.randomize()
if rng.randf_range(0.0, 50.0) < 1.0:
var turret_spawn_pt = $Path2D/PathFollow2D
turret_spawn_pt.progress_ratio = randf()
var turret = turret_scene.instantiate()
add_child(turret)
turret.position = turret_spawn_pt.position