Access class variable with GetOverlappingActors()

Hello,

I have a problem with GetOverlappingActors() to create a capture the flag zone.
I want to get a list of overlapping characters, and check their team.
Here is my current code :

// Called every frame
void AFlagObjective::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);



	TArray<AActor*> PlayersIn;
	GetOverlappingActors(PlayersIn, TSubclassOf<ASoldier>());

	FString nom = "";
	if (PlayersIn.Num() >= 1)
	{
		nom = PlayersIn[0]->GetName();
		//EnterEffect();

		//int team = PlayersIn[0]->GetTeam();   //does not work because it seems PlayersIn[0] is an AActor class, not my ASoldier class...
	}
	else
	{
		nom = "";
	}

	GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Purple, "Nom : " + nom);

	UE_LOG(LogTemp, Warning, TEXT("Nom du character qui capture : "), *nom);		

}

What’s weird is the fact that the GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Purple, "Nom : " + nom); says “Soldier_0” so my character class named Soldier is listed in the TArray…

BUT I cannot access a public variable of Soldier.cpp directly, or by using a GetVariable() function, because the compiler says it cannot find the variable or the function in the class AActor. For some reason, the compiler is looking for GetVariable() in AActor.h instead of Soldier.h… This is the code that does not work : //int team = PlayersIn[0]->GetTeam();

Another problem : I can’t write :
TArray PlayersIn; GetOverlappingActors(PlayersIn, TSubclassOf());
or
TArray PlayersIn; GetOverlappingActors(PlayersIn, TSubclassOf());
because GetOverlappingActors() only accepts TArray<AActor*>, nothing else.

The answerhub destroyed this part of my initial post :

So what I was saying, is that this code is working :

TArray<AActor*> PlayersIn;
GetOverlappingActors(PlayersIn, TSubclassOf<ASoldier>());

but I cannot replace AActor* by something else, because GetOverlappingActors only accepts an AActor* array.
The TSubclassOf in the 2nd line is just a filter to select what will be stored in PlayersIn, but PlayersIn 's type is still AActor.

I just realized I wrote

int team = PlayersIn[0].GetTeam();

instead of

int team = PlayersIn[0]->GetTeam();

in the 3rd screenshot.

The correct thing is ->, and this is the error I get :

C:\UE4_forks\Unreal_Projects\VRFracture\Source\VRFracture\FlagObjective.cpp(64) : error C2039: 'GetTeam'?: is not a member of 'AActor'

Hey,
you need to cast an element of your array to the ASoldier because AActor has no function called GetTeam().

ASoldier* SoldierRef = Cast<ASoldier>(PlayersIn[0]);

if(SoldierRef)
{
	int32 Team = SoldierRef->GetTeam();
}

Cheers,

Shaggy41

Thank you @Shaggy41, however I still have a question.

I am not experienced with casting.
I want my AFlagObjective.cpp to go look for the ASoldier.cpp 's team. Actually, multiple ASoldier.cpp instances considering the goal is to count the number of players per team in the capture area, I haven’t done the loop yet because I was trying to get this code to work.

You are telling me I need to cast an element from my array to the ASoldier, but I thought I had to do the opposite : to cast something from the ASoldier to the AFlagObjective considering the 2nd needs the informations of the first?

Anyway, I will try your solution and tell you if it works :wink:

Well, your code works but my implementation doesn’t.

// Called every frame
void AFlagObjective::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);



	TArray<AActor*> PlayersIn;
	GetOverlappingActors(PlayersIn, TSubclassOf<ASoldier>());

	FString nom = "";
	if (PlayersIn.Num())	//On vérifie que l'array n'est pas vide (NULL) car le jeu crasherait. if(PlayersIn.Num() >= 1) fonctionne aussi.
	{
		nom = PlayersIn[0]->GetName();
		//EnterEffect();

		ASoldier* SoldierRef = Cast<ASoldier>(PlayersIn[0]);
		if (SoldierRef)
		{
			int32 team = SoldierRef->GetTeam();
			GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Red, "Team : " + team);
			UE_LOG(LogTemp, Warning, TEXT("Equipe : "), team);
		}

		//int team = *PlayersIn[0]->GetTeam();
	}
	else
	{
		nom = "";
	}

	GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Purple, "Nom : " + nom);		
	UE_LOG(LogTemp, Warning, TEXT("Nom du character qui capture : "), *nom);		
	
	
}

I wanted to print on screen and in the output console the team int to check if it works - it doesn’t.

I fixed it.
For some reason, the int was being transformed into an other character like if it was an ascii code instead of being displayed by AddOnScreenDebugMessage().
So I simply wrote :

 if (team == 1)
    {
    	GEngine->AddOnScreenDebugMessage(-1, 0.0f, FColor::Red, "Equipe n1 ");
    }