Understanding Set Custom Properites and CAS

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Understanding Set Custom Properites and CAS

DevJed
2021-10-16 09:03:15

Hi there

I am just looking over the Photon documenation and want to verify if what I am doing is correct.

When I join a new room, I am creating a new Hashtable for the room and using SetCustomProperties so it is accesable between all players:

Hashtable roomCustomProperties = new Hashtable();    
roomCustomProperties.Add("woodFuel", 0);    
roomCustomProperties.Add("coalFuel", 0);    
PhotonNetwork.CurrentRoom.SetCustomProperties(roomCustomProperties);    

Now if a player collects wood, I am calling this method:

public void ResourceGathered()    
{    
    woodFuel = (int)PhotonNetwork.CurrentRoom.CustomProperties["woodFuel"];    
    woodFuel++;    
        
    PhotonNetwork.CurrentRoom.SetCustomProperties(new Hashtable() { { "woodFuel", woodFuel } });    
}    

I am then using the OnRoomPropertiesUpdate to update the UI text for all players.

This works across 2 differnt builds. Is this above implementation correct?

CAS: Now if I wanted the piece of wood to be a unique item, so only one in the scene and it belongs to one player how would I do this? I have seen that you add a expectedProperties hashtable to the SetCustomProperties but how would I go around doing this?

Comments

[Deleted User]
2021-10-17 20:21:03

Hi, players also have custom properties just like the room does, you could store the unique wood in their player custom properties for the sake of continuity. Your implementation is fine, but please be aware that type casting can potentially be dangerous in a production environment where bad actors might have influence over your game code. If your game is competitive, make sure you error handle these types of cast operations thoroughly.

Please see the documentation for player properties:

Photon Unity Networking 2: Player Class Reference
Custom Properties are a set of string keys and arbitrary values which is synchronized for the players in a Room. They are available when the client enters the room, as they are in the response of OpJoin and OpCreate.

Back to top