TGridArray
Overview
TGridArray is a generic template class designed to manage a 2D array of elements. It provides methods for reserving space, converting between coordinates and indices, and accessing elements based on their coordinates. This class is useful for scenarios where data needs to be stored in a grid-like structure, such as tile-based games or spatial partitioning.
Properties
Type | Name | Access | Description |
|---|---|---|---|
| Elements | Private | Pointer to the array of elements. |
| Size | Private | The dimensions (width and height) of the grid. |
Functions
Return Type | Signature | Description |
|---|---|---|
| Reserve(FIntVector2 Size) | Reserves space for a 2D array with the specified size. |
| CoordinateToIndex(const FIntVector2 Coordinate) const | Converts a coordinate to its corresponding index in the array. |
| IndexToCoordinate(const int32 Index) const | Converts an index back to its corresponding coordinate in the array. |
Usage & Implementation Notes
The class uses dynamic memory allocation to manage the 2D array, which should be handled carefully to avoid memory leaks.
The
Reservefunction checks if the current elements pointer is not null before allocating new memory, ensuring that existing data is not lost.The
CoordinateToIndexandIndexToCoordinatefunctions include assertions to ensure that the provided coordinates or indices are within valid ranges.