Spawning Pre-built obstacles Based on the Backround

Godot Version

4.22

Question

I am curently making a 2D Endless runner and i am trying to let my Pre-built obstacles spawn based on my Backround . the obstacles also contain the Ground for the player to walk on so they have to be spawned Without any gaps

Sorry, but it’s not entirely clear what your question is. Can you provide more details about what you’ve tried, and why it hasn’t worked?

Are you trying to spawn the entire obstacles, including their artwork, or just their collision shapes?

So the obstacles are just made with a Tileset and they and the Colision shaped (wich are death Zones and Jumppads) should be Spawned in the main Scene the Backround was just for reference for Scale.
And what have i tried : i am prety new to it so i Tried using yt tutorial but none realy Fit what i want to do and i also Tried using Ai but that did not work :frowning:

I’m not sure what your question really is but I try to give you some ideas:

  • you can build you or level with tile maps (platforms, walls, etc)
  • the endless aspect of your platformer is more or less generating level content

My first steps would be:

  • build an initial level with some elements you want to use (platforms with gaps, walls, whatever…)
  • implement your movement, jumping, etc.

After that I would start generating level content because you should have some constraints already (you know what you have to generate).
Now you will have to find mechanism to generate platforms. That’s very subjective and depends heavily on your requirements.

Your question sounded more conceptual so a gave a more general answer. Hope that helps a bit

:coffee:

I mean i wanted it to be more like the lvl Generation in SubwaySurfers where you have ~10 Diferent prebuild Sections and they Randomly Spawn behind each other . and i want to prebuild 10 of the ones in the pic . (When i then am further i thought about adding biomes where then the obstacles look diferent in each one ) But for now just trying to get it ti work with no biomes

Okay that sounds like building preset tile maps.

You basically build segments as tiles maps and spawn them as needed.
Say you have 10 segments aka 10 different tile maps. You start with 1 or 2 and on certain triggers you spawn the next one at the position you want it.

So you don’t have to spawn multiple objects or manipulate single tiles. You probably just need to define some kind of interface between each segment so they fit together

But How to i know where i have to spawn the segments ? it is an endless runner so i move and the position changes all the time

|_____| = Segement

Let’s assume your level looks like this with 2 segements
|_____| _____|

As I understand your idea you have to spawn the third one at the end:
|_____| _____| _____|

You can calculate the length of a segment (if not equal) so you know where the end of your segments is.

You could spawn the next one when a player enters a segment or define an area that triggers the next one

ok i lett chat gbt cook but now the segment sometimes spawn at the same time and sometimes just dont at all

extends Node

@export var obstacle_scenes: Array = []
@export var distance_between_obstacles: float = 544.0
@export var spawn_interval: float = 2.0

var last_obstacle_position: float = 0.0
var time_since_last_spawn: float = 0.0

func _ready():
	var scene1 = ResourceLoader.load("res://Scene/Hindernis/hindernis_1.tscn") as PackedScene
	var scene2 = ResourceLoader.load("res://Scene/Hindernis/hindernis_2.tscn") as PackedScene
	if scene1:
		obstacle_scenes.append(scene1)
	if scene2:
		obstacle_scenes.append(scene2)
	last_obstacle_position = -distance_between_obstacles
	spawn_obstacle()

func _process(delta):
	time_since_last_spawn += delta
	if time_since_last_spawn >= spawn_interval:
		spawn_obstacle()
		time_since_last_spawn = 0.0

func spawn_obstacle():
	if obstacle_scenes.size() == 0:
		print("Error: obstacle_scenes array is empty.")
		return
	
	var obstacle_scene = obstacle_scenes[randi() % obstacle_scenes.size()]
	if obstacle_scene:
		var obstacle_instance = obstacle_scene.instantiate()
		if obstacle_instance is Node2D:
			var new_position = last_obstacle_position + distance_between_obstacles
			obstacle_instance.position = Vector2(new_position, 0)  # Position im 2D-Raum
			add_child(obstacle_instance)
			last_obstacle_position = new_position
		else:
			print("Error: Instantiated scene is not of type Node2D.")
	else:
		print("Error: obstacle_scene is null.")

I’m not sure we are in the same boat here :slightly_smiling_face:

Tile maps can make your life way easier.
May start with a tutorial on how to make a simple platformer so you understand the basics.

Generating your level in the fly is just additionally making your gameplay more pleasant.
Therefore you need core gameplay and some definitions. Like what is a segment?

This may be a good start for most of the fundamentals you need: