How to clone a scene multiple times on launch?

Godot Version

4.4

Question

I am making a game similar to Don't Starve Together. I need a script that makes the trees spawn in random positions in random ammounts at the start of the game

You could use a for loop with your normal spawning function

func _ready() -> void:
    for i in 100:
        var clone := preload("res://YourTreeScene.tscn").instantiate()
        var x: float = randf_range(-1000, 1000)
        var y: float = randf_range(-1000, 1000)
        clone.position = Vector2(x, y)
        add_child(clone)
2 Likes

Invalid argument for “add_child()” function: argument 1 should be “Node” but is “PackedScene”.

I have the tree scene in a (Scenes) folder, and in the Tree scene there’s the Sprite 2D, StaticBody2D, and a CollisionShape2D, if that helps

Sorry I’ve edited my post to include .instantiate()

Yeah, it works now. Thank you, wise man

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.