Skip to content

Data Editor Usage Example

In this example, I'm going to show you how you can create a simple craft logic for your game. We gonna need just several Templates: Item, Recipe, and Ingredient. I assume you've read and implemented the basics for your game.

Here is the video of this tutorial.

Templates preparation

  1. Open Data Editor
  2. Select Templates Section and click on Create Template Screenshot
  3. Set Name as "Item" and leave other fields as default.
  4. Add the following parameters:
    • Name: (Type: String, Use in display name - YES)
    • Description: (Type: String)
    • When you have done, the list of parameters should look like this: Screenshot
  5. Create another Template for Ingredients. We'll make it a Component for convenience.
    • Name: Ingredient
    • Type: Component
  6. Add the following parameters:
    • Item: (Type: Document, Reference Template: Item)
    • Count: (Type: Integer, Default value: 1)
    • When you have done, the list of parameters should look like this: Screenshot
  7. The last template we gonna need is Recipe. Set Name as "Recipe" and leave other fields as default.
  8. Add the following parameters:
    • Item: (Type: Document, Reference Template: Item)
    • Ingredients: (Type: List, List Type: Document, Reference Template: Ingredient)
    • When you have done, the list of parameters should look like this: Screenshot

Create documents

Now, as we have our Templates ready, we need to add a couple of actual items and recipes. When you added the Templates, you should've noticed that in the navigation (on the left) two new sections appeared: Items and Recipe. The ingredient doesn't show up because it's a Component and exists only within another Document.

  1. In the Navigation Panel select Items
  2. Click on the Create button to add a new Item
  3. Create 3 items as shown below:
    Screenshot

Now we are going to make a recipe to create a Hammer using a Rock and Wood:

  1. In the Navigation Panel select Recipe
  2. Click on the Create button to add a new Recipe
  3. Select Item parameter as Hammer
  4. Add 2 ingredients: One Wood and one Rock
  5. It means that to create a Hammer, you need to spend one Wood and one Rock

Ok, we set all the data we need for the test project. Now we just need to publish our changes.

Deploy

  1. Select the Deploy section in the Navigation Panel
  2. Click Deploy and wait a couple of seconds until it's done

Every time you make any changes in the data, it'll be saved only inside of the Editor. If you want to push the changes to the game, you can do that in the Deploy Section. As a programmer, you can think of it as commit/push in the GIT system.

Code Generation

  1. Open Unity Project
  2. Import Balancy plugin
  3. Open editor window Tools->Balancy
  4. Input your GameId and PublicKey
  5. Click on Generate Code. It might take several seconds
  6. Once it's done, you can find a new folder with several scripts created at /Assets/Balancy/AutoGeneratedCode
  7. Don't change anything in that folder. The changes will be lost with the next generation

Balancy Initialization

You should read more about Balancy initialization here

  1. Create a new script DataEditorExample.cs
  2. Create a new scene and add this script to the Main Camera.
  3. In the Start method write the following code:

Balancy.BalancyNewInit.Init(new Balancy.AppConfig
{
   ApiGameId = YOUR_GAME_ID,
   PublicKey = YOUR_PUBLIC_KEY,
   Environment = Balancy.Constants.Environment.Development,
   OnReadyCallback = responseData =>
   {
       Debug.Log("Balancy Initialized: " + responseData.Success);
   }
});

Validate Data

First, let's check if we receive the correct data from the Editor. Add those methods to our script:

private void PrintAllData()
{
    PrintItems();
    PrintRecipes();
}

private void PrintItems()
{
    var items = Balancy.DataEditor.Items;
    Debug.LogWarning("Items Count = " + items.Count);
    foreach (var item in items)
        Debug.Log("ITEM: " + item.Name + " : " + item.Description);
}

private void PrintRecipes()
{
    var recipes = Balancy.DataEditor.Recipes;
    Debug.LogWarning("Recipes Count = " + recipes.Count);
    foreach (var recipe in recipes)
        Debug.Log("RECIPE to create item " + recipe.Item.Name + " requires " + recipe.Ingredients.Length + " other items");
}

Now we just need to call PrintAllData once Balancy was initialized. Call this method after the Debug.Log line:

Debug.Log("Balancy Initialized: " + responseData.Success);
PrintAllData();

Launch the game and you should see the following logs in the console:

Screenshot

Write Craft Logic

The next step is to write some logic to store the items you have and spend items to make craft items. I'll provide the whole code listing, so you could investigate it. Keep in mind that this example was made super easy on purpose. For the real project, you would want to organize the code better. And don't forget about SOLID principles. Good luck with coding!

DataEditorExample.cs

Next: What's next