Setting the color of the player prefab

Options
I'm trying to learn PUN and have created a very simple multiplayer game. I have a "character editor" where the player can set an index (between 0 and 10) that looks up different colors, and saves this in the PlayerPrefs list.

The multiplayer game works, and up to 6 players can join the game. But I want to learn how to best set the color of each spawned player prefab to the color that each player set in their character editor.

So player A sets color to index 2, player B sets color to index 6 and player C sets color to index 5. All stored in the local PlayerPrefs. When a new multiplayer game starts, I want each of the player game objects (spawned by the player prefab) to have the color that the real owner of the game object (photonView) chose.

How can I do this the "right" way?

Thanks!

Comments

  • Tobias
    Options
    There are (as always) many ways.
    One might be to set the color in the PhotonNetwork.player via SetProperties(). These player props get synced in a room and you can access any player's color. Use a short string as key for color (like "c") and set a value (like (byte)color).
    You can find all player's properties in the PhotonNetwork.players list.
  • Thanks! Sounds good, I noticed that there was a custom properties list on each player. I will try this when I get home again but then something like this might do the trick?

    (pseudo code)
    When a player joins a room I get the values from PlayerPrefs and store them on the players custom properties list:

    [code2=csharp]void OnJoinedRoom()
    {
    Debug.Log("We have joined a room.");

    int _cb = PlayerPrefs.GetInt(PlayerParts.Body, 1);

    PhotonNetwork.player.name = MultiplayerGameHelper.GetPlayerName();

    ExitGames.Client.Photon.Hashtable customCharProps = new ExitGames.Client.Photon.Hashtable();
    customCharProps.Add("cb",_cb);

    PhotonNetwork.player.SetCustomProperties(customCharProps);
    }[/code2]
    And then when a player prefab is spawned and the Start function runs:
    [code2=csharp]photonView.RPC("LoadPlayerCustomization", PhotonTargets.AllBuffered);[/code2]

    And then the RPC function looks like this:
    [code2=csharp][RPC]
    void LoadPlayerCustomization()
    {
    foreach(PhotonPlayer player in PhotonNetwork.playerList)
    {
    int _cb = (int)player.customProperties["cb"];

    [corret prefab].playerBody.color = CometCustomizeController.GetColor(_cb);
    }
    }[/code2]

    Am I on the right track or did I get the architecture wrong at this stage?
  • Ok, I tried this now and it seems to be working well!

    Thanks for the tip!

    However, is this an OK way to do this? It's just something that happens once when a MP game startes so the performance isnt really an issue, and all it does is to send over a few integers (4 pr. player).
  • Tobias
    Options
    Yes, this is a very OK way to do this. You can even set the props for color before you even join a game. They should get synced when you join and the others immediately know the color when they notice someone join.
    It's much less work than sending color every now and then and handling it yourself :)