Player Centric API - Code

Provide power and flexibility to the game maker

Code

Getting The Context

The Beamable Player-Centric API has multiple ways to reference the API.

Syntax
var beamContext = BeamContext.Default;Default. This is suggested for most use-cases
var beamContext = BeamContext.ForContext("MyPlayer01");Ideal for games with 2+ local players
var beamContext = BeamContext.ForContext(this);Traverses the hierarchy for relevant configuration data. Advanced Users Only

Beamable Player-Centric API

The Beamable Player-Centric API continues to offer a flexible API reference syntax of Async / Await, Callbacks, Coroutines, as well as Synchronous.

One Local Player
Here the game maker references the context synchronously.
Note that the PlayerId may be null during the initialization of the Beamable system.

private void MyMethodViaSynchronous()
{
    var beamContext = BeamContext.Default;
    Debug.Log($"beamContext.PlayerId = {beamContext.PlayerId}");
}

One Local Player
Here the game maker references the context asynchronously to guarantee the value PlayerId will be set.

private async void MyMethodViaAsynchronous()
{
    var beamContext = BeamContext.Default;
    await beamContext.OnReady;
    Debug.Log($"beamContext.PlayerId = {beamContext.PlayerId}");
}

Two or More Local Players
Here the game maker references 2 (or more) contexts. This is ideal for advanced use-cases such as couch-coop games.

private void MyMethodViaSynchronous()
{
    var beamContext01 = BeamContext.ForPlayer("MyPlayer01");
    Debug.Log($"beamContext01.PlayerId = {beamContext01.PlayerId}");

    var beamContext02 = BeamContext.ForPlayer("MyPlayer02");
    Debug.Log($"beamContext02.PlayerId = {beamContext02.PlayerId}")
}

After that setup, game makers use the BeamContext object as the main entry-point to Beamable functionality. Benefits of this API include that it relates to one or more local players and most Beamable functionality within may be called asynchronously or synchronously. Game makers may choose from the syntax options above (and mix-and-match them) to find what that works best for their needs.

Observing Players
Players can be "observed" by other players, allowing them limited access to some of their player data. such as public stats.

var otherPlayer = beamContext.ObservePlayer(otherPlayerId);
var otherPlayerStats = otherPlayer.Stats;

Attaching Service Callbacks

Here the game maker references the context and subscribes to service changes via callback. The common Beamable services support various callbacks.

Currency OnUpdated Callback

private async void MyMethodForCurrencyCallback()
{
    var beamContext = BeamContext.Default;
    beamContext.Inventory.Currencies.OnUpdated += () => 
   {
        Debug.Log("The currency has been modified.");
   };
}

Common Service Callbacks

NameDetail
OnLoadingStartedInvoked when the service starts loading.

Note: between the loading start and loading finish, a data update may occur, however that does not mean the service is fully finished loading.
OnLoadingFinishedInvoked when the service finishes loading.

Example:
beamContext.Inventory.Currencies.OnLoadingFinished
OnUpdatedInvoked when some change is detected in the data contained in the service. This event has no arguments, so it is only a notification with no data.

Example:
beamContext.Inventory.Currencies.OnUpdated
OnDataUpdatedInvoked directly after OnUpdated, but contains a list of the internal data. The event argument is of type List<T>.

Example:
beamContext.Inventory.Currencies.OnDataUpdated, which passes type List<PlayerCurrency>