Best node to use for platforms in an infinite platformer

Godot Version

4.5

Question

What would be the best way to spawn platforms in an infinite platformer? It’s going to be like Flappy Bird but then vertically where you’re constrained by the screen width. I see advantages of just using StaticBody2D’s, code-wise they’re probably the easiest as you could just basically spawn them on a random y between the left and right of the screen if you wanted to.

But advantages of TileMaps is that they have one-way collisions built in, which I’d really like for my platformer, and that they’re specifically built for platformers so you can easily have more diversity. How would I do that?

I think you would have to be more careful with your auto-generation than just random y placements. That could go wrong dramatically.

As for tilemaps, you would generate a tile and add it to the tilemap at whatever position. As the camera scrolled up to it, it would appear. This would normally be done in chunks, and you would have a chunk generator to generate and load the chunks, and unload them once they went off screen or beyond a certain range.

How you do this exactly, would depend entirely on your game vision and gameplay and design ideas. Lets say you were doing DonkeyKong style. Your generator would generate diagonally sloped platforms with carefully spaced random ladders and barrel spawners etc. Lets say a diagonal platform ‘layer’ is 300px high, your chunks would be screen width by 300px chunks, so you could then just stack them as they are generated.

Anyway, doing this for tilemaps is more than do-able. Especially because your description is limited to vertical movement only. An open world is much harder because you have to be able to back track over previously loaded areas. Maps based on noise like perlin noise are good for this, as you don’t have to save them because, as long as you don’t use any random generation, they can always be recreated on demand.

Good luck with it, auto generation is great fun to code, even though it can seem tricky at first, when you get going with it, the results can be amazing as entire worlds based on a single seed open up around you!

What can exactly go wrong? I’m doing a game jam and this was one of the main features I had to implement, so with a tight deadline I just started with the StaticBody implementation as that looked the most feasible to me.

This is what I have right now (no random y generation yet as I started to code around half an hour ago)
The code might be slightly spaghetti, I’m a beginner lol.

class_name PlatformSpawner extends Node2D


@export var platform_scene: PackedScene

@onready var camera_2d = $"../Player/Camera2D"
@onready var player = $"../Player"
@onready var player_collision_shape_node = player.get_node("CollisionShape2D")
@onready var player_collision_shape = player_collision_shape_node.shape

var y_pos := 0


func spawn_platforms(amount: int) -> void:
	var camera_size = get_viewport_rect().size * camera_2d.zoom
	var camera_rect = Rect2(camera_2d.get_screen_center_position() - camera_size / 2, camera_size)
	var height_addition = 5
	for i in amount:
		var height = player_collision_shape.size.y * player_collision_shape_node.global_scale.y * height_addition
		y_pos = camera_rect.end.y - height
		var platform = platform_scene.instantiate()
		platform.position.y = y_pos
		height_addition += 3
		add_child(platform)


func _on_game_manager_game_started() -> void:
	spawn_platforms(4)

Good luck in your game jam! I have been meaning to do one myself for ages but have never been able to commit to it!

If you rely on random generation the danger is that all your platforms are all over to the left, or overlapping, or progress up might become impossible.

However, for a game jam, go for it! That might turn out to be part of the fun!