How to instantiate thousands of scenes [Godot 3.1]

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Ploppy

Even though I am using Godot 3.1-alpha, I don’t think it is related since I had a similar problem in Godot 3.0.

I want to create a giant grid, made out of hundreds of chunks which are also made out of 100 cells each.

I opted for this design to improve performance, only the chunks visible on the screen are rendered.

At 225 chunks which represents ~22k cells, the game does not start and print constantly ‘Object was deleted while awaiting a callback’ in console.

This is how I instantiate a cell, in a for loop:

var cell = Cell.instance()
$cells.add_child(cell)

Chunks are instantiated in a similar manner.

I have a map scene where the packed scenes are loaded:

var Chunk: PackedScene = preload("res://Chunk.tscn")
var Cell: PackedScene = preload("res://Cell.tscn")

Chunks are created in this scene, the Cell packed scene is then given to the chunks once created in order to instantiate the cells.

I think that the problem is related to my implementation and not to Godot itself.

I would like some help, thanks.

Edit:

It looks like that the problem was related to the size of the message queue. By increasing it, I managed to instantiate 40k cells.
Though I doubt that it is a proper solution.

I’m not sure what’s going on here, but I can say 22K is probably way off the charts. Nodes are high-level objects, which means there is a limit of how many you can create at once without running into performance/memory issues. To go beyond, a more low-level or specific technique needs to be used, or simply not instanciate nodes that are too far away.

The error message is printed for statistics, maybe it’s not directly related to your game but rather the debugger struggling with all the nodes? Also make sure to disable “Remote scene tree” otherwise it’s going to choke for good.

What is in your Cell.tscn?

Zylann | 2018-11-21 18:56

First of all, thank you very much.

Where is the “Remote scene tree” setting?

My Cell.tscn is made of 1 Node2d and 1 Sprite.

I think that the problem is that I don’t know what the “message queue” is and what it does. That’s why I’m having a hard time finding a solution.

I’m pretty sure that all I have to do is not build the scene in a single tick. I should try to only load a limited number of chunk per tick to not exceed the default message_queue limit. Because performance wise, it runs at 60 fps without any issue with 40k cells, so I think it’s good.

Ploppy | 2018-11-21 21:00