Worlds functionality can also be accessed through the PlayResolver
just like rooms. Worlds work a differently however and are a bit simpler.
First we need to fetch the available Worlds. Unlike Rooms, Worlds cannot be created by a Client and need to be setup in the Developer Portal.
FetchWorlds
in PlayResolver.cs
allows us to fetch the available Worlds for our project. This task returns a list of Worlds in the form of WorldsData
objects and a boolean that indicates if the operation was successful.
This method also calls EnsurePlayConnection
which initializes the needed mechanisms in thePlayResolver
if necessary. EnsurePlayConnection
can also be called directly for initialization.
FetchLocalWorld
in PlayResolver.cs
returns the local World for a local running World Server.
The WorldsConnectDialog
populates a dropdown with the Worlds returned by both of these methods so we can select a World.
After we've selected a World we can connect to it using:
JoinWorld
in PlayResolver.cs
connects the Client that we pass to the method to the World we pass to the method.
The isSimulator
optional parameter is used for Simulators and can be ignored for regular Client connections (see Simulators).
The WorldsConnectDialog
is an example implementation for Worlds usage.
When connected to a Room or a World, the Client can access the currently connected endpoint by accessing the Coherence.IClient.LastEndpointData
property of the CoherenceMonoBridge,
e.g. myBridge.Client.LastEndpointData
.
Rooms functionality can be accessed through the PlayResolver
which includes all the methods needed to use rooms.
To manage Rooms we must first decide which region we are working with.
FetchRegions
in PlayResolver.cs
allows us to fetch the regions available for our project. This task returns a list of regions (as strings) and a boolean that indicates if the operation was successful.
FetchLocalRegions
in PlayResolver.cs
returns the local region string for a local running Rooms Server, or null if the operation is un-successful (if the Server isn't running for example).
Every other Rooms API will require a region string that indicates the relevant region for the operation so these strings should not be changed before using them for other operations.
The RoomsConnectDialog
populates a dropdown with the region strings returned by both of these methods directly for easy selection.
These methods also call EnsurePlayConnection
which initializes the needed mechanisms in the PlayResolver
if necessary. EnsurePlayConnection
can also be called directly for initialization.
After we have the available regions we can start managing Rooms, for instance:
CreateRoom
in PlayResolver.cs
allows us to create a Room in the region we send it.
the RoomCreationOptions
is used to optionally specify:
a Room name
the maximum number of Clients allowed for the Room
a list of tags for Room filtering and other uses
a key-value collection for the Room
This task returns the RoomData
for the created Room assuming the operation was successful.
FetchRooms
in PlayResolver.cs
allows us to search for available Rooms in a region. We can also optionally specify tags for filtering the Rooms.
This task returns a list of RoomData
objects for the Rooms available for our specifications.
JoinRoom
in PlayResolver.cs
connects the client that we pass to the method to the Room we pass to the method. This RoomData
object can be either the one we get back from CreateRoom
or one of the ones we got from FetchRooms
.
When joining a Room, the method is optionally supplied if the connecting Client is a Simulator, as well.
The RoomsConnectDialog
demonstrates both of these cases in CreateRoom
when called with true for autoJoin and in JoinRoom
respectively.
RemoveRoom
in PlayResolver.cs
allows us to close a Room. The uniqueID can be either the one we get back from CreateRoom
or one of the ones we got from FetchRooms
, but roomToken (Room's Secret) is only returned by CreateRoom
.
Extending what can be synced from the Configure window
This is an advanced topic that aims to bring access to coherence's internals to the end user.
The Configure window lists all variables and methods that can be synced for the selected Prefab. Each selected element in the list is stored in the Prefab as a Binding
with an associated Descriptor
, which holds information about how to access that data.
By default, coherence uses reflection to gather public fields, properties and methods from each of the Prefab's components. You can specify exactly what to list in the Configure window for a given component by implementing a custom DescriptorProvider
. This allows you to sync custom component data over the network.
Take this player inventory for example:
Since the inventory items are not immediately accessible as fields or properties, they are not listed in the Configure window. In order to expose the inventory items so they can be synced across the network, we need to implement a custom DescriptorProvider
.
DescriptorProvider
The main job of the DescriptorProvider
is to provide the list of Descriptors
that you want to show up in the Configure window. You can instantiate new Descriptors
using this constructor:
name: identifying name for this Descriptor
.
ownerType: type of the MonoBehaviour that this Descriptor
is for.
bindingType: type of the ValueBinding class that will be instantiated and serialized in CoherenceSync, when selecting this Descriptor
in the Configure window.
required: if true, every network Prefab that uses a MonoBehaviour of ownerType will always have this Binding active.
If you need to serialize additional data with your Descriptor
, you can inherit from the Descriptor
class or assign a Serializable
object to Descriptor.CustomData
.
Here is an example InventoryDescriptorProvider
that returns a Descriptor for each of the inventory items:
To specify how to read and write data to the Inventory component, we also need a custom binding implementation.
Binding
A Descriptor
must specify through the bindingType which type of ValueBinding
it is going to instantiate when synced in a CoherenceSync
. In our example, we need an InventoryBinding
to specify how to set and get the values from the Inventory
. To sync the durability property of the inventory item, we should extend the IntBinding
class which provides functionality for syncing int values.
For the full list of supported binding types, see Supported types in Commands and Bindings.
We are now ready to sync the inventory items on the Prefabs.