#Session Player Properties

1 messages · Page 1 of 1 (latest)

dim bloom
#

Youve got it backwards in the session options here. It would be new("name",playerProperty) That will set the hosts player name. The other player could do the same with JoinSessionOptions

If you want to change them after joining then you would use
session.CurrentPlayer.SetProperty("name", new("[changed name]", VisibilityPropertyOptions.Public)); session.SaveCurrentPlayerDataAsync();

There are events you can listen for when player properties change. The docs a bit light but it just when 1.0 release a few days ago. Hopefully in the next month with Unity 6 releasing they will give the docs another pass

granite acorn
#

so, like this?

#

I don't really get it

#

:ccc

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

    try
    {
        PlayerProperty playerName = new("PlayerName", VisibilityPropertyOptions.Public);

        var options = new SessionOptions
        {
            PlayerProperties = new("_playerName", playerName),
            MaxPlayers = 2
        }.WithRelayNetwork();

        var session = await MultiplayerService.Instance.CreateSessionAsync(options);

        session.CurrentPlayer.SetProperty(AuthenticationManager.Instance.PlayerName(), playerName);

        RoomFound = true;
        _hostNameText.text = SecurePlayerPrefs.GetString("_displayName", "");
        _lobbyRoomCodeText.text = session.Code;
    }
    catch (SessionException e)
    {
        Debug.Log(e);
    }
}```
dim bloom
granite acorn
#

oh

#

so this

#

so the green stays?

#

but then, when joining the lobby, "playerName (PlayerProperty)" is not existent

#

so it would be a variable like this, outside my function?

#

and then calling it like this?

#

JoinRoom()

#

is this correct?

#
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);

        session.CurrentPlayer.SetProperty(AuthenticationManager.Instance.PlayerName(), _playerName);

        RoomFound = true;
        _hostNameText.text = SecurePlayerPrefs.GetString("_displayName", "");
        _lobbyRoomCodeText.text = session.Code;
    }
    catch (SessionException e)
    {
        Debug.Log(e);
    }
}

public async void JoinRoom(string roomCode)
{
    Debug.Log($"Is room found already?: {RoomFound}");
    RoomFound = false;

    try
    {
        var session = await MultiplayerService.Instance.JoinSessionByCodeAsync(roomCode);
        session.CurrentPlayer.SetProperty(AuthenticationManager.Instance.PlayerName(), _playerName);
        _guestNameText.text = AuthenticationManager.Instance.PlayerName();
        RoomFound = true;
        _lobbyRoomCodeText.text = session.Code;
    }
    catch (SessionException e)
    {
        Debug.Log(e);
    }
}```
dim bloom
#

The first parameter of SetProperty() is the key string. so it will be "PlayerName" or whatever. the second part will be the PlayerProperty that contains the actual user name

granite acorn
#

oh

#

so, I would have to... hmm

#

it would be like this?

            session.CurrentPlayer.SetProperty("PlayerName", new("-Player name here-", Public));```
#

like this?

#
session.CurrentPlayer.SetProperty("PlayerName", new(AuthenticationManager.Instance.PlayerName()));```
dim bloom
#

Right, either of those will work. the visibility is Public by default.

granite acorn
#

fair enough

#

and then you said, save the player data

#

like this

dim bloom
#

right. saving it basically will upload the data to the session so other players can see it

granite acorn
#

oh, cool!

#

and now

#

XD

#

to get it?

#

session.Host.GetProperty("PlayerName"); XD?

#

that doesn't exist :'v

dim bloom
#

no, session.Players is a list of all players.

#

You can loop through them and get player.Properties["PlayerName"] for each player

#

There is also a session.PlayerPropertiesChanged that you can subscribe to get when ever a player property has changed.

granite acorn
dim bloom
#

that change _hostnameText to the last player that joined

#

if you only have 2 player then that will be fine

granite acorn
#

I will implement spectating later on but

#

for now that would work I guess

#

hmm what would be to get the property value?

granite acorn
#

which, I have the OnPlayerConnected already suscribed

granite acorn
#

I think I got it

#

it works for the Guest

#

but the host still doesn't have the Guest name when they're joining hmm 😦

#

this is such a mess, and it's just the lobby

#

I haven't even gotten to the gameplay part xDDDDDDDD

dim bloom
#

The host need to sub to the Player joined event as well

granite acorn
#

buff

#

I'll be back tonight... 😦

#

hey evilotaku, do you have a snippet of code or something I can guide myself with?

#

I'm taking 3 days just to have a lobby and player names displayed