Same Character for different player

Idodi

New member
Don't use ActorNumber because it can change when the same player returns to the same game (if they disconnected for example) and this will lead to a lot of problems.
Also this is not an Opsive matter, but rather a Photon matter and how you synchronize player properties within your game.

You should use CustomProperties like Archiniegas suggested.

You can read more about it in the Photon PUN Documentation here:

 

Vlaxep

Member
Select character code(OnClick event on button):
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class CharacterSelector : MonoBehaviour
{
  
    private ExitGames.Client.Photon.Hashtable _myCustomProperties = new ExitGames.Client.Photon.Hashtable();
    public void SelectCharacter(int index){
        PlayerPrefs.SetInt("MyCharacter", index);
        Debug.Log("Player prefs: "+ PlayerPrefs.GetInt("MyCharacter"));
        _myCustomProperties["ID"] = index;
        PhotonNetwork.LocalPlayer.CustomProperties = _myCustomProperties;
    }
}

Custom Character Spawn Manager
C#:
public class CharacterSpawnManager : SpawnManagerBase
{
    [SerializeField]
    protected GameObject[] m_Characters;

    protected override GameObject GetCharacterPrefab(Player newPlayer)
    {
      
        if(newPlayer.CustomProperties.ContainsKey("ID")){
            int result = (int)newPlayer.CustomProperties["ID"];
            Debug.Log("Character ID : "+result);
            return m_Characters[result];
        }else{
            return m_Characters[0];
        }
    }
}
Characters Prefabs are assigned manually in the editor to m_Characters.


Hope it helps you!
Hey @Arciniegas I'm really struggling with this for a slightly different use case.

I'm allowing players to change 'skin' in realtime by interacting with a UI or something along those lines.

When that occurs I simply need to SetCustomProperties for that player only and send to other clients to update that character's/player's Character model. So the prefab doesn't change only SkinnedMeshObjects are activated/deactivated in the correct combination.

My problem is I don't understand how to reference that Player or Character to sync with other clients.

Firstly I'm wondering whether it's best to reference by UserID which is Photon or just the actual Character which is Opsive (correct me if I'm wrong on this).

Secondly I can't wrap my head around how this is actually done (referencing the Client or CharacterPrefab that requires the update), and then how is this 'checked' against the Prefab in OnPlayerPropertiesUpdate()?

I guess the only difference here is mine cannot be determined by 'newplayer' as this will be instigated at a time of that client's choosing.
 

Vlaxep

Member
I'm thinking perhaps I should be using the 'PhotonView' class 'ViewID' to specify a reference to the particular object?

(I'm not great with code - still learning)
 
Last edited:

96698041

New member
Thank you soo much!!
Kisses!!!

Select character code(OnClick event on button):
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class CharacterSelector : MonoBehaviour
{
  
    private ExitGames.Client.Photon.Hashtable _myCustomProperties = new ExitGames.Client.Photon.Hashtable();
    public void SelectCharacter(int index){
        PlayerPrefs.SetInt("MyCharacter", index);
        Debug.Log("Player prefs: "+ PlayerPrefs.GetInt("MyCharacter"));
        _myCustomProperties["ID"] = index;
        PhotonNetwork.LocalPlayer.CustomProperties = _myCustomProperties;
    }
}

Custom Character Spawn Manager
C#:
public class CharacterSpawnManager : SpawnManagerBase
{
    [SerializeField]
    protected GameObject[] m_Characters;

    protected override GameObject GetCharacterPrefab(Player newPlayer)
    {
      
        if(newPlayer.CustomProperties.ContainsKey("ID")){
            int result = (int)newPlayer.CustomProperties["ID"];
            Debug.Log("Character ID : "+result);
            return m_Characters[result];
        }else{
            return m_Characters[0];
        }
    }
}
Characters Prefabs are assigned manually in the editor to m_Characters.


Hope it helps you!
Thank you sooo much.
 
Top