Proper way to separate server/client logic in same project?

Godot Version

4.6.1

Question

What is the proper way to separate server and client code in such a way that I can make a server release and a client-only release? Most tutorials I found out there are the kind of you write a game in which you can serve and also connect as a client to another instance.
I want to make a game in which the user is a client-only and will connect to my server to play. As talking with AI it suggested me to use Feature Tags and use code like

if OS.has_feature("client"):
	print("client build")
        # run client logic
if OS.has_feature("server"):
	print("server build")
        # run server logic

This seems to work well, but I can only test it when I actually do an Export. Is there a way to separate it and also run the client/server by pressing F5 (run scenes)?

Or maybe I’m in the wrong path and I should use something different than Feature Tags?

EDIT*****
By “Proper way” I know there isn’t a single source of truth of “must be that way”, I’m just asking for something like Best practices, or what an experienced godot dev would do.
Some stuff would need to run different in client and server. For example, the starting scene in the client would be a Login Screen and in the server would be just a Screen for starting the Server instance. But I need to internally differentiate if I’m running either a client or a server

You detect if you are in the editor, and allow both, and have a way to choose between the two when you launch. Then you can go to Debug Menu → Customize Run Instances… and turn on Enable Multiple Instances. Tell it to run 2, and when you press the play button, two instances will pop up. They will detect you are running from the editor and give you the option to choose client or server.

3 Likes

Thank you, that seems to solve the problem

Another question, how can I separate scenes that are only for server from the client? since the OS.has_feature(“feat_name”) is only available for the gdscript

1 Like

You probably don’t want that as high level multiplayer does require the same scene tree for nearly all operations.

In your export settings you can mark specific resources or folders to be excluded from the generated pack, if you intend to load a separate but similar scene for the server and one for the client you will again have to use OS.has_feature or make use of the multiplayer information such as multiplayer.is_server

2 Likes

What @gertkeno said. I assume you have one export that is server-specific. Exclude files you don’t need in that. It can help to organize your folders along those lines.

Keep in mind that the Godot exporter is smart. If you have a .png file that isn’t used in your project, it will not export it. Which means that by excluding the scenes that you don’t need, all their dependencies will be excluded as well.

1 Like