Microservice Storage - Code

Create, deploy, and manage databases from within Unity [PROF-MicroStorage-03]

Here are examples which cover common programming needs.

Examples

The Beamable Example Project is a collection of code snippets. Each snippet demonstrates real-world usage of a specific feature or service in an isolated Unity Scene with a MonoBehaviour.

📘

Beamable SDK Examples

The following example code is available for download at SDK Examples (Beamable Microservices Storage Examples)

UserDataStorage

A custom child of MongoStorageObject is defined to wrap the database. The UserMessage is added as the high-level object to be stored in the database.

using Beamable.Server;
using MongoDB.Bson;

namespace Beamable.Server
{
    [StorageObject("UserDataStorage")]
    public class UserDataStorage : MongoStorageObject
    {

    }

    public class UserMessage
    {
        public ObjectId Id;
        public string Message;
        public int X;
        public int Y;
    }
}

UserDataService

The Beamable Microservice UserDataService is created to wrap all calls to the database.

See Microservices for more info.

using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Driver;
using UnityEngine;

namespace Beamable.Server
{
   [Microservice("UserDataService")]
   public class UserDataService : Microservice
   {
      [ClientCallable]
      public async Promise<bool> SaveMessage(string message, int x, int y)
      {
         bool isSuccess = false;

         try
         {
            var db = await Storage.GetDatabase<UserDataStorage>();
            var collection = db.GetCollection<UserMessage>("messages");
            collection.InsertOne( new UserMessage()
            {
               Message = message,
               X = x,
               Y = y
            });
            
            isSuccess = true;
         }
         catch (Exception e)
         {
            Debug.LogError(e.Message);
         }

         return isSuccess;
      }
      
      [ClientCallable]
      public async Promise<List<string>> GetMessage(int x, int y)
      {
         var db = await Storage.GetDatabase<UserDataStorage>();
         var collection = db.GetCollection<UserMessage>("messages");
         var messages = collection
            .Find(document => document.X == x && document.Y == y)
            .ToList();

         return messages.Select(m => m.Message).ToList();
      }
   }
}

UserDataMicroServiceExample

using System.Collections.Generic;
using UnityEngine;
using Beamable.Server.Clients;

namespace Beamable.Examples.Features.Microservices.UserDataMicroServiceExample
{
    /// <summary>
    /// Demonstrates <see cref="Microservices"/>.
    /// </summary>
public class UserDataMicroServiceExample : MonoBehaviour
{
    //  Properties  -----------------------------------
        
    //  Fields  ---------------------------------------
    private UserDataServiceClient _userDataServiceClient = null;
        
    //  Unity Methods  --------------------------------

    protected void Start()
    {
        Debug.Log("Start() Instructions...\n" + 
        "* Complete docker setup per https://docs.beamable.com/docs/microservices-feature-overview\n" +
        "* Start the server per https://docs.beamable.com/docs/microservices-feature-overview\n" +
        "* Play This Scene\n" + 
        "* View the Unity Console output\n" + 
        "* Enjoy!\n\n\n");
            
        SetupBeamable();
    }
        
        //  Methods  --------------------------------------
        private async void SetupBeamable()
        {
            var beamContext = BeamContext.Default;
            await beamContext.OnReady;

            Debug.Log($"beamContext.PlayerId = {beamContext.PlayerId}");
            
            _userDataServiceClient = new UserDataServiceClient();
            
            // #1 - Call Microservice
            bool isSuccess = await _userDataServiceClient.SaveMessage("Hello World!", 0, 0);
                
            // #2 - Result = true
            Debug.Log ($"SaveMessage() isSuccess = {isSuccess}");
            
            // #3 - Call Microservice
            List<string> messages = await _userDataServiceClient.GetMessage(0, 0);
                
            // #4 - Result = true
            Debug.Log ($"GetMessage() messages.Count = {messages.Count}, messages[0] = {messages[0]}");
        }
    }
}

Samples

Beamable Sample Projects are created to demonstrate real-world usage of specific features.

Each Sample Project repo is a full and complete game.

📘

Beamable Samples

The sample code is available for download at Chat & MicroStorage Sample Game (GPW2)

Scene01Intro

Scene02Game

Scene03Chat

Scene04Settings

Scene05Leaderboard


Did this page help you?