How to increase the size of an existing grid

So I am still attempting to increase the capacity of the grid. I have successfully converted the gridObjectArray into a gridObjectDictionary. However I believe I am fundamentally misunderstanding some of the code.

I have code that allows me to click on a region. The region will be as large as the starting levelgrid settings. Clicking in each region will return what region it is ~ (0,0) or (0,1) or (-1,0) and so on.

I want to rework the existing code to create new gridobjects in that region and add them to the existing dictionary. However my ADHD brain is getting confused with regards to the creation of new gridpositions, along with TGridObject.

Is there a proper easy solution or should I murder this code and completely separate gridpositions from the concept of levelgrid?

Hope my question is clear.

This, I’m not entirely clear on…

I don’t think you want to separate that logic at all… a LevelGrid and GridPositions are sort of joined at the hip.

Like you, my GridSystem houses a Dictionary<GridPosition, TGridObject> rather than a TGridObject[,]. This allows for unwalkable areas to not even be in the grid system to begin with, to save space.

I think my approach would be to instantiate the actual floor plan of the new region, with each tile in the region “registering” itself with the LevelGrid and PathFinder. The tile should be able to get it’s GridPosition from LevelGrid (that’s just a math function), and then have some sort of method on LevelGrid like

public void AddGridObject(GridPosition position)
{
    gridSystem.AddGridObject(new GridObject(gridSystem, position), position);
}

And in GridSystem,

public void AddGridObject(TGridObject newGridObject, GridPosition position)
{
    gridObjectDict[position] = newGridObject;
}

You would need to do the same thing with the Pathfinder or you won’t be able to make a path to the new locations.

1 Like

So I ended up reading the code again and again last night until my brain comprehended what the create methods were doing. I managed to get it partially functional last night and I believe now “fully” functional this morning.

I believe have successfully refactored every bit of grid related code into being properly scalable in this way. And by keeping track of that information in Regiongrid, when I load I can recreate every “discovered” region.

RegionGrid function is called

public void CreateNewSector()
    {
        Vector3 worldPosition = MouseWorld.GetPosition();
        float gridOffSet = regionCellSize / 2;

        Vector2 gridLocation = new Vector2(Mathf.RoundToInt((worldPosition.x - gridOffSet) / regionCellSize),
            Mathf.RoundToInt((worldPosition.y - gridOffSet) / regionCellSize));
            
        Vector2 gridPosition = new Vector2(regionCellSize * gridLocation.x, regionCellSize * gridLocation.y);

        if(!RegionList.Contains(gridLocation))
        {
            RegionList.Add(gridLocation);
            LevelGrid.Instance.IncreaseGridSize(gridLocation);
            Pathfinding.Instance.SetupNewRegion(gridLocation);
            GridSystemVisual.Instance.AddNewGridVisualObjects();
        }
        else
        {
            Debug.LogError("This Region Already Exists!");
        }
    }

Then Levelgrid Creates positions

public void IncreaseGridSize(Vector2 region)
    {
        gridSystem.IncreaseGridSize(
            (GridSystem<GridObject> g, GridPosition gridPosition) => new GridObject(g, gridPosition), 
            new Vector2 (region.x * width, region.y * height));
        gridSystem.CreateNewDebugObjects(gridDebugObjectPrefab, this.transform);
    }

Pathfinding creates its portion

public void SetupNewRegion(Vector2 region)
    {
        gridSystem.IncreaseGridSize(
            (GridSystem<PathNode> g, GridPosition gridPosition) => new PathNode(gridPosition), 
            new Vector2 (region.x * width, region.y * height));

        foreach(KeyValuePair<GridPosition, PathNode> pair in gridSystem.GetDictionary())
        {
            Vector3 worldPosition = LevelGrid.Instance.GetWorldPosition(pair.Key);
            float raycastOffsetDistance = 5f;
                if(Physics.Raycast(
                    worldPosition + Vector3.down * raycastOffsetDistance, 
                    Vector3.up, 
                    raycastOffsetDistance * 2, 
                    obstaclesLayermask))
                {
                    GetNode(pair.Key.x, pair.Key.y).SetIsWalkable(false);
                }
        }
    }

And then finally visibility for the unit movement

public void AddNewGridVisualObjects()
    {
        foreach(KeyValuePair<GridPosition, GridObject> pair in LevelGrid.Instance.GetDictionary())
        {
            if(gridSystemVisualDictionary.ContainsKey(pair.Key)) continue;

            Transform gridSystemVisualSingleTransform = 
                    Instantiate(gridSystemVisualSinglePrefab, LevelGrid.Instance.GetWorldPosition(pair.Key), Quaternion.identity, this.transform);
            gridSystemVisualDictionary.Add(pair.Key,gridSystemVisualSingleTransform.GetComponent<GridSystemVisualSingle>());
        }

        UpdateGridVisual();
    }

I also refactored “validpositions” to correctly use the dictionary (ontop of just about every other array based code).

If you see anything unnecessary I forgot in these sections let me know. But I can happily say it’s working exactly as I desire at present!

1 Like

That’s quite a refactor, well done! This is the type of step that I believe takes a student from following the course to understanding the content and taking it further.

I took programming in college so It’s not so much that I don’t know how to code or even fully understand it as it is that after I graduated my ADHD factor increased greatly, making focusing on tasks like this hard at times, especially when I read code and don’t immediately or quickly understand what it does. I often also find it hard if not weird to ask for help because many times even after I ask for help I find the solution I was looking for one way or another.

I’ll try to make sure to always ask when I get stuck tho, as it does often result in getting unstuck by just asking for the help to begin with, whether I get an answer I was looking for or not.

You have no idea how many times just by asking/articulating the question, the solution presents itself right after I ask the question.

2 Likes

This has happened to me more times then i can count,

Started asking for help, getting all the relevant information and half way through realized"oh. shoot, its in that screenshot"

Or send an email for a ticket, hit send, and realize the issue in that moment.

Something about asking a(what turns out to be) “simple/silly” question out loud really gets your brain thinking

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms