1
votes

I'm trying to create a co-operative first person multiplayer game and I think I've misunderstood how Photon and RPC is supposed to work. I have a working first person multiplayer project where player avatars can see each other and move around as the player avatars have a photon view photon transform view. As part of the player controller script (handles player movement only for local player), which is attached to the player avatar prefab, I have an attribute:

public PlayerCharacterInfo myCharacter;

which contains all of the player's info and stats including name, level, currentHP, maxHP, etc which is added to the player avatar's PlayerController as it the player enters the room and their avatar is instantiated.

When I join a multiplayer room, each player can only see their own stats. For example, in the editor when running the game, only the values of the local editor player avatar are shown on the Player Controller myCharacter. I can see that the other player avatars have an instantiated myCharacter on them but no values are shown. At this point, I figure I just need to have an RPC function like this in my player controller that just reassigns the myCharacter to itself so it can be broadcasted to all:

[PunRPC]
void RPC_AddCharacter(PlayerCharacterInfo paramCharacter)
{
    myCharacter = paramCharacter;
}

in void Start():

if(PV.IsMine)
{
    PV.RPC("RPC_AddCharacter", RpcTarget.All, myCharacter);
}

But this does not let me see the myCharacter values for non-local player avatars.

My goal is to first show the name of the player avatar that you're looking at via a raycast, but I cannot even get this data to sync.

Have I got the right idea here, but I'm executing it wrong? Or is this just not how Photon works? Do I need to instead have each player's PlayerCharacterInfo stored in the room controller for each player or something like that?

2

2 Answers

1
votes

You want to use Custom Properties.

Photon's Custom Properties consist of a key-values Hashtable which you can fill on demand. The values are synced and cached on the clients, so you don't have to fetch them before use. Changes are pushed to the others by SetCustomProperties().

How is this useful? Typically, rooms and players have some attributes that are not related to a GameObject: The current map or the color of a player's character (think: 2d jump and run). Those can be sent via Object Synchronization or RPC, but it is often more convenient to use Custom Properties.

1
votes

Solved somewhat! I removed my RPC call that was trying to to sync my custom PlayerCharacterInfo object and instead made a call and new string 'myCharacterName' in PlayerController to hold just the player's name.

The new RPC call takes the local player's myCharacter.characterName (from PlayerCharacterInfo) and sets the myCharacterName and syncs to all. This works and my editor player can see the myCharacterName of other players! However I was no closer to syncing my entire myCharacter class.

Now that I had the right idea about how this works, I was able to research a bit more and found that Photon can't sync custom classes normally, but you could serialize your class to be able to send it: https://doc.photonengine.com/en-us/realtime/current/reference/serialization-in-photon

However reading this made me realize that I probably don't need to sync my entire player data (it contains not only health and status, but inventory, equipment, quest, etc info) and that I'm probably better off just syncing separate data types in separate calls.

This thread helped too: https://forum.photonengine.com/discussion/880/custom-class-object-sent-over-rpc