Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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:


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

 //Forward Declarations
 class GameCharacter;
 class GAME_API ACameraActor1 : public ACameraActor
     virtual void BeginPlay() override;
     virtual void Tick(float DeltaTime) override;
     TArray<GameCharacter*> arrayOfCharacters[2];


 #include "Game.h"
 #include "EngineUtils.h"
 #include "Characters/GameCharacter.h"
 #include "CameraActor1.h"
 void CameraActor1::BeginPlay()
     for (TActorIterator<GameCharacter> CharacterItr(GetWorld()); CharacterItr; ++CharacterItr)
         GameCharacter* Character = *CharacterItr;
         UE_LOG(LogTemp, Warning, TEXT("Character Initial Location: %s"), *GameCharacter->GetActorLocation().ToString())
 void CameraActor1::Tick(float 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.

Product Version: UE 4.10
more ▼

asked Apr 14 '16 at 07:49 PM in C++ Programming

avatar image

50 14 21 28

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

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)

 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

more ▼

answered Apr 14 '16 at 08:37 PM

avatar image

35.2k 921 166 1098

avatar image Boagz Apr 14 '16 at 09:00 PM

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:


and get out the locations of the actors:


If I remove TFixedAllocator then everything works fine.

avatar image Shadowriver Apr 15 '16 at 12:08 AM

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

avatar image Boagz Apr 15 '16 at 12:14 AM

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

avatar image Abanoub-G Jan 23 '19 at 12:05 PM

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


(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question