How to Inherit custom classes, or how to pass maze array from one class to another

Hi Guys,

I’m creating the maze game and I’m in situation where I need to add my own movement system.
So I have the maze.cpp file which creates maze by creating 2D array of cells.



	Cell **maze;
	maze = new Cell *[ROW];

	for (int i = 0; i < ROW; ++i)
	{
		maze* = new Cell[COLUMN];
	}


…so I can reach each cell by typing: maze*[j].
And I have default pawn blueprint, where I added new Actor Component, called custom_movement.cpp

My mission is to get that maze instance created in maze.cpp and use it in custom_movement.cpp file. Example: if(maze[0][1].clear) => move player forward and so on…
But when I try to inherit it, it gives me error:

Error	Implements: Class maze.cpp is not an interface; Can only inherit from non-UObjects or UInterface derived interfaces

So if my approach is right, can somebody tell me how to properly pass 2d array from Actor .cpp class to another Actor component .cpp class
If my approach is wrong, please advice me how should I do it?

Thank you

I would completely change the way you’re going about it to be honest.

  • Create a USTRUCT() struct which represents a maze ‘Cell’. That Cell can then have properties / functions / operator overload of it’s own. For example:


USTRUCT(BlueprintType)
struct MYGAME_API FMazeCell
{
	GENERATED_BODY()
public:
	UPROPERTY(BlueprintReadOnly, Category = "Cell")
	bool bIsClear;

	FMazeCell()
		: bIsClear(true)
	{}
};


  • Use a one-dimensional TArray instead of a 2D C++ Array. C++ Arrays aren’t supported by reflection and neither are nested TArrays, and you should be using Unreal’s Garbage Collector instead of new/delete operators. It’s also very easy to treat a 1D-array as a 2D-array, which makes lookups MUCH faster.


       UPROPERTY(BlueprintReadOnly, Category = "Maze")
       TArray<FMazeCell> Maze;

	FORCEINLINE static int32 Get2DIndex(const int32 Column, const int32 Row, const int32 NumRows) { return Row + Column * NumRows; }
	FORCEINLINE static void Get1DIndex(int32& OutColumn, int32& OutRow, const int32 NumRows, const int32 InIndex)
	{
		OutColumn = InIndex % NumRows;
		OutRow = InIndex / NumRows;
	}


  • You can then store all your ‘Mazes’ in one place as a TArray marked with UPROPERTY(), and pass it around to other classes by const reference to avoid copying it all over the place. E.g:


void UMyClass::MyFunction(const TArray<FMazeCell>& InMazeCells);