Sites

Menu

Oerview

This document relates to the MonoBehaviour workflow.

Before reading this document, make sure you are familiar with:

Readers and Writers allow you to inspect and change the state of SpatialOS components using MonoBehaviours by letting you perform the following actions:

Reader

  • Read the data of a SpatialOS component.
  • Read the authority state of your worker over a SpatialOS component.
  • Register callbacks for reacting to property value changed of a SpatialOS component.
  • Register callbacks for reacting to events corresponding to a SpatialOS component.
  • Register callbacks for reacting to changes to the authority state of your worker over a SpatialOS component.

Writer

  • Change the property values of a SpatialOS component.
  • Send events defined in a SpatialOS component.
  • Send acknowledgements for AuthorityLossImminent notifications.
  • All the functionality that a Reader provides.

Note that a writer will only receive authority state change callbacks for AuthorityLossImminent.

For every SpatialOS component defined in schema, the GDK generates a Reader and Writer. After code generation has finished, the generated Readers and Writers are located in the following namespace:

  • <namespace of schema component>.<component name>Reader
  • <namespace of schema component>.<component name>Writer

You can use Readers and Writers by declaring a field in your MonoBehaviour and decorating it with the [Require] attribute (See documentation on interacting with SpatialOS using MonoBehaviours). The GDK automatically injects these fields with their corresponding Readers and Writers, if the following requirements are fulfilled:

  • A reader for a specific component can be injected as long as the worker has read access over this component.
  • A writer for a specific component can only be injected, if the worker has write access over this component.

You can find out more about how to work with Readers and Writers in:

Reader API

For the following API, let:

  • TComponentData be the component data type for the SpatialOS component of this Reader. (<namespace of schema component>.<component name>.Component)
  • TComponentUpdate be the update type for the SpatialOS component of this Reader. (<namespace of schema component>.<component name>.Update)

Fields:

Field Type Description
Data TComponentData The data stored inside the component that this Reader is associated with.
Authority Authority The authority status of the current worker of the component that this Reader is associated with.

Events:

event Action<TComponentUpdate> OnUpdate;

Register to this event to receive a callback whenever any property of a component was changed.

Callback parameter:

  • TComponentUpdate: This will contain fields wrapped inside the Option struct to identify which fields were changed.
event Action<TField> On{Name of TField}Updated;

Register to this event to receive a callback whenever a specific field of the component is updated.

Callback parameter:

  • TField: The type of this field is code-generated and depends on the schema definition of the field. It will contain the updated value of the specified field.

General OnUpdate callbacks are invoked before specific property update callbacks.

event Action<TEventPayload> On{Name of TEvent}Event;

Register to this event to receive a callback whenever the corresponding SpatialOS event is triggered on this SpatialOS entity.

Callback parameter:

  • TEventPayload: The type of the event payload for TEvent. This type depends on the schema definition of the event. It will contain the event payload.
event Action<Authority> OnAuthorityUpdate;

Register to this event to receive a callback whenever the authority status of your worker over the corresponding component is changed.

Callback Parameters

  • Authority: Contains the new authority status of the worker.

Writer API

Note that a Writer inherits from the Reader for the same SpatialOS component and such the Reader API is also available from a Writer.

For the following API, let:

  • TComponentData be the component data type for the SpatialOS component of this Reader. (<namespace of schema component>.<component name>.Component)
  • TComponentUpdate be the update type for the SpatialOS component of this Reader. (<namespace of schema component>.<component name>.Update)

Methods

void SendUpdate(TComponentUpdate update);

Allows you to send a component update to the SpatialOS Runtime.

Parameters:

  • TComponentUpdate update: An update object which contains the changed field data. Any field that should not change, should not be set.
void Send{name of event}Event(TEventPayload payload);

Allows you to send an event. These methods are code-generated for each event in your component.

Parameters:

  • TEventPayload payload: The data that you want to send with your event. The exact type is specified by the schema definition of the event.
void AcknowledgeAuthorityLoss();

Allows you to send acknowledgements for AuthorityLossImminent notifications.

Search results

Was this page helpful?

Thanks for letting us know!

Thanks for your feedback

Need more help? Ask on the forums