GDE Example: Upgrades

Posted October 16, 2015

Today I’m going to use GDE to manage Upgrades. If you’d like to use these Schemas, download the Data File and import it into GDE or download and import the Google Spreadsheet.

Let’s say my game has Potions. As you go through this example, keep in mind, this concept can apply to any upgradable item. The ideas I’ll lay out can work for Power Ups, Weapons, Armor, etc.

I’ve created a simple schema called Potion. It has a name, and the amount of hit points it grants when consumed.

Upgrade Data

I want my Potions to be upgradable, for some cost. To describe my upgrade, I created an Upgrade schema. It contains the key of the item you get after paying the cost, and the cost of the upgrade. You can make your upgrades more complex by adding multiple constraints like minimum XP, required level, etc. The Upgrade schema should hold the key of the item that will be obtained, and all the requirements to obtain that item.

Setting up the Upgrade Path

To set up my upgrade path, I’ve modified Potion to contain an Upgrade. That way, when potion data is loaded, the upgrade data is loaded as well. Remember the Upgrade schema only contains the key of the item obtained, rather than linking to the item itself. I set it up this way so that the upgrade won’t load the obtained item, since that is only needed when an item is upgraded. I don’t need to load the entire upgrade path for a particular item.

If I started with a level 1 potion, I can upgrade to level 2 potion with a short method:

GDEPotionData UpgradePotion(GDEPotionData potion)
{
  GDEPotionData upgradedPotion;
  
  GDEDataManager.DataDictionary.TryGetCustom(
    potion.key, out upgradedPotion);
  
  return upgradedPotion;
}

That’s the easy part. The value comes in processing the requirements on upgrade. If my Character had coins, and a Potion slot, I could write my upgrade logic like this:

void DoUpgrade(GDECharacterData character)
{
  // Check if I have enough coins to upgrade my potion
  if (character.coins >= character.potion.upgrade.cost)
  {
    // Subtract the coins used for the upgrade
    character.coins -= character.potion.upgrade.cost;

    // Do the upgrade using the simple method above
    character.potion = UpgradePotion(character.potion);
  }
}

When I set my potion slot to a new potion, that potion will now be the upgraded potion. The next time I try to upgrade, it will load the upgrade data for that new potion. The GDE data classes automatically save when assigning non-list types so my characters coin value and the potion slot will be saved until changed again.

Multiple Upgrade Requirements

If I wanted multiple requirements for an upgrade, I can add them to the Upgrade schema. You can define minimums, maximums, required completed missions, required location, anything that makes sense in your game. The Upgrade schema will hold what is necessary. A new method “CanUpgradePotion” will give those requirements context and provide the upgrade logic.

I’ve added a min_level field to the Upgrade schema to indicate the minimum level the Character needs to be to purchase the upgrade. To determine if I can upgrade my potion, I’ll define CanUpgradePotion() like this:

bool CanUpgradePotion(GDECharacter character)
{
  bool result = true;
  GDEUpgradeData upgrade = character.potion.upgrade;

  // Any requirements can be checked inside this method
  if (character.coins < upgrade.cost)
    result = false;
  else if (character.level < upgrade.min_level)
    result = false;

  return result;
}

Now I can update my DoUpgrade method to call the CanUpgradePotion method before performing the upgrade, like this:

void DoUpgrade(GDECharacterData character)
{
  // Check if I have enough coins to upgrade my potion
  if (CanUpgradePotion(character))
  {
    // Subtract the coins used for the upgrade
    character.coins -= character.potion.upgrade.cost;

    // Do the upgrade
    character.potion = UpgradePotion(character.potion);
  }
}

These examples illustrate one way to organize game data. Your game might require more Schemas and/or different structures. My goal is to get your creative juices flowing. Feel free to download this Data File and modify it to fit your needs. Or download and import the Google Spreadsheet.