Skip to main content
Version: 5.15.0

Storing user currency information on the server

Securely store users' valuable in-game currency on the server for safe and reliable access.
Follow each step, from configuring the server storage to requesting data modifications from the client, with the comprehensive guide.

1. Before getting started

Users must exist.

You can easily create a user by clicking on [Create Game User] in the 'User' on the top of the menu.
Create a user with the ID 'user1' and the password '1234'.

create user

2. Configuring server storage

Create a table specifically for storing currency information.

Click on [Create Table] in the top menu of the 'Game information' section in the BACKND console.
Since currency data is sensitive, set the table as a Private table, allowing only the user to access it. Create the table without using a schema, name it "GAME_MONEY", and proceed with the creation.

create table

3. Sending and receiving data from the client

Create a script.

Create a new script and name it 'GameMoney.cs'.
Open the script and modify the contents as follows:

using System.Collections.Generic;
using System.Text;
using UnityEngine;

// Add BACKND SDK namespace
using BackEnd;

public class GameMoney {
public int gold = 100;
public int diamond = 1;
public int heart = 10;

// This is a method for data debugging. (Debug.Log(GameMoney);)
public override string ToString() {
StringBuilder result = new StringBuilder();
result.AppendLine($"gold : {gold}");
result.AppendLine($"diamond : {diamond}");
result.AppendLine($"heart : {heart}");
return result.ToString();
}
}

public class BackendGameData {
private static BackendGameData _instance = null;

public static BackendGameData Instance {
get {
if(_instance == null) {
_instance = new BackendGameData();
}

return _instance;
}
}

public static GameMoney gameMoney;

public void InitializeGameMoney() {
// Step 1. Reset game currency
}

public void GetGameMoney() {
// Step 2. Load game currency
}

public void IncreaseHeart() {
// Add heart
}

public void UpdateGameMoney() {
// Step 3. Modify game currency
}
}

Write a method to reset game currency.

When a new user account is created, call a method to set the user's game currency to its initial value.
Write the method as follows:

public void InitializeGameMoney() {
// Step 1. Reset game currency

if(gameMoney == null) {
gameMoney = new GameMoney();
}

Debug.Log("Resets data.");
gameMoney.gold = 100;
gameMoney.diamond = 1;
gameMoney.heart = 10;

Param param = new Param();
param.Add("gold", gameMoney.gold);
param.Add("diamond", gameMoney.diamond);
param.Add("heart", gameMoney.heart);

Debug.Log("Add a new data row to the 'GAME_MONEY' table.");
var bro = Backend.GameData.Insert("GAME_MONEY", param);

if(bro.IsSuccess()) {
Debug.Log("Successfully added data. : " + bro);
} else {
Debug.LogError("Failed to add data. : " + bro);
}
}

Write a method to fetch data from the server.

public void GetGameMoney() {
// Step 2. Load game currency

Debug.Log("Call the method to retrieve data from the "GAME_MONEY" table.");
var bro = Backend.GameData.GetMyData("GAME_MONEY", new Where());
if(bro.IsSuccess()) {
Debug.Log("Data lookup successful : " + bro);

// Receive data returned in JSON format.
LitJson.JsonData gameDataJson = bro.FlattenRows();

// If the number of returned data cases is 0, the data does not exist.
if(gameDataJson.Count <= 0) {
Debug.LogWarning("The data does not exist.");
} else {
gameMoney = new GameMoney();
gameMoney.gold = int.Parse(gameDataJson[0]["gold"].ToString());
gameMoney.diamond = float.Parse(gameDataJson[0]["diamond"].ToString());
gameMoney.heart = gameDataJson[0]["heart"].ToString();
Debug.Log(gameMoney.ToString());
}
} else {
Debug.LogError("Failed to look up data. : " + bro);
}
}

Write a function to update the data on the server.

public void IncreaseHeart() {
// Add heart

Debug.Log("Increases the heart by 1.")
gameMoney.heart += 1;
}

public void UpdateGameMoney() {
// Step 3. Modify game currency

if(gameMoney == null) {
Debug.LogError("Data does not exist. Use 'Initialize' or 'Get' to create data.");
return;
}

Param param = new Param();
param.Add("gold", gameMoney.gold);
param.Add("diamond", gameMoney.diamond);
param.Add("heart", gameMoney.heart);

BackendReturnObject bro = null;

Debug.Log("Modify my data.");
bro = Backend.GameData.Update("GAME_MONEY", new Where(), param);

if(bro.IsSuccess()) {
Debug.Log("Successfully modified data. : " + bro);
} else {
Debug.LogError("Failed to modify data. : " + bro);
}
}

Call method from BackendManager.cs.

To ensure that the method you wrote executes, it should be called from the BackendManager, which runs automatically when the game is launched.
Make sure to call the methods after the BACKND initialization and backend login have been completed.

using UnityEngine;
using System.Threading.Tasks;

// Add BACKND SDK namespace
using BackEnd;

public class BackendManager : MonoBehaviour {
void Start() {
var bro = Backend.Initialize(); // Initialize BACKND

// Response value for BACKND initialization
if(bro.IsSuccess()) {
Debug.Log("Initialization successful : " + bro); // If successful, 'statusCode 204 Success'
} else {
Debug.LogError("Initialization failed : " + bro); // If failed, a 4xx statusCode error occurs
}

Test();
}

// A method that allows synchronous methods to be called from asynchronous methods (cannot be accessed by the Unity UI)
async void Test() {
await Task.Run(() => {
BackendLogin.Instance.CustomLogin("user1", "1234"); // Log in to BACKND

BackendGameData.Instance.GetGameMoney(); // Resets data.

// When the data to load does not exist, new data is created.
if(BackendGameData.gameMoney == null) {
BackendGameData.Instance.InitializeGameMoney();
}

BackendGameData.Instance.IncreaseHeart(); // Change locally saved data.

BackendGameData.Instance.UpdateGameMoney(); // Update data saved in the server.

Debug.Log("Test complete.");
});
}
}