# Need to store coordinates and associate them with another value

I have recently started fiddling around with unity and learning it, but am still somewhat a beginner, and haven’t even finished one original project yet (even if that is my unwillingness to just start with some simple clicker game or whatever XD)
_
I’ve been looking at data sets like arrays and dictionaries, but none seem to properly do what I need.

I can’t seem to figure out how to use unity grids to make an editable grid (in game builder, topdown), so I’m trying to code my own grid system.
_
I need to store a set of values like this: (X.VALUE, Y.VALUE, STRUCTURE)
X.VALUE + Y.VALUE = grid coordinates with building
STRUCTURE = the structure that needs to go there
_
Something similar to a Hashset, or Dictionary, so no duplicates like (2,3,shortgun) and (2,3,longun)?
_
Here’s what I’ve got:

_
and I want to store positions where it can snap to, like a grid
I need to be able to store positions of where the capsule is when a building is placed, and use them.
The square thing is at 0,0
_
I basically need to store values so I can check if something is there or what is there to prevent building buildings on buildings.
_
Or to check, persay, lets pretend if ObjA is adjacent to ObjB, ObjB gets a boost. I would need to check if any of the coordinates around ObjB are equivalent to ObjA, and if so, provide the boost.

Ok, as I understand it you want to make a custom grid system to store structures at specific grid coordinates (X, Y)? so that you can query them and see if and what ‘type’ of structure is at any given location? If this is the case you can use a Dictionary for it and then do all sorts of magic with that, let’s explore =)

Let’s start by making a custom key type for the Dictionary, you can do something like this, your milage may vary and this is untested code:

``````public struct GridCoordinate
{
public int X;
public int Y;

public GridCoordinate(int x, int y)
{
X = x;
Y = y;
}

public override bool Equals(object obj)
{
if (obj is GridCoordinate other)
{
return X == other.X && Y == other.Y;
}

return false;
}

public override int GetHashCode()
{
return X.GetHashCode() ^ Y.GetHashCode();
}
}
``````

We will probably need some sort of enumeration to detect the type of structure:

``````public enum StructureType
{
None,
ShortGun,
LongGun,
// etc.. whatever you need =-)
}
``````

Our Dictionary:

``````Dictionary<GridCoordinate, StructureType> grid = new Dictionary<GridCoordinate, StructureType>();
``````

So now we have the basic system set up, here’s how we can use it. To add a structure to your new grid system you can do something like this (again I haven’t really tested this code):

``````public void AddStructure(int x, int y, StructureType structureType)
{
GridCoordinate coordinate = new GridCoordinate(x, y);
if (!grid.ContainsKey(coordinate))
{
}
else
{
Debug.Log("A structure already exists at this location.");
}
}
``````

To check if a structure exists at a specific location, you can use:

``````public bool HasStructureAt(int x, int y)
{
GridCoordinate coordinate = new GridCoordinate(x, y);
return grid.ContainsKey(coordinate);
}
``````

To get the structure type at a specific location:

``````public StructureType GetStructureAt(int x, int y)
{
GridCoordinate coordinate = new GridCoordinate(x, y);
if (grid.TryGetValue(coordinate, out StructureType structureType))
{
return structureType;
}

return StructureType.None;
}
``````

Finally, to check if a specific structure is adjacent to another structure, you can use a method like this:

``````public bool IsStructureAdjacent(int x, int y, StructureType targetStructure)
{
{
new GridCoordinate(x - 1, y),
new GridCoordinate(x + 1, y),
new GridCoordinate(x, y - 1),
new GridCoordinate(x, y + 1)
};