How to use two SceneTrees to run both server and client in one project

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

I want to programm a card game.
The architecture i want to use is that all game logic is handled by a server and the grafic and gui stuff by a client.
But the programm should include server and client in the same project so that evryone can start a server to play with friends.

I found an example using multiple scene trees for godot 2

I tried to adapt this for godot 3
But when starting a new scene tree it seems to become the active render target directly.

So I can’t start a scene tree to start the server on because the client scene tree is not rendered anymore. But the client process is still called.

In the example

  scene_tree.get_root().set_as_render_target(true)

is called to do this but it seems to happen automaticly in godot 3.

I have to use seperate scene trees because one scene tree can handle one network peer for the highlevel networking.

I am trying to solve a similar problem. I’m considering submitting a feature request to somehow separate the network binding from SceneTree so it’s easier to build dedicated servers with separate listeners for each functional domain (e.g. user authentication, lobby, game play, etc). It will allow servers to be better structured, more secure, better support multiple instances, etc.

I’m interested in how you adapted the scenetree_holder example to Godot 3.2.3. Would you share that part of your code?

tx350z | 2020-12-19 11:21

:bust_in_silhouette: Reply From: MintSoda

Maybe server_tree.get_root().set_update_mode(Viewport.UPDATE_DISABLED)? Just guessing.

This solves the problem with the drawing but i still dont get rpc to work. But i guess this is an other problem.

RoniPerson | 2020-12-20 09:34

Ok I got rpc to work as well. Thx

RoniPerson | 2020-12-20 09:48

:bust_in_silhouette: Reply From: RoniPerson

For all with the same problem here is how I solved it.

InitServer.gd

onready var server_tree = SceneTree.new()

func start_server():
  server_tree.init()
  server_tree.get_root().set_update_mode(Viewport.UPDATE_DISABLED)

  # add your server scene (it needs to have the same name as the singelton which handles the client connection)
  server_tree.change_scene_to(preload("res://Server/Server.tscn"))

  set_process(true)
  set_physics_process(true)

func _ready():
  set_process(false)
  set_physics_process(false)

func _process(delta):
  server_tree.idle(delta)

func _physics_process(delta):
  server_tree.iteration(delta)

func _exit_tree():
  server_tree.finish()

To start the server call InitServer.start_server()
Inside of your Server.tscn you can start a server via the standart

get_tree().network_peer = peer

methode.

For rpc to work you have to know that the script which calls the rpc has a path. In your server scene you must have your script which handles the rpc under the same path.
So if you autoload your client manager as Networking its path is “root/Networking”. Your Server.tscn is added to the root. If you name the root node of the scene its path is “root/Networking” as well. So rpc should work.

Tell me if I misunderstood the stuff with the paths but it works for me.

Note: I havent tested all details of this.

:bust_in_silhouette: Reply From: RedPilled

Use custom multiplayer for this purpose, which will allow you to run as many servers and clients you want in the same SceneTree.

Here is a demo project: GitHub - LudiDorici/gd-custom-multiplayer: A godot demo project showing how to use some of the new MultiplayerAPI functionalities