glue-how-to-cross-reference-csvs

Introduction

CSVs are often used to define data for games, but it's common for one CSV to need to reference information from another CSV. This tutorial will walk you through creating two CSVs, one which will reference another.

Our situation

In this situation we will create two CSVs:

  1. The first will define the weapons which exist in your game. The weapons defined for this game will be for a traditional RPG.

  2. The second will define the shops in the game which will contain a list of the weapons which they can sell.

Creating the CSVs

First we'll create a CSV for weapons, which we'll call WeaponInfo:

  1. Right-click on GlobalContent

  2. Select "Add File"->"New File"

  3. Select "Spreadsheet (.csv)" as the type

  4. Enter the name "WeaponInfo" as the new file's name

  5. Click OK

Next create the CSV for StoreInfo:

  1. Right-click on GlobalContent

  2. Select "Add File"->"New File"

  3. Select "Spreadsheet (.csv)" as the type

  4. Enter the name "StoreInfo" as the new file's name

  5. Click OK

Filling the WeaponInfo CSV

Fill the WeaponInfo so it appears as follows:

Name (string, required)

Damage (float)

Sword

3

Spear

4

Dagger

2

Filling the StoreInfo CSV

Fill the StoreInfo so it appears as follows:

Name (string, required)

WeaponTypes (List<string>)

CapitalCity

Sword

Spear

SouthCity

Dagger

Spear

**Note we used List<string>:**Note that the StoreInfo CSV file uses a List<string> instead of a List<WeaponInfo> for the WeaponTypes it contains. The reason for this is because we want to simply reference the key from WeaponInfo.csv so that it can change without requiring changes to StoreInfo.csv.

Adding a StoreInfo partial class

Next we'll need to write some simple code to associate the values in the WeaponTypes column in the StoreInfo CSV to the weapons defined in WeaponInfo.csv. To do this:

  • Open your project in Visual Studio

  • Expand the "DataTypes" folder in the Solution Explorer

  • Right-click on the "DataTypes" folder and select "Add"->"Class..."

  • Name the class "StoreInfo". This should be the same name as your CSV.

    public partial class StoreInfo
    {
    }

Finally save your project through the "File"->"Save All" option in Visual Studio. This will save changes to the project so that if Glue is open it will reload the changes. Visual Studio may notify you that the project has changed. This may be because Glue has inserted the StoreInfo.Generated.cs file under the StoreInfo.cs in the Solution Explorer.

Adding the "Weapons" property to StoreInfo

Next we'll add a property to the StoreInfo.cs file to make it more convenient to access which WeaponInfo's a Store contains. To do this, add the following code to StoreInfo.cs:

public IEnumerable<WeaponInfo> Weapons
{
    get
    {
        foreach (var weaponType in this.WeaponTypes)
        {
            yield return GlobalContent.WeaponInfo[weaponType];
        }
    }
}

Now each ScreenInfo which is loaded into GlobalContent has an IEnumerable of WeaponInfo's that can be used to populate UI, menus, and drive game logic.

Last updated