Hello,
I’m trying to create a data structure where actors contain others actors by referencing them in containers as TMap or TArray. To explain a bit, I created a AWall class, a ALabyrinthCell class which reference AWall objects in a TMap and
ALabyrinthBody which have a two dimensionnal TArray referencing ALaryrinthCell objects.
I have no problems to spawn these actors as i want to, but once I try to access let’s say ALabyrinthCell container through the ALabyrinthBody container, all the data is gone. Anyone has an idea of what i am doing wrong ?
Thanks!
ALabyrinthCell.h
#pragma once
#include "Wall.h"
#include "GameFramework/Actor.h"
#include "LabyrinthCell.generated.h"
UCLASS()
class LABYRINTH_API ALabyrinthCell : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ALabyrinthCell();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
void formCell();
void abattreMur(FString cote);
TMap<FString, class AWall*> getCell() const { return cell; }
protected:
UPROPERTY()
TMap<FString, class AWall*> cell;
};
ALabyrinthCell.cpp
function which fills the container
void ALabyrinthCell::formCell()
{
if (GetWorld() != nullptr)
{
FVector *v = new FVector(GetTransform().GetLocation().X, GetTransform().GetLocation().Y, GetTransform().GetLocation().Z);
FRotator *r = new FRotator(0.f, 90.f, 0.f);
cell.FindOrAdd("north") = (AWall*) GetWorld()->SpawnActor(AWall::StaticClass(),v,r);
cell.FindOrAdd("east") = (AWall*)GetWorld()->SpawnActor(AWall::StaticClass(), v);
cell.FindOrAdd("west") = (AWall*)GetWorld()->SpawnActor(AWall::StaticClass(), v);
cell.FindOrAdd("south") = (AWall*)GetWorld()->SpawnActor(AWall::StaticClass(), v,r);
FVector Orgin;
FVector size;
cell["north"]->GetActorBounds(false, Orgin, size);
v->Set(GetActorLocation().X, GetActorLocation().Y - size.X, GetActorLocation().Z);
cell["north"]->SetActorLocation(*v);
v->Set(GetActorLocation().X, GetActorLocation().Y + size.X, GetActorLocation().Z);
cell["south"]->SetActorLocation(*v);
v->Set(GetActorLocation().X + size.X, GetActorLocation().Y, GetActorLocation().Z);
cell["east"]->SetActorLocation(*v);
v->Set(GetActorLocation().X - size.X, GetActorLocation().Y, GetActorLocation().Z);
cell["west"]->SetActorLocation(*v);
delete(v);
delete(r);
}
}
ALabyrinthBody container is a TArray >
ALabyrinthBody function which fills it
void ALabyrinthBody::formGrid(int x, int y)
{
AWall* w = GetWorld()->SpawnActor<AWall>();
FVector Orgin;
FVector size;
w->GetActorBounds(false, Orgin, size);
w->Destroy();
FVector *position;
FVector *origin;
//Test si on a un nombre pair ou non de ligne et de colonne dans la grille
if (y % 2 == 0) {
if (x % 2 == 0)
position = new FVector((GetTransform().GetLocation().X - size.Y) - (x*size.Y), (GetTransform().GetLocation().Y - size.Y) - (y*size.Y), GetTransform().GetLocation().Z);
else
position = new FVector(GetTransform().GetLocation().X - (x*size.Y), (GetTransform().GetLocation().Y - size.Y) - (y*size.Y), GetTransform().GetLocation().Z);
}
else {
if (x % 2 == 0)
position = new FVector((GetTransform().GetLocation().X - size.Y) - (x*size.Y), GetTransform().GetLocation().Y - (y*size.Y), GetTransform().GetLocation().Z);
else
position = new FVector(GetTransform().GetLocation().X - (x*size.Y), GetTransform().GetLocation().Y - (y*size.Y), GetTransform().GetLocation().Z);
}
origin = new FVector(*position);
for (int i = 0; i < y; i++)
{
body.Add(TArray<ALabyrinthCell*>());
for (int j = 0; j < x; j++)
{
body[i].Add((ALabyrinthCell*) GetWorld()->SpawnActor(ALabyrinthCell::StaticClass(), position));
position->Set(position->X, position->Y + size.Y*2, position->Z);
}
position->Set(position->X + size.Y*2, origin->Y, position->Z);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, FString::FromInt(body[0][0]->getCell().Num()));
}
delete(position);
delete(origin);
}
Here’s the visual result when I send (2,2) to formGrid
And when i try something like “body[0][0]->getCell()” the TMap is empty, why ?