Hello everyone,
I’m new to both C++ and the Unreal Engine, but I decided to give learning it a shot.
I’ve encountered the error described when trying to modify the puzzle sample and return an array of blocks.
Z3BlockGrid.cpp
#include "Z3.h"
#include "Z3BlockGrid.h"
#include "Z3Block.h"
#include "Components/TextRenderComponent.h"
AZ3BlockGrid::AZ3BlockGrid(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
// Create dummy root scene component
DummyRoot = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("Dummy0"));
RootComponent = DummyRoot;
// Create static mesh component
ScoreText = ObjectInitializer.CreateDefaultSubobject<UTextRenderComponent>(this, TEXT("ScoreText0"));
ScoreText->SetRelativeLocation(FVector(200.f, 0.f, 0.f));
ScoreText->SetRelativeRotation(FRotator(90.f, 0.f, 0.f));
ScoreText->SetText(TEXT("Score: 0"));
ScoreText->AttachTo(DummyRoot);
// Set defaults
Size = 6;
BlockSpacing = 250.f;
}
void AZ3BlockGrid::BeginPlay()
{
Super::BeginPlay();
// Number of blocks
const int32 NumBlocks = Size * Size;
//int** ary = new int*[xMax];
//for (int i = 0; i < xMax; ++i)
//ary[i] = new int[yMax];
// 2D array for mapping each block to coordinates
//int xMax = ((NumBlocks - 1) / Size);
//int yMax = ((NumBlocks - 1) % Size);
//AZ3Block tilemap [5];
// second method:
//std::vector<Car> mycars; // empty
//mycars.reserve(userInput); // optional: reserve the memory upfront
//TArray<AZ3Block*> tilemap;
Tilemap.Reserve(NumBlocks);
// Loop to spawn each block
for (int32 BlockIndex = 0; BlockIndex < NumBlocks; BlockIndex++)
{
int xCoord = (BlockIndex / Size);
int yCoord = (BlockIndex % Size);
const float XOffset = (BlockIndex / Size) * BlockSpacing; // Divide by dimension
const float YOffset = (BlockIndex % Size) * BlockSpacing; // Modulo gives remainder
// Make postion vector, offset from Grid location
const FVector BlockLocation = FVector(XOffset, YOffset, 0.f) + GetActorLocation();
// Spawn a block
AZ3Block* NewBlock = GetWorld()->SpawnActor<AZ3Block>(BlockLocation, FRotator(0, 0, 0));
NewBlock->xPos = xCoord;
NewBlock->yPos = yCoord;
// Tell the block about its owner
if (NewBlock != NULL)
{
NewBlock->OwningGrid = this;
}
// Add the spawned blocks to the array
Tilemap.Insert(NewBlock, xCoord * yCoord + yCoord);
}
}
void AZ3BlockGrid::AddScore()
{
// Increment score
Score++;
// Update text
FString ScoreString = FString::Printf(TEXT("Score: %d"), Score);
ScoreText->SetText(ScoreString);
}
TArray<AZ3Block*> AZ3BlockGrid::GetTiles()
{
return Tilemap;
}
Sorry for that wall of code. The issue is with that very last function. It keeps giving me an error that "declaration is incompatible with TArray