Having a hard time absorbing the grid theory

Hi,

I’ve been trying to make notes as I go on this course in case I forget what certain bits of code does after not touching it for a while and I’ve been having trouble keeping up with the last few grid lectures.

I’m hoping someone can break down the last few bits of the GridSystem script as that’s where I find it really hard to follow and summarise as we have so many words and variables with very similar names.

    public Vector3 GetWorldPosition(GridPosition gridPosition) //Searches for and returns the X,Y,Z coordinates of the centre of the cell, based on cell size
    {
        return new Vector3(gridPosition.x, 0, gridPosition.z) * cellSize; 
    }

    public GridPosition GetGridPosition(Vector3 worldPosition) //Translates the world position to grid coordinates
    {
        return new GridPosition(

            Mathf.RoundToInt(worldPosition.x / cellSize), // Vector3 defaults to floats, whereas we need ints for our grid.
            Mathf.RoundToInt(worldPosition.z / cellSize)
        );
    }

    public void CreateDebugObjects(Transform debugPrefab) // Creates the GridDebugObject prefab across each calculated grid cell
    {
        for (int x = 0; x < width; x++)
        {

            for (int z = 0; z < height; z++)
            {
                GridPosition gridPosition = new GridPosition(x, z); // Uses the looped values to create cell 'gridPosition's

                Transform debugTransform = GameObject.Instantiate(debugPrefab, GetWorldPosition(gridPosition), Quaternion.identity); // Spawns the GridDebugObject prefabs in the looped value coordinates
                GridDebugObject gridDebugObject = debugTransform.GetComponent<GridDebugObject>(); // Accesses the GridDebugObject script and stores it in 'gridDebugObject'
                gridDebugObject.SetGridObject(GetGridObject(gridPosition)); // ???
            }

        }
    }

    public GridObject GetGridObject(GridPosition gridPosition) // ???
    {
        return gridObjectArray[gridPosition.x, gridPosition.z];
    }

So in essence, I need a bit more clarification on the ‘???’ bits. I’m also hoping my existing notes are correct…

Everything looks fine except that GetWorldPosition doesn’t search for anything. It just converts the GridPosition into world coordinates.

As for the ???;
Each grid position holds a grid object. For the course we had two grids holding two different types of objects; one with units currently on it, and one for pathfinding with the pathfinding data. GetGridObject() returns the object for the specified position. The GridDebugObject had a reference to this objects so that it can display the relevant info. At the first ??? we get the object at the grid position and pass it to the GridDebugObject. The second ??? is where we get the actual object

Hope this helps

1 Like

Yeah that seems to have helped thanks! I think I now understand the meaning behind the code even if looking at the formatting seems to throw me every time. The last course’s grid I recreated seemed like a cakewalk compared to this one somehow

I’ve added the following notes:

???(1) is now ‘gridPosition(looped x,z coords) is used to find the object in that position, the object is then used to pass the values to GridDebugObject which writes the label before moving on’

???(2) is just ‘Returns the object for the specified x,z position’

Looks good

2 Likes

We want our GridDebugObject to be able to access the specific GridObject in the GridSystem. While the GridDebugObject could ask the GridSystem for the GridObject any time it needs it based on it’s position, it makes more sense to set the GridObject directly with SetGridObject. This is a type of dependency injection.

Our GridSystem stores the GridObjects in a 2 dimensional array. This method simply retrieves that GridObject based on the position.

If you’ve ever played the game Battleship, you’ll recall that we have a row (a letter in Battleship) and a column (a number in Battleship) coordinate for launching the torpedoes. This is very similar to what we’re doing here. A GridPosition contains an X and Z coordinate. We’re reaching into the array, which is really just a grid like in Battleship, and finding out what’s in that Array at [x,z].

1 Like

Thanks Brian, I think I’m getting there. I’ve re-watched all of the grid videos today (not the Level Grid course yet, I need a mental breather!) and now have a very large OneNote Notebook with the entire system so far in my own words. Hopefully it’ll act as a bit of a cheat sheet going forward!

Feeling a bit more confident on it now, I’m hoping it’ll sink in the more we use it going forward

2 Likes

Privacy & Terms