0 votes

I'm trying to make a 2D endless runner.

I want to set up a few scenes of obstacles and then spawn the scenes in a random order. But the game lags whenever I spawn a scene that has more than a few obstacles in it.

I'm completely new to this so the answer might be obvious. But is there any way to improve performance without decreasing the amount of nodes in each scene that I'm spawning?

Here is the code for my spawner:

export (Array,PackedScene) var scenes
var random_scene = RandomNumberGenerator.new()
var selected_scene_index = 0

func _ready():
    random_scene.randomize()
    selected_scene_index = random_scene.randi_range(0,scenes.size()-1)
    var tmp = scenes[selected_scene_index].instance()
    add_child_below_node(self,tmp)

func _on_Area2D_area_exited(area):
    random_scene.randomize()
    selected_scene_index = random_scene.randi_range(0,scenes.size()-1)
    var tmp = scenes[selected_scene_index].instance()
    add_child_below_node(self,tmp)

Thanks for the help!

in Engine by (15 points)

1 Answer

0 votes
by (889 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.