Maps (more than a simple 'Game' scenes)

In GameInfo class (inside GameData class), we have a enum Map (with Default only).
I have added 2 maps more (3), and a dropdown so the player can select the map he wants (or a random one).

The problem I have seen is that the dedicated server goes to the ‘Game’ scene by default.
So if I want to have multiple maps, will I have to set a different scene name on the UGS servers?
For example, I have 5 servers on one machine, and I have 3 different maps.
Server 1 and 2 > map 0.
Server 3 and 4 > map 1.
Server 5 > map 2.
Would it be correct? (3 different dedicated server unity builds i want to say)

Because I imagine that if I change only the “Game” in HostGameManager line: NetworkManager.Singleton.SceneManager.LoadScene(“Game”, LoadSceneMode.Single);
will not work correctly multiplay on those maps.

Thanks in advance, I have learned and enjoyed the course very much!

Hi there,
Instead of using a const to set the game scene, you will need to make that a normal variable and set it depending on the map you have selected.
Does that make sense?

I show an image for better understanding.
This is the ApplicationController course script:

As you can see as a Dedicated Server, it starts and goes to the ‘Game’ scene. And there it stays according to the logic of the course.
When starting the game as a Player, it goes to the ‘MainMenu’, and the user click find match button. In the course goes to ‘Game’ scene too.
All works as expected in the course.

The doubt/question is how it should be for it to work properly to have different maps?

Have your different maps in different scenes with different scene names.
Then store the name of the map scene in the game data.
You can then altern the GameSceneName string to the correct name for the map you want to load. You might need to pass the game name into a couple methods to get it to the LoadGameSceneAsync method.
Does that make sense?

Yes, I was thinking to do that on the player’s side, but then the dedicated server I leave it as it is?

If I delete the ‘Game’ scene and create Game01, Game02, Game03, I guess I will have to change ‘something’ on the dedicated server to make it work.

For example, in the code we have this from the course: if (!IsServer)


which will run on the server in the case of a matchmake game or lobby host user (if he is himself the server) I think .

The problem is that on the dedicated server I don’t know what I should do to have multiple maps working properly.

The server is responsible for loading new scenes, so you specifically need to the update the gameScene string ON the server. It will sync which scene to load to the clients when it loads the scene.

But as I say on the dedicated server what is currently being done is to go to scene X. If I have also scene Y and Z, these will not work because the server is synchronizing just scene X (scene X will be the only one that will work, right?).
Do you understand me?
I don’t know how to make the dedicated server able to sync multiple scenes at the same time.

(That’s how I understood it according to the course, I don’t know if I’m wrong)

Thanks for your patience

Oh, I am sorry, when we are hosting a game, the game scene is loaded from the HostGameManager.cs, so that is where the string will need to be updated.

We call StartHostAsync in the HostGameManager from the MainMenu.

So I would add a map selection UI element to your MainMenu, then pass the string of the selected map to the HostGameManager when you host the game. The HostGameManager can then use the customized string to load the correct map.

This is what you will need to do. When the player selects a map, you will then connect them to the correct server.

There are ways to have a single build and then start the server with a command line parameter which will tell it which scene to load as its map. It’d be something like (taken from this stackoverflow post)

// Helper function for getting the command line arguments
private static string GetArg(string name)
{
    var args = System.Environment.GetCommandLineArgs();
    for (int i = 0; i < args.Length; i++)
    {
        if (args[i] == name && args.Length > i + 1)
        {
            return args[i + 1];
        }
    }
    return null;
}

You’d then run the server like this (I think)

mygame.exe -map map0
mygame.exe -map map1
mygame.exe -map map2

And use the above helper to find your setting and use that to tell the server which scene to load.

private async void Start()
{
    GameSceneName = GetArg("-map") ?? "map0";
}

I’m in another gamedev.tv course, when I finish it I’ll go back and look at what you say.
Thank you very much!
I will keep you updated if I succeed

1 Like

Privacy & Terms