Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetAvailableRooms doesn't deserialise metadata correctly #51

Closed
gioragutt opened this issue Nov 9, 2018 · 1 comment
Closed

GetAvailableRooms doesn't deserialise metadata correctly #51

gioragutt opened this issue Nov 9, 2018 · 1 comment

Comments

@gioragutt
Copy link

gioragutt commented Nov 9, 2018

When I set my room metadata, even at Client.cs:220 you can see that metadata is null in the received object.

  onInit(options: any): void {
    this.setState(this.initialPlayerState());
    this.maxClients = options.maxClients || MAX_PLAYERS_IN_ROOM;
    this.setMetadata({
      mode: options.mode || '',
      name: options.name || `Room ${this.roomId}`,
    });
  }

I tried JSON.stringifying the metadata object, still don't get a string on the client side.

@gioragutt gioragutt changed the title GetAvailableRooms doesn't deserialises metadata GetAvailableRooms doesn't deserialise metadata correctly Nov 9, 2018
@endel
Copy link
Member

endel commented Dec 2, 2019

As of 0.11.4, it is possible to retrieve the metadata from the client-side by defining a custom RoomAvailable + Metadata types.

Example:

[Serializable]
class Metadata
{
	public string mode;
	public string name;
}

[Serializable]
class CustomRoomAvailable : RoomAvailable
{
	public Metadata metadata;
}
var roomsAvailable = await client.GetAvailableRooms<CustomRoomAvailable>(roomName);

for (var i = 0; i < roomsAvailable.Length; i++)
{
    // getting metadata
    Debug.Log("metadata.mode: " + roomsAvailable[i].metadata.mode);
    Debug.Log("metadata.name: " + roomsAvailable[i].metadata.name);
}

The metadata comes from a JSON response from the server. Unfortunately, the built-in JsonUtility from Unity has limitations regarding the usage of generic types, making it necessary to create a new type having its own hardcoded Metadata type defined on it. (here's the method to decode the JSON response)

If you think the API can be improved please don't hesitate to share your thoughts.

@endel endel closed this as completed Dec 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants