GridSquare

Updated on March 17, 2022

public bool CheckCellStatus(Vector3 cellPos)

This function returns true if the given key (cellPos) is found in the dictionary and that position’s value is null. It will return false if that keys value is any object except for null. If the value is null, it means the cell is marked as empty, if it is anything else, the cell is still marked as full.

Note the keys entered into the dictionary are the cell position and not the point position. For example, if placing an object in a newly created grid with a cell size of 1, at its first cell, the cell position will be Vector3(0.5f, 0, 0.5f). This is because objects are placed in the centre of each cell.

//Checks the first cell if cell size is 1. 
GridSquare myGrid;

void Start() 
{
    if(myGrid.CheckCellStatus(new Vector3(0.5f, 0, 0.5f)))
    {
        Debug.Log("This cell is empty");
    }
    else 
    {
        Debug.Log("This cell is full");
    }
}

public void ChangeCellStatus(Vector3 cellPos, GameObject obj)

This will assign a value(obj) to the key(cellPos) provided. Thus marking that cell as full if the object is not null. Send null to ’empty’ the cell.

//Checks the first cell if cell size is 1. 
GridSquare myGrid;

void Start()
{
    if(myGrid.CheckCellStatus(new Vector3(0.5f, 0, 0.5f)))
    {
        Debug.Log("This cell is empty");
        //The cell is empty, so you may now want to fill it.
        myGrid.ChangeCellStatus(new Vector3(0.5f, 0, 0.5f), myObject);
    }
    else 
    {
        Debug.Log("This cell is full");
        //This cell is full, so you may now want to empty it.
        //Send null in the arguements to 'empty' the cell. 
        myGrid.ChangeCellStatus(new Vector3(0.5f, 0, 0.5f), null);
    }
}

public GameObject GetGridObjContainer()

Will return this grids container of objects that have been placed. The container is created at runtime.

//Hides the game objects placed on the grid at start.
GridSquare myGrid;

void Start() 
{
    myGrid.GetGridObjContainer.SetActive(false);
}

public float GetCellSize()

Returns this grids cell size.

//Debugs the grids cell size.
GridSquare myGrid;

void Start() 
{
    Debug.Log(myGrid.GetCellSize());
}

public float GetGridHeight()

Returns this grids grid height.

//Debugs the grids height.
GridSquare myGrid;

void Start() 
{
    Debug.Log(myGrid.GetGridHeight());
}

public float GetGridWidth()

Returns this grids grid Width.

//Debugs the grids width.
GridSquare myGrid;

void Start() 
{
    Debug.Log(myGrid.GetGridWidth());
}

public Dictionary<Vector3, GameObject> GetCellsDictionary()

Returns this grids Dictionary. There is a debug.log for each entry in the dictionary. This is sometimes useful for debugging purposes to actually see what each cell contains and if it matches the physical grid.

//Debugs the grids Dictionary of cells.
//Each cell will have an entry of either the object placed, or null.
GridSquare myGrid;

void Start() 
{
    myGrid.GetCellsDictionary();
}

Still need help? Contact support at support@golemitegames.com

Please describe your issue in detail and if possible, provide screenshots to help us understand your problem better and answer your question more effectively.