Search Unity

Creating Game Data

Discussion in 'Scripting' started by squigglyo, Jul 11, 2017.

  1. squigglyo

    squigglyo

    Joined:
    May 21, 2012
    Posts:
    107
    Hey guys

    So my game has 3 layers to the Game Levels.

    Islands - Kingdoms - Towns

    Each Island has x number of Kingdoms to it and each Kingdom has y number of Towns in it.
    To start with, Ive got
    2 Islands
    7 Kingdoms in total
    350 Levels in total


    I have an Island Class that holds the total points of the kingdoms and a Dictionary of Kingdoms.
    Each Kingdom holds the total points of the Towns and a collection of Towns
    Each Town has the points obtained in it.

    The Towns hold in various data such as stars obtained, level setup info, score (population), etc.

    Code (CSharp):
    1.  
    2. _IslandInfo = new Dictionary<ISLAND_NAMES, IslandInfo> ();
    3.    
    4.         #region Island 1
    5.         //ISLAND DATABASE
    6.         _IslandInfo.Add(ISLAND_NAMES.Island1, new IslandInfo () {
    7.             name = ISLAND_NAMES.Island1,
    8.             //Add a Kingdom to Island 1
    9.             kingdomInfo = new Dictionary<KINGDOM_NAMES, KingdomInfo> () {
    10.                 {KINGDOM_NAMES.Island1_Kingdom1, new KingdomInfo () {
    11.                         name = KINGDOM_NAMES.Island1_Kingdom1,
    12.                         //Add levels to the Kingdom
    13.                         levelInfo = new LevelInfo[] {
    14.                             new LevelInfo () {
    15.                                 basePopulation = 100,
    16.                                 starValues = new int[]{ 1000, 5000, 10000 },
    17.                                 levelMakeup = LEVEL_MAKEUP.GRID_4x4
    18.                             },
    19.                             new LevelInfo () {
    20.                                 basePopulation = 1000,
    21.                                 starValues = new int[]{ 2500, 7000, 12000 },
    22.                                 levelMakeup = LEVEL_MAKEUP.GRID_5x5
    23.                             },
    24.                             new LevelInfo () {
    25.                                 basePopulation = 5000,
    26.                                 starValues = new int[]{ 4000, 6500, 10000 },
    27.                                 levelMakeup = LEVEL_MAKEUP.GRID_6x6
    28.                             }
    29.                         },
    30.                     }
    31.                 },
    32.                 //Add another Kingdom
    33.                 {KINGDOM_NAMES.Island1_Kingdom2, new KingdomInfo () {
    34.                         name = KINGDOM_NAMES.Island1_Kingdom2,
    35.                         levelInfo = new LevelInfo[] {
    36.                             new LevelInfo () {
    37.                                 basePopulation = 100,
    38.                                 starValues = new int[]{ 1000, 5000, 10000 },
    39.                                 levelMakeup = LEVEL_MAKEUP.GRID_4x4
    40.                             },
    41.                             new LevelInfo () {
    42.                                 basePopulation = 10000,
    43.                                 starValues = new int[]{ 10000, 50000, 100000 },
    44.                                 levelMakeup = LEVEL_MAKEUP.GRID_4x4
    45.                             }
    46.                         }
    47.                     }
    48.                 },
    49.                 {KINGDOM_NAMES.Island1_Kingdom3, new KingdomInfo () {
    50.                         name = KINGDOM_NAMES.Island1_Kingdom3,
    51.                         levelInfo = new LevelInfo[] {
    52.                             new LevelInfo () {
    53.                                 basePopulation = 100,
    54.                                 starValues = new int[]{ 1000, 5000, 10000 },
    55.                                 levelMakeup = LEVEL_MAKEUP.GRID_4x4
    56.                             },
    57.                             new LevelInfo () {
    58.                                 basePopulation = 10500,
    59.                                 starValues = new int[]{ 100000, 500000, 1000000 },
    60.                                 levelMakeup = LEVEL_MAKEUP.GRID_4x4
    61.                             },
    62.                         }
    63.                     }
    64.                 }
    65.             }
    66.         });
    67.         #endregion
    68.  
    This code creates the first Island in 1 line.
    This island has 3 kingdoms which have 3, 2 and 2 Towns in them.
    To figure out total Score (Population) I iterate through the Island's Kingdoms and through the Kingdom's Towns.


    All of this works fine, but im not sure how well this kind of system will work when it gets bigger.

    When I add the second Island, my total Towns will be up to about 50.
    The third island will bring that up to 150, etc.


    Is there any problems that anyone can forsee ill run in to creating a system like this?
    There is no 'thinking' that goes on, no level creation or anything, this is just allocating heaps of data into organised lists/dictionarys. When the levels are loaded they will be created based on this info.
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    It's a good start, organized into a nice hierarchical structure. But in the long run you'll find it much easier to maintain your data separately from your code. For example, you could put it in a JSON file and convert it to this data structure using JsonUtility.FromJson(). Also, if non-programming level designers join your project, they'll be able to edit data without having to touch any code.
     
  3. squigglyo

    squigglyo

    Joined:
    May 21, 2012
    Posts:
    107
    Thanks TonyLi

    Before I read this I had already found, learnt and moved over to JSON.

    But you are right, this is definitely the right direction.