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"

Get AI character location C++

Hi, Note I am relatively low skilled in C++. I am trying to:

  • loop over AI characters in the game and get there location every frame or every some period of time (say every 0.5s) (NOT ACCOMPLISHED)

  • Keep this data in an array or somewhere till the game ends. (NOT ACCOMPLISHED)

  • When the game ends produce a csv file with the data in the array that was collected during the game running time.(ACCOMPLISHED)

As far as I know, TArray and TActorIterator should do the job, but I can't seem to be able to use them properly. I was able to produce csv files. So the main issue is with the first two points. A detailed answer would be very much appreciated.

Thanks in advance.

Product Version: UE 4.20
more ▼

asked Jan 23 '19 at 01:22 PM in C++ Programming

avatar image

10 2 4

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

1 answer: sort voted first

This would be one way to achieve what you want:

 void AAnyClass::Tick()
     TArray<AYourAICharacterClass> AICharacters;
     UGameplayStatics::GetAllActorsOfClass(this, AYourAICharacterClass::StaticClass(), AICharacters);
     TMap<AYourAICHaracterClass> AICharacterToLocations;
     for (AYourAICharacterClass AICharacter : AICharacters)
         AICharacterToLocations.Add(AICharacter, AICharacter->GetActorTransform());

but it would be the most 'hacky' way, as the use of GetAllActorsOfClass() is even mentioned in its comment that is very expensive and mustn't be used every frame (what my example does), but if you trigger this method at a specific point in your game (e.g. when every ai character was spawned) then you can cache the resulting array from it and iterate through the cached array.

If it is not deterministic for you to say when "every" AICharacter is spawned then I would recommend to add the AICharacters to a cached array, at the moment when they're spawned and removed when they're destroyed (but the information still would need to be persisted). But if you're a beginner then this might be a much more complex approach and not even needed. I hope my answers helped.

more ▼

answered Jan 23 '19 at 02:13 PM

avatar image

11 2 4

avatar image Abanoub-G Jan 23 '19 at 04:45 PM

Assuming all characters are spawned at time = 0 and no new characters get spawned or destroyed during the game. It should probably be not too hacky if we get references to the AI characters and save them in an array in begin play. Then use this array of references to call the location of each character AI in every tick. Any idea how I can accomplish that in terms of code?

In this thread, the guy is trying to do something very similar to what I want to do but I can't get his code to work and he didn't really mention how he ended up making it work.

avatar image Caviarail Jan 23 '19 at 04:56 PM

Eh. Maybe I don't quite understand you, but the code I posted pretty much does exactly that. It gets alle available characters in the current scene and stores them into the TArray AICharacters and for easierer mapping with their Location into the TMap.

Just place it somewhere else (not Tick()), but maybe in a BeginPlay() and instead of caching it in a local TArray make the Array a property of your class in your header file.like


 TArray<AICharacterClass> AICharacters;


 void AnyClass::BeginPlay()
     UGameplayStatics::GetAllActorsOfClass(this, AYourAICharacterClass::StaticClass(), MyCachedCharacters);
 void AAnyClass::Tick()
     for (AYourAICharacterClass AICharacter : AICharacters)
         // Do sth. with AICharacter
         // Do sth. with AICharacter->GetActorTransform()

(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