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.
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.
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
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 :