Using LINQ

This section of the document describes how it can be easily done with LINQ(Language Integrated Query) to query specific data set whcih is exported from complex spreadsheet.

Highly recommended to see 101-LINQ Samples if you not familiar with LINQ yet. Also there are plenty of documents for LINQ so just do googling for that.

Ok, let's start.

As you already know spreadsheet like Excel is one of the most powerful tool what game designers daily use to make various tables and data set used within game.

The following image shows a table data for all weapon items in a game. Note that the shown columns are not all of columns in the table, there can be much more for usual game especially if the game is RPG genre.

No matter how complicated the table of the given spreadsheet is, it is easy to get the table into Unity editor via Unity-Quicksheet. So assume that importing the table into Unity was successfully done.

Now we have automatically generated WeaponTable.cs script file which shows the class as the following:

public class WeaponData
{
     public int ID { get; set; }    
     public string Name { get; set; }
     public int STR { get; set;}
     ...
}

And there is also a ScriptableObject derived class which is for .asset file.

public class WeaponTable : ScritableObject
{
    ...
    public WeaponData[] dataArray;    
    ...
}

To make it easy to manage items, all items in the spreadsheet has its unique ID(see the first column) which is used to indentify what the item type is with its range.

Type Range
Weapon 1000 ~ 1999
Armor 2000 ~ 2999
Consumable 3000 ~ 3999

The range can be various depends on its number of item type or each number of item itself.

So just with having integer array of the item ID is enough to describe what items are in the inventory.

public class Inventory
{
    ...
    // currently belonging items in the inventory
    int[] items = {
        1000, 1001, 1002, 1003, 2002, 2003,
        2004, 2005, 2006, 2007, 2008, 2009,
        2010, 2011, 2012, 2013, 2014, 2015,
        2016, 2017, 2018, 4014, 4015, 4016,
        4017, 4018, 4019, 4020, 4021, 4022,
        4023, 4024, 4025, 4026, 4027, 4028, 4029,
        4030, 4031, 4032, 4033, 4034, 4035, 4036
    };
    ...
}

And then we need to retrieve actual item data we have in the inventory which can be done to query to the item table.

ItemManager is a class which provides methods to handle and access various items in the inventory. Note weapons member field as an intance which is automatically created from Unity-Quicksheet.

public class ItemManager : Singleton<ItemManager>
{
    public WeaponTable weaponTable;
    ...
    // weapon IDN: 1000~1999
    public List<weaponData> GetWeaponList(int[] ids)
    {
        List<weaponData> items = new List<weaponData>();

        var weaponIDs = ids.Where(x => x >= 1000 && x < 2000).ToList();
        var weaponResult = from w in weaponTable.dataArray // all weapon type items from spreadsheet.
                         from n in weaponIDs             // weapons in the inventory.
                         where w.ID == n
                         select w;
        return weaponResult.ToList();
    }
}

As seen from the above code, retrieving items of weapon type can be easily done with using LINQ as seens as GetWeaponList method.

Consider our tables in a spreadsheet as a kind of database. Then extracting and importing data into Unity with Unity-Quicksheet at edit-time and prefer to use LINQ to query data from the table at runtime. No matter how complicated the table is, it is a piece of cake.

results matching ""

    No results matching ""