Help With Multiple Scene Trees

Godot Version

4.2

Question

I am making server side for a game that needs to simulate multiple active worlds at once, each world loading and unloading when a player joins and all players have left (in that respective order). This is a completely server authoritative model, with players only telling the server what inputs they are pressing and when. This means all physics etc. are being simulated on the server side.

The processing time/lag that occurs when a new world is opened is considerable, approximately a 1-2 second freeze. All worlds are being simulated in one scene tree, and you cannot multithread the scene tree. When a new world is opened, all other worlds freeze for that 1-2 seconds, because they are on the same thread. Even further, currently I’m only building the chunks of the world when it opens, and there is no processing happening during runtime for the world mostly. But once the game is built upon more, there will be even more expensive things happening during runtime that will be causing a lot of lag if they are all happening on one thread.

What I want to be able to do is have an array with current running worlds, and each element is a scene tree object. I want each scene tree running on its own thread. And then the “main” scene tree/default thread will essentially just be a re-router that is a unified “spot” for player’s to connect to, and then the server tells them where to go.

I’ve successfully created SceneTrees (it’s easy, it feels illegal, but you can just do SceneTree.new()). I’ve also successfully assigned a MainLoop script to them. One question I’m having is what do I put inside the MainLoop to successfully simulate a default scene tree’s MainLoop? Furthermore, how do I setup a thread that is correctly calling _process and _physics_process etc. at the standard times? I know how to make a new thread/function, but I don’t know how to utilize that for this specific use case. How do I set the root node? Any relevant information would be helpful, even if it’s not something I explicitly asked.

As a last note, if you think you have an alternative to multiple scene trees to allow different parts of the tree (each world) to run on different threads, please feel free to say. Though as far as I know, this isn’t possible.

1 Like

Resources can be opened in background, check out ResourceLoader.

Another option would be to make the world it’s own process that talks to the server via network. This would scale better then having all worlds on the same process, you could even have dedicated hardware for each world.

There will be a performance penalty defining a scripted main loop. This could be exacerbated depending on how many worlds you intend to instance.

For answering your question take a look at the source code. I think it’s input, physics steps with input in each step, input again, process, draw, and a few other things can’t remember.