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.

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.

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.

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

AnyClass.h

TArray<AICharacterClass> AICharacters;

AnyClass.cpp

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()
    }
}