How do I create a variable in C++ that stores a specific character? or accesses a specific character?

For instance, I want to create an enemy that only targets one specific character on screen. Right now I have the enemy walking towards the location of the player character using

ACharacter* MainCharacter = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);

and manipulating the Maincharacter stuff, but when I switch to a different playable character, now they start to target that character. I know why it does this, because it gets 0 on the player index and when I switch to the other character he is still player 0 index.

What I would like is to create a reference to main character of the game, and then when I switch to the second character, the enemy will still target that main character because they’re doing logic only with the reference of the main character.

In blueprint you solve this by doing a cast. Now I have very little knowledge how to use this correctly and I’d like an idea of how to do so.

I have ACharacter_Main
and ACharacter_Partner
and ACharacter_Enemy
if that helps…

Hey Demoneyejin

Why not store a pointer to the character you want the enemy to target? Say in the game mode? You could initialize it with the UGameplayStatics function, but thereafter it just sits in the game mode and the enemy makes a call to the game mode to get it? Thats if you’re spawning new enemies- otherwise you could probably store the pointer as a data member of the AI? You could make it private and have getter and setter functions in the AI.