Hello,
we are using Unity for building our game client making a multiplayer game. Our server however is written in C++ for obvious performance reasons.
I would like to ask the community if you guys know any tool/library which can allow me to export the terrain in some format and then build myself a logic to import it in C++ in our server.
For example maybe I can export the terrain as OBJ and import in in our C++ server via OpenGL?
Can you suggest any other better/easier approach ?
I need that to act as authoritative solution. For example when player 1 is shooting at player 2 I want to see if there isn’t any wall between them.
I am really open for suggestions.
So basically you want to recreate the Unity engine (or a part of it at least) in a separate C++ project? But Unity core is written in C++. GameObject itself has no effect on the scene, it’s the components that create the behaviour and fill the scene.
What exactly would “exporting the gameobjects to C++” even mean?
If you would describe an exact subset of what you want to mirror in your C++ server, it might get more doable. For example, maybe all you need is a list of static box colliders from a scene. Still, you would most likely be getting different results on the client and on the server.
In such case, you can either write an editor script that extracts all data you want (eg. find all colliders and write their position and size to a json file) or parse the unity’s yaml files directly.
Still, you would have to recreate the collision detection (or anything else) in your own C++ project.
While I understand your motivation to mirror the unity behavior in a separate project, I’d suggest that you instead create a headless build from your Unity project to serve as a dedicated server. You can boost the performance by utilizing the powerful IL2CPP build option. To further optimize the server build, you can remove everything that’s unneccessary. For example, your server might not need to evaluate character animations (unless of course you want them there for hitbox evaluation). Your server most likely doesn’t need to include any graphical assets (unless you want your server to render screenshots and send them to clients or store them in a database), same goes for sound clips etc. In a such a case, you could implement a custom BuildPreprocessor that simply removes any MeshRenderer, MeshFilter, AudioSource, Animator, Camera and other components from all scenes and prefabs, to ensure the server build stays lightweight and doesn’t do anything absolutely necessary for the simulation of the game. Use C# conditional compilation to exclude any code related to client-only experience and access to those removed components. Maintain a single project instead of mirroring the same behavior in two that use completely different frameworks. This approach works for me.