How do i get platforms to randomly generate

Godot Version

4.3

Question

Hello, i was wondering if anyone knows how to generate platforms for a doodle jump clone since im confused about how i should approach this. Currently im using:
func _on_timer_timeout():
var platform_temp = platform_scene.instantiate()
platform_temp.position.x = randi_range(0,600)
platform_temp.position.y = randi_range(0,10000)
add_child(platform_temp)
This to generate the platforms but its not even close to the player and its unplayable. Any help would be amazing thanks in advance.

You will need a reference to the player to spawn things near the player, you could use @export potentially. Your random values can be relative to the player if you add the player’s position.

@export var player: Node2D

func _on_timer_timeout() -> void:
    var platform_temp = platform_scene.instantiate()
    platform_temp.position.x = randi_range(-100,100) + player.position.x
    platform_temp.position.y = randi_range(-100,100) + player.position.y
    add_child(platform_temp)
2 Likes

Maybe player.global_position.[xy], but yes. that.

it gives me this error
Invalid access to property or key ‘global_position’ on a base object of type ‘Nil’.

You must assign it in the editor, it will show up as the node’s Property in the Inspector

In @gertkeno 's answer (and mine, for that matter) we were assuming that you have a player of some sort in the game, and you probably called it player. There isn’t a built-in player as far as I’m aware, you have to make one. You can name it whatever you like, and just replace our use of player with doodle or whatever you use to represent the player’s avatar in the game.