How to add and remove fields from an entity [Unity]

First of all, make sure you have:

  1. Created a collection in the Gamedonia Dashboard for the entity.
  2. Logged in a user in Gamedonia. Without a user login you won' be able to make any calls to GamedoniaData.

You can also check this article about creating your first entity in Gamedonia.

Now on to modify the structure of an entity. Let's say you have a collection called "heroes" and entities with this structure:

{
  "name": "Dragon3",
  "experience": "54",
  "rank": "Captain",
  "inventory": 
    {
      "weapon": "sword1",
      "shield": "shield3",
      "helmet": "helmet8"
    }
}
What we are going to do now is create an entity with this structure and data, and then update it to:
  • Add a new field called spell.
  • Remove the field shield from the inventory.

To add a new field, we get the entity from the callback of the Create call and store it in a new Dictionary called newHero. Then we set a new field called spell.

To remove a field, we need to send the whole enclosing structure in overwrite mode (the update method has an overwrite parameter). In this case, to remove the shield entry, we send the whole inventory structure with the same data in the weapon and helmet fields but without a shield entry.

The code would look like this:

// We fill a Dictionary with a hero and his inventory
Dictionary<string,object> hero = new Dictionary<string,object>(); 
Dictionary<string, object> inventory = new Dictionary<string, object>();
// Inventory
inventory["weapon"] = "sword1";
inventory["shield"] = "shield3";
inventory["helmet"] = "helmet8";
// Hero
hero["name"] = "Dragon3";
hero["experience"] = 54;
hero["rank"] = "Captain";
hero["inventory"] = inventory;
// Create the hero
GamedoniaData.Create("heroes", hero, delegate (bool success, IDictionary data) {
    if (success) {
        // If we created the hero successfully, we create a Dictionary to overwrite the hero
        Dictionary <string, object> newHero = (Dictionary<string, object>) data;
        // We add a new field called "spell"
        newHero["spell"] = "fireball";
        Dictionary <string, object> oldInventory = (Dictionary<string, object>) newHero["inventory"];
        Dictionary <string, object> newInventory = new Dictionary<string, object>();
        newInventory["weapon"] = oldInventory["weapon"];
        newInventory["helmet"] = oldInventory["helmet"];
        // The inventory of the hero is now the old one without the "shield" entry
        newHero["inventory"] = newInventory;
        // We update the hero entity
            GamedoniaData.Update("heroes", newHero, delegate (bool up_success, IDictionary up_data) {
            if (success){
                //TODO Your update success processing
            } 
            else{
                //TODO Your update fail processing
            }
        }, true);
    }
    else{
        //TODO Your create fail processing
    }
});

More info here: Data storage guide