#```Trying to get my players names shown

1 messages · Page 1 of 1 (latest)

blissful edge
#
public async void CreateRoom()
{
    // Check if player is in a lobby already, if so quit it
    RoomFound = false;

    try
    {
        var options = new SessionOptions
        {
            MaxPlayers = 2
        }.WithRelayNetwork();

        var session = await MultiplayerService.Instance.CreateSessionAsync(options);
        RoomFound = true;
        _currentSession = session;
        session.PlayerPropertiesChanged += UpdateNames;
        session.CurrentPlayer.SetProperty("PlayerName", new(AuthenticationManager.Instance.PlayerName()));
        await session.SaveCurrentPlayerDataAsync();
        _hostNameText.text = AuthenticationManager.Instance.PlayerName();
        _lobbyRoomCodeText.text = session.Code;
    }
    catch (SessionException e)
    {
        Debug.Log(e);
    }
}

public async void JoinRoom(string roomCode)
{
    RoomFound = false;

    try
    {
        var session = await MultiplayerService.Instance.JoinSessionByCodeAsync(roomCode);
        RoomFound = true;
        _currentSession = session;

        session.PlayerPropertiesChanged += UpdateNames;
        session.CurrentPlayer.SetProperty("PlayerName", new(AuthenticationManager.Instance.PlayerName()));
        await session.SaveCurrentPlayerDataAsync();

        _guestNameText.text = AuthenticationManager.Instance.PlayerName();
        _lobbyRoomCodeText.text = session.Code;
    }
    catch (SessionException e)
    {
        Debug.Log(e);
    }
}
}```
#
void UpdateNames()
{
    print(_currentSession.PlayerCount + " players in lobby.");

    foreach (var player in _currentSession.Players)
    {
        var playerName = player.Properties["PlayerName"].Value;
        var playerId = player.Id;

        if (player != _currentSession.CurrentPlayer)
        {
            Debug.Log($"Player name is {playerName}");
            if (_currentSession.Host == playerId)
                _hostNameText.text = playerName;
            else
                _guestNameText.text = playerName;
        }
        else
        {
            Debug.Log("This is the current player");
        }
    }
}```
#

this is my code

#

both names show on the Client (Build), but not for the Host (Unity)

#

my thought is that, the property (name) is being changed and "UpdateNames" is being called

#

before the csharp await session.SaveCurrentPlayerDataAsync();

#

however I can't think of a way to work this around, or if that's the actual problem

tepid beacon
#

huh, So UpdateNames on the host isn't finding "PlayerName" in the Player Properties?

blissful edge
tepid beacon
#

Its not going to be a drag and drop solution. You would need use the Widget package for it work properly

blissful edge
#

bet

#

I'm 100% willing to learn doing this

#

it's just... not being a good learning experience I feel xD

blissful edge
#

I guess I was right

#

it is the timing this is being called

#

I did this for testing, and it worked

#

which I assume I could just make a coroutine to update it a few seconds later

#

what do you say about that @tepid beacon ?

#
void UpdateNames()
{
    float transitionDuration = 1f;

    if (_updateTextOnPlayerJoined != null)
        _updateTextOnPlayerJoined.Kill();

    _updateTextOnPlayerJoined = _readyButton.image.DOFade(1, transitionDuration).From(1).OnComplete(() =>
    {
        foreach (var player in _currentSession.Players)
        {
            if (player != _currentSession.CurrentPlayer)
            {
                var playerId = player.Id;
                var playerName = player.Properties["PlayerName"].Value;

                if (_currentSession.Host == playerId)
                    _hostNameText.text = playerName;
                else
                    _guestNameText.text = playerName;
            }
        }
    });
}```
#

to keep it a void, I used this, with DoTween

#

the "OnComplete" on something that's not changing

#

:v it works... but I need opinion on how bad this is... xDDDDDDD