Is my use of TActorIterator and TArray correct?

Hi all. I’m trying to loop through my game world to gather all characters and store them in my TArray for use within my CameraActor1 class. Right now this is my code inside my CameraActor1 class:

CameraActor1.h

#include "Camera/CameraActor.h"
#include "CameraActor1.generated.h"

/**
 * 
 */

//Forward Declarations
class GameCharacter;

UCLASS()
class GAME_API ACameraActor1 : public ACameraActor
{
	GENERATED_BODY()
	
	virtual void BeginPlay() override;

	virtual void Tick(float DeltaTime) override;
	
private:
	TArray<GameCharacter*> arrayOfCharacters[2];
	
};

CamerActor1.cpp:

#include "Game.h"
#include "EngineUtils.h"
#include "Characters/GameCharacter.h"
#include "CameraActor1.h"


void CameraActor1::BeginPlay()
{
	Super::BeginPlay();
	for (TActorIterator<GameCharacter> CharacterItr(()); CharacterItr; ++CharacterItr)
	{
		GameCharacter* Character = *CharacterItr;
		UE_LOG(LogTemp, Warning, TEXT("Character Initial Location: %s"), *GameCharacter->GetActorLocation().ToString())
		arrayOfCharacters->Add(GameCharacter);
	}
}

void CameraActor1::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

My questions are:

  1. Am I using TActorIterator function and TArray correctly?

  2. If I am then how do I access my characters within my TArray? I’ve tried arrayOfCharacters[0].GetActorLocation() so far to no avail. Any help is appreciated.

1 Like

You deceleration of character array seems to be invalid, TArray is dynamic array so it has no array size limit (well maybe addressing limit) TArray will reallocate the array in memory when needed, so this [2] at the end may mess up things. You can tell TArray to allocate array staicly by doing this:

TArray<GameCharacter*,TFixedAllocator<2>> arrayOfCharacters;

Also you should place UPROPERTY() before TArray declertion, without that this array won’t be tracked by garbage collection and when character will be destroyed you will have a nasty invalid pointer in that array (or else you manually remove it when actor is destroyed)

UPROPERTY()
TArray<GameCharacter*> arrayOfCharacters;

TActorIterator use looks ok to me. You see “Character Initial Location:” in logs right?

I assume you use “GameCharacter” name to mask real name if class, all actor should have A prefix in class name

1 Like

Thank you very much for your answer. Yes I am masking actual names for readability purposes. With your help I was able to get everything to work properly. However, I still get an error when trying to use TFixedAllocator. Within UE4 the error reads:

“Explicit allocators are not supported in TArray properties”

All of done so far with my array is add characters to it:

arrayOfCharacters.Add(GameCharacter);

and get out the locations of the actors:

arrayOfCharacters[0]->GetActorLocation();

If I remove TFixedAllocator then everything works fine.

Ah that means it can’t work with UPROPERTY()

Ah okay. Thanks for all the help. Saved me.

It would be nice if you can post your final piece of working code, so others can benefit from this post as well.

Thanks

1 Like