Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question making a matrix for tile types.

Discussion in 'Scripting' started by puddleglum, Apr 29, 2022.

  1. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    406
    i am making a city builder game which operates on a hexagonal tile map. i have scriptable objects so each tile can hold variables. each tile has 2 variables, its type. (forest, farm, town, house, mountain etc..) and its elevation. which is basically used for terrain generation to keep it from looking like a flat plane. when I click on a tile, I need to know its type and its elevation, (i already have code for this) and with that information, find the exact tilebase that it is, to do stuff like upgrade, highlight the tile when mouseover, etc.. . my current solution would be to take the elevation and type from the scriptable object, have 3 arrays each with 6 tile bases, and have a switch statment run through all 18 variations of tiles,

    and then go from there but i was wondering if there was a way to easily make a matrix for somthing like this, where i plug in the height and type, and it spits out the exact tilebase
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,646
    I'm kind of confused about your explanation... so each ScriptableObject is a Type and Elevation, and you want to be able to find the ScriptableObject, given a Type and Elevation? Shouldn't each tile just have a reference to the specified SO?
     
  3. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    406
    sorry for the confusing explanation, yes it does return the tilebase with the scribtable object info. however there are other things i want to do. for instance, highlight on mouse hover, i have a sprite that i made for each elevation that makes it highlighted, and i want to be able to find the right sprite to highlight based on a matrix, which includes the type and elevation, (type cause each tile has a different silhouette. and i want to make this scalable so i can easily add more elevations and types, so basically, i have 2 sets of objects, both sets contain elevation and type. lets say there was a highlight sprite with an elevation of 2 and a type of 6, if i hovered over a tile, it would return the elevation and type, run it through the matrix, and spit out the correct highlight sprite. idk if i can do this with just the tilebase, idk how i would find the matching sprite.
     
  4. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    442
    Why would elevation change the highlight sprite, other than its position?
     
  5. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    406
    i set up different sprites for different elevations because they look different,
     
  6. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    442
    Okay, well the datastructure you want is probably either:

    Dictionary<tiletype, Dictionary<int,Sprite>>


    or

    Dictionary<tiletype, List<Sprite>>


    depending on whether your elevations are all over the place or 0,1,2,3 etc

    However, the issue there is populating the datastructure, because Unity doesn't support serialised dictionaries.

    What I normally end up doing is something like this:

    Code (CSharp):
    1.         [System.Serializable]
    2.         public class SpriteList
    3.         {
    4.             public string name;
    5.             public List<Sprite> sprites;
    6.         }
    7.  
    8.         public List<SpriteList> spriteLists;
    9.  
    10.         Dictionary<string, List<Sprite>> spriteDict;
    11.  
    12.         private void Awake()
    13.         {
    14.             spriteDict = new Dictionary<string, List<Sprite>>();
    15.             foreach (SpriteList s in spriteLists)
    16.             {
    17.                 spriteDict[s.name] = s.sprites;
    18.             }
    19.         }
    20.  
    This will let you define sprites for each elevation for each named type (if you have an Enum of types, so much the better) that Unity will happily serialise, and then when the game starts up it puts it into the form of a dictionary so you can look up the sprite you want.
     
  7. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    406
    ooo, ok thankyou i will definitely do this, this zyntax is new to me so ima do some research :p thx