Unreal Engine 4 plug-in for square and hex grids and simple A*(Star) pathfinding. An instance of a grid is managed by an Actor that holds an array of the cells (points/nodes) on the grid. There are helper functions in both C++ and Blueprints for you to manipulate the grid, it's cells and your gameplay actors. Made and tested in Unreal Engine 4.27.
- Async grid generation
- Async A* pathfinding
- Non iterative cell validation
- World Location <-> Grid coordinates conversion
- GameplayTag based filtering for cell retrieval
- Hexagon version
- Common helper cell selection operations (think trace and overlap but for cells, sorta)
- WFC (Wave Function Collapse) base class
- Map importing
- Install plugin to your project's plugin folder and enable it by going to Edit > Plugins. e.g:
MyProject\Plugins\Dn2GameGrids
- Add a BoxGridActor to your level or create a blueprint with BoxGridActor as its parent class.
- In the details panel of your placed BoxGridActor or class defaults panel of your blueprint, under the Grid category, check bAutoGenerate, and set the extents or your grid.
- Alternately, you can skip step 3 and use the blueprint node
Update Grid Async
to build your base grid at runtime.Update Grid Async
overwrites the current grid if there is one. - Additionally
Update Grid Async
, on completion fires off Blueprint and C++ events with the resulting grid array, grid extents and cell size.
-
Cell: A point or node on the grid with associated 2D coordinates (e.g 4,20). See
Cell Info
. -
Cell Address or Address: the 2D coordinates (e.g 7,11) that represent a position on the grid.
-
Cell Info:
FCellInfo
Struct that holds the info for the cells on a grid.UpdateGridAsync()
populatesTArray<FCellInfo> GridArray
with these. You can findFCellAdress
,FGameplayTagContainer
, etc held within. -
Cell Tags:
FGameplayTagContainer
that holds theFGameTags
that are associated with aCell Info
. -
Grid Extents: The extents of the grid. A
FIntPoint
, X being how many cells wide and Y being how many cells 'tall'. -
Neighbors: Any cells directly next to a given cell. e.g.
GetCellNeighbors()
returns cells next to a given cell. -
Cell Size: The size of a cell when calculating its dimensions & location in world space in relation to the
AGridActorBase
that owns it.
Name | Members | Description |
---|---|---|
FCellAddress | int32 X in32 Y |
Coordinates for a cell, X and Y |
FCellInfo | FCellAddress Address FGameplayTagContainer CellTags |
Used for grid array. Holds info for each cell in the grid array |
FAStarCellInfo | float H float G float F FCellAddress Address FCellAddress CameFrom |
Used for the A-Star algo |
FAStarSearchResults | bool bFoundGoal TArray<FCellAddress> Path |
Struct holding the results of AStar search algorithm |
Name | Type | Description |
---|---|---|
bAutoGenerate | bool |
Show a grid preview in editor & generate on BeginPlay |
CellSize | float |
Size of cells |
DefaultCellTags | FGameplayTagContainer |
If bAutoGenerate , generate a grid on BeginPlay using these Tags |
GridArray | TArray<FCellInfo> |
Array holding our grid cells |
GridMeshComp | UStaticMeshComponent* |
Visual of the grid and trace collisions for world locations <-> grid address conversions |
GridMat | UMaterial* |
The material we'll make and instance of and use for visual debugging |
Name | CPP | BP | Description |
---|---|---|---|
UpdateGridAsync | Yes | Yes | Create default grid for this grid actor. Populates TArray<FCellInfo> GridArray . On completion calls C++ event ABoxGridActor::OnUpdateGrid_Internal and blueprint event ABoxGridActor::OnUpdateGrid with the results of the grid creation. |
GetPathToGoalAsync | Yes | Yes | Create default grid for this grid actor. Populates TArray<FCellInfo> GridArray . On completion calls C++ event ABoxGridActor::OnUpdateGrid_Internal and blueprint event ABoxGridActor::OnUpdateGrid with the results of the grid creation |
CreateEmptyGrid | Yes | Yes | Create a grid array that you can do what you want with. Not Async. No sort of setups of anything. Just returns an array of FCellInfo |
AStarSearchToGoal | Yes | Yes | Not Async. Try to find a path to the goal given addresses and optional filters, using an AStar pathfinding algorithm |
GetCellLocationFromAddress | Yes | Yes | Given the address of a cell (e.g 12,24) return the cell's world location. Uses the Actor's GridArray |
GetCellAddressFromLocation | Yes | Yes | Given a world location (e.g FVector(123,456,10) ) return a cell address IF the address is valid. Return value of FCellAddress(-1,-1) means there is no cell associated with given location. Uses the Actor's GridArray |
GetIndexFromAddress | Yes | Yes | Given the address of a cell, return the Index for that cell's FCellInfo in AGridActorBase::GridArray . Avoids iterating arrays by just calculation its index |
DoesCellExist | Yes | Yes | Returns true if the given address is valid and the FCellInfo in the assumed index of AGridActorBase::GridArray matches |
GetCellInfoByAddress | Yes | Yes | Given and address returns the associated FCellInfo from AGridActorBase::GridArray . Make sure the returned FCellInfo::Address is valid. i.e. not FCellAddress(-1,-1) |
GetCellNeighbors | Yes | Yes | Given an address, an array of neighbouring cells are returned. Optionally filtered using GameplayTags. ToDo: maybe return addresses instead of cell infos |
GetGridExtents | Yes | Yes | Returns the extents of the grid. |