I am working on a multiplayer project that involves loading into different scenes and playing some kind of mini-game with any other players that happen to be in the scene at that time. These mini-games will contain AI that needs to be controlled by a dedicated server. Additionally, there is simulation that needs to happen in these scenes, even if no player is currently in one.
My first idea to take a crack at this was to have my server on startup, load all of the scenes in the game using
LoadSceneMode.Additive This way, on the server side, each scene would be in one place and available to be simulated.
I made a little sample project with three scenes, a base world, and two other worlds. When the server is started from the Base World scene, it loads World1 and World2 into its scene. From my server, I could spawn a cube in World1 by pressing the 1 key, and I could spawn a cube in World2 by pressing the 2 key.
I used the following code to spawn the cube:
GameObject cube = (GameObject)Instantiate(myPrefab, new Vector3(0, 10, 0), Quaternion.identity); NetworkServer.Spawn(cube);
The problem is, if a cube is spawned in World1, clients in World2 still spawn the cube. In hindsight this makes sense because the documentation for
Spawn the given game object on all clients which are ready.
My next idea was that I could designate one player in the minigame as a psuedo "host" and his machine would be responsible for running the AI. However, with this method, I really cant simulate things going on when nobody is in the scene.
Is there a better way I should go about solving this problem? How can I create a dedicated server which can simulate multiple scenes at once? Should each scene be handled by its own server? Am I better off just trying to run everything in one scene?