Steam User Names in the Main Menu Lobby?

Hello!

I think this lecture might be the last one, so I wanted to post a topic that mentioned the Steam User Names for the Main Menu Lobby. I thought you mentioned we were going to add these to our lobby?

If not, I’ll try to post a solution for anyone interested!

so i gave it the old college try, but it always just uses the steam user name of the host. so, in other words, when i host a game, it shows my username as the first person in the lobby. and then when a friend joins it shows that his username is my username as well… but this seems to be a little bit in the right direction.

in RTSNetworkManager i created a new event to listen on

public static event Action<RTSPlayer, string> ServerSetPlayerName;

and then in OnServerAddPlayer, i replaced

player.SetDisplayName($"Player {Players.Count}");

with

ServerSetPlayerName?.Invoke(player, $"Player {Players.Count}");

and then in the MainMenu, in Start, i subscribe with

RTSNetworkManager.ServerSetPlayerName += ServerHandleSetPlayerNameUpdated;

and my ServerHandleSetPlayerNameUpdated method looks like

    private void ServerHandleSetPlayerNameUpdated (RTSPlayer player, string serverDisplayName) {
        if (useSteam) {
            player.SetDisplayName(SteamFriends.GetPersonaName());
            return;
        }
        player.SetDisplayName(serverDisplayName);
    }

let me know if you can figure out how to get the proper steam username…

1 Like

Yes! I actually found one of the instructor’s videos on it! Here: How To Display Steam Profile Pictures & Names - Unity Multiplayer Tutorial

I believe the line you wrote:

player.SetDisplayName(SteamFriends.GetPersonaName());

only gets the host’s user name with the function GetPersonaName(). To get the user name of your friends, you need to edit that line to

player.SetDisplayName(SteamFriends.GetFriendPersonaName(steamId));

where steamId is the lobby member’s ID

CSteamID steamId = SteamMatchmaking.GetLobbyMemberByIndex(
   new CSteamID(callback.m_ulSteamIDLobby),
   numPlayers - 1 // this is the lobby ID of your friend, it syncs up with Mirror's lobby member ID
);

Yeah I just found that video earlier today!!! Yeah, I see what I was doing wrong. Thanks for the reply!!

1 Like

I just wanted to add my solution for anyone else that was curious! I did the same thing as lometur, but through some different code.

I first added a default property at the top RTSNetworkManager.cs

public static ulong LobbyId { get; set; }

This property then allows me to transfer the CSteamID from when the lobby is created inside of MainMenu.cs. Next in MainMenu.cs, I replaced the code inside the OnLobbyCreated(…) method with the following code…

if (callback.m_eResult != EResult.k_EResultOK)
{
   landingPagePanel.SetActive(true);
   return;
}

CSteamID lobbyId = new CSteamID(callback.m_ulSteamIDLobby);
RTSNetworkManager.LobbyId = lobbyId.m_SteamID;

NetworkManager.singleton.StartHost();

SteamMatchmaking.SetLobbyData(
   lobbyId,
   "HostAddress",
   SteamUser.GetSteamID().ToString()
);

Now that RTSNetworkManager.cs has access to the lobby ID, I can simply access its data inside its method OnServerAddPlayer(…)

public override void OnServerAddPlayer(NetworkConnection conn)
{
   ... // other code

   CSteamID steamId = SteamMatchmaking.GetLobbyMemberByIndex(
      new CSteamID(LobbyId),
      numPlayers - 1
   );

   player.DisplayName = $"{SteamFriends.GetFriendPersonaName(steamId)}";
}

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.