- Home /
Smart Tile Info ?
Hi,
I'm making a tile-based game, and I have a lot of different types of tiles. Each tile should have a number, a name and a GUIContent (later maybe even more info). What I need is a fast and easy to use way to get info about tiles: when I have the number, I want to be able to get the name very easely, when I have the name I want to get the number easely, same for GUIContent and perhaps more info when added later. (all these values can't be changes at runtime, so all const/readonly).
I made a struct which contains all the info:
struct TileInfo { public TileInfo(int num, string name, GUIContent guiContent) { Number = num; Name = name; GuiContent = guiContent; }
readonly int Number;
readonly string Name;
readonly GUIContent GuiContent;
}
Now I was thinking of making an array of TileInfo's, and then writing some methods to extract the needed info (eg: int GetNumer(string name); or string GetName(int number);).
Though perhaps there's a better way ? Faster to code ? 'Easier' to use (without Get-methods) ? ...
Thanks ! -The Oddler
Answer by Statement · Mar 31, 2011 at 08:20 PM
You could make use of extension methods that work on any IEnumerable of TileInfo (Arrays, Lists, Queues...):
using System.Linq; using System.Collections.Generic; using UnityEngine;
public static class TileInfoExt { public static string NameOf(this IEnumerable<TileInfo> tiles, int number) { return tiles.Single(t => t.Number == number).Name; }
public static string NumberOf(this IEnumerable<TileInfo> tiles, string name)
{
return tiles.Single(t => t.Name == name).Number;
}
public static string GuiContentOf(this IEnumerable<TileInfo> tiles, int number)
{
return tiles.Single(t => t.Number == number).GuiContent;
}
public static string GuiContentOf(this IEnumerable<TileInfo> tiles, string name)
{
return tiles.Single(t => t.Name == name).GuiContent;
}
}
using System.Collections.Generic; using UnityEngine;
public class Example : MonoBehavior { TileInfo[] array = / imagine it being set. /; List<TileInfo> list = / imagine it being set. /;
void Usage()
{
string nameOf4 = array.NameOf(4);
GUIContent guiContentOfBrick = list.GuiContentOf("Brick");
}
}
Fastest for lookup would be using a combination of hashmaps and tables. Like a TileInfoCollection that has a HashSet, TileInfo[] and depending if you search by String or Number, access the appropriate container. Insertion and deletion would become slower since you need to maintain two containers but that isn't an issue if you aren't going to add or remove tiles while playing.
Hmm, I'm currently indeed maintaining multiple containers, but I hadn't heard of hashmaps before, so I'll look in tot that further :D Thanks in advance, looks promissing :D I'm going to take a look at that hopefully tomorrow :D Thanks !
Answer by DaveA · Mar 31, 2011 at 08:05 PM
Look into Hashtables. With that you can do things like
hashTab['tile1'] = new TileInfo();
var ti : TileInfo = hashTab[name];
and it's got iterators and so forth
I'll look in to this, I'll report back as soon as I can ! Thanks in advance :D