Can custom room properties be arrays?
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).
Can custom room properties be arrays?
Spektre
2019-05-09 18:39:12
Base question: Can custom room properties be arrays and if so, can a player modify one element of it?
Context: I have a game where, in each round, players have a fixed span of time to complete a task as fast as they can. If they complete it before the time is up, they may try to complete it again to better their time, until that round's time has expired. Players may freely come and go throughout the game but ONLY those who were there at the round's start compete for rewards.
My idea is to store an array of players ids, and another array of players' times, corresponding to the id's in the previous array. When players complete the task, their time, if improved, is updated in the array.
Originally I mocked this up using player properties to hold their times, but they disappeared if a player left.
If Room custom properties cannot be an array, or if you quickly see a better structure, I'd appreciate any insight.
Comments
Follow up. I cannot find the page that lists the accepted data types for room properties.
Kaiserludi
2019-05-10 11:48:55
Hi @Spektre.
Yes, they can be values of room properties.
The top-level keys of room-properties must be strings. The values can be any type that is supported by Photons serialization. This includes arrays of supported types. A noteable exception is that the C# Clients don't support Dictionary-arrays, but arrays of all other supported types should work just fine.
Additionally to the types, which are supported out of the box, you can also register other types, including your own classes, through Photons CustomType interface to be able to also use them for example as room property values.
Thanks!
I could find the serilazeable types and good to know they are the same ones allowed as Room Properties.
It states Object arrays are supported (I assume so long as the object's public members are supported types). This could allow one structure to be saved that contained the player's IDs and times.
As for changing only one element of it, it would seem you need to reconstruct the whole element in order to make the hashtable so you're actually rewriting the whole object's value.
Kaiserludi
2019-05-10 12:58:05
All entries in a Object array must be supported types themselves.
The element at index 5 in the array might be a Hashtable, the element at index 7 a Dictionary, the one at index 14 a String array and so on, but you can't just put an instance of class Foo in your Object-array and expect Photon to successfully serialize it, unless you register the Foo class with Photon as a custom type.
A simple object with two public members string and float could not be serialized by Photon then? (As the object is not a supported type?)
Kaiserludi
2019-05-10 13:20:41
Correct.
Additionally Object refers specifically to the .NET framework Object class, not to some class in your application that might also have that name.
Hi @Spektre,
I wanted to add:
Hi John,
Thanks for the suggestion. I had considered separate room properties, but could not think of a good way to ensure they were all deleted when the next round started. The game could quickly accumulate many unneeded player past times.
In addition the times would no longer be valid, if the player left abruptly.
@Kaiserludi are you sure we can make and send arrays of any supported type?
Im trying to create a custom room property, an array of players, to keep track of the ones that have been defeated, but any time I try to update the value on the Network with
CurrentRoom.SetCustomProperties
or get it with
CurrentRoom.CustomProperties["propertyName"]
I get a null reference exception; what's wrong then? should I try another approach?
Please start a new thread for your issue. The previous post is 2 years old.
We need to know which client package you use and which version it is, too.
Hi @ChichoRD,
Thank you for choosing Photon!
I think this is a timing issue.
Read this:
By default, setting properties for actor or room properties will not take effect on the sender/setter client (actor that sets the properties) immediately when joined to an online room unlike what it used to be in PUN Classic. Now, instead, the sender/setter client (actor that sets the properties) will wait for the server event PropertiesChanged to apply/set changes locally. So you need to wait until OnPlayerPropertiesUpdate or OnRoomPropertiesUpdate callbacks are triggered for the local client in order to access them. The new behaviour is due to the introduction of the new room option flag roomOptions.BroadcastPropsChangeToAll which is set to true by default. The reason behind this is that properties can easily go out of synchronization if we set them locally first and then send the request to do so on the server and for other actors in the room. The latter might fail and we may end up with properties of the sender/setter client (actor that sets the properties) different locally from what's on the server or on other clients. If you want to have the old behaviour (set properties locally before sending the request to the server to synchronize them) set roomOptions.BroadcastPropsChangeToAll to false before creating rooms. But we highly recommend against doing this.
Kaiserludi
2021-03-29 16:42:21
Hi @ChichoRD.
@Kaiserludi are you sure we can make and send arrays of any supported type?
Yes, I am sure for built-in types of Photon.
However PUN adds its own types as Photon CustomTypes on top of Photon itself.
Im trying to create a custom room property, an array of players
Photon itself does not support sending instances of a 'Player' class. If PUN does support that type (Sorry, I don't know, which types PUN adds support for additionally to the ones that Photon itself supports), then it does so through Photons CustomType APIs.
As I have mentioned earlier in this thread, the Photon C# clients don't support arrays of custom types, so PUN can't add support for arrays of Players.
Hi @JohnTube
Finally after some thinking I reached the same conclusion as you, RPCs, custom properties and anything on the network can suffer from delays.
So unlike the normal code, which would execute from top to bottom; this we have to ensure that the reciever have indeed recieved the action before continuing our code.
Thanks for your answers and don't worry I've solved my problem
Back to top