How do I make infinite spawning

Godot Version

v4.3

Question

I am trying to make a game kinda like flappy bird but you go upwards, instead of pipes to dodge you need to hit the powerups to jump higher, I want them to spawn infinitely as you go higher and higher, I use a path2d to make my powerups spawn randomly in a line I just need the path2d to spawn infinitely

extends Node2D

func random_bolt():
	var spawn_bolt = preload("res://power.tscn").instantiate()
	%PathFollow2D.progress_ratio = randf()
	spawn_bolt.global_position = %PathFollow2D.global_position
	add_child(spawn_bolt)

func _ready() -> void:
	random_bolt()

I haven’t tried this for myself, but here’s my silly idea: why not make the Path2D a child node of the player? If you set it to always be 200 pixels (or whatever other value you desire) above the player, that offset will be kept regardless of the player’s position. Then add a timer node, and connect its timeout signal to random_bolt.

damn, thats actually a good idea, although if you were too far to either side it could spawn off the map, I want to add walls on the sides later on so it would outside

In that case, how about this? Instead of making the Path2D a child of the player, add a script to Path2D. Something like this:

extends Path2D

@export var player : CharacterBody2D = null
@export var distance : float = 200

func _process(_delta : float)->void:
	position.y = player.position.y - distance

Then add the player scene via Path2D’s inspector. This code only ever updates the position in the y-direction and leaves the x-direction untouched.

I was trying to do it but I have an error
Invalid access to property or key ‘position’ on a base object of type ‘Nil’.
on the last line, also I don’t understand what you mean by add the player scene via path2d inspector, the player scene is already in the game scene, I am quite new to making games so I don’t understand everything

If you click on your Path2D node in the scene view, you should see a bunch of new fields appear on the right side on your screen. (Under the tab named inspector.) One is called player, the other is called distance. You should be able to add your player scene from there. (I think you can drag it straight from the scene editor.)

edit: to explain this further, the player scene is already in the game scene. But the script on Path2D is entirely unaware the player scene even exists. By exporting a variable (that’s what that @export var ... line is for, you can tell the Path2D script “hey, here’s this external object you can use.”

The null in @export var player : CharacterBody2D = null means “by default, I don’t exist.” That’s why you have to drag the player scene in.

thank you so much it worked, I added a timer but I think I will make it so it spawns whenever I pickup a powerup

1 Like