GridSquare

Updated on August 1, 2022

public void SaveGrid()

Save this GridSquares placed objects. These save files are found under the local OS storage space for Application.persistantDataPath.

//Get the grid
GridSquare myGrid;

void OnApplicationQuit() 
{
    //Save this grid when the application quits
    myGrid.SaveGrid();
}

public void LoadGrid()

Loads the grid it is called on. There has to be a save file stored with the same name, and same ID. You can load mid game, but will only load what was last saved. So if changes have been made since the last save, they will be lost.

//Get the grid
GridSquare myGrid;

void Start() 
{
    //Load the grid on start
    myGrid.LoadGrid();
}

public void DeleteGridSave()

Deletes this grids save file, careful when doing this, as by default there is only one save file per grid.

//Get the grid
GridSquare myGrid;
Button deleteBtn;

void Start() 
{
    deleteBtn.onClick.AddListener(() => { myGrid.DeleteGridSave(); });
}

public void LoadConfiguration()

Loads the grids saved Preconfiguration. Only either a load file, or a Preconfig file can be loaded at once. As a preconfig file cannot be written by the user, this is a perfect way to reset a level or scenario for example.

//Get the grid
GridSquare myGrid;
static int lives = 3;
bool runOnce = false;

//Checks update to see if lives have been lost. 
//If they are all gone, reset the level to its Preconfigured state  
void Update() 
{
    if(!runOnce && lives == 0)
    {
        myGrid.LoadPreconfiguration();
        runOnce = true;
    }
}
public void takeLife() 
{
    lives--;
}

public void CreateAutoCellBlock(GameObject prefab, int i)

This function is mostly used for the purposes of seeing the AutoCellBlock in the editor and the initial setup at runtime.

This function should not be called at any other time.

public Vector3 GetCheckBoxSize()

Returns the above checkbox size for the AutoCellBlock function inside the editor and at runtime.

This function should not be called at any other time.

public void CreateGrid()

Creates the initial points for the grid to be made up from. This is used both in the editor view and at runtime.

This function should not be called at any other time.

public void CreateGridCells()

Creates the Vector3 center of the cell check points and adds them to the cells array. This is used both in the editor view and at runtime.

This function should not be called at any other time.

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 bool CheckIfOnGrid(Vector3 cellPos)

Check if a position is initially available, also returning true if a placed object is underneath it.

This function is used only by the ObjectPlacer overwrite boolean but could also be used to just check which total cells are ‘buildable’ not including already placed objects.

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 void ChangeGridCellStatus(Vector3 cellPos, GameObject obj)

This function is used when creating the physical grid cell GameObjects. This is used both in the editor and at runtime.

This function should not be called at any other time.

public GameObject GetCellObject(Vector3 pos)

This function returns the actual physical grid cell created at position pos.

public GameObject GetGridObject(Vector3 pos)

Returns the GameObject placed on this cell at position pos.

public GameObject GetGridObjContainer()

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

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.