Understanding Set Custom Properites and CAS
The whole answer can be found below.
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).
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:
Back to top