I would like to be able to get the instance of the current character that is in the game from another class, as well as cast it to my own class that i’ve derived from ACharacter.
Hey you can do the following. (Edited to fix some issues with prev ocde)
auto It = GetWorld()->GetPlayerControllerIterator();
for (int Idx = 0; Idx < 1; ++Idx)
{
if (It->Get() /* && Idx == WANTED_PLAYER_INDEX */ )
break;
++It;
}
APlayerController* PC = *It; // May need to be AYourPlayerController:: instead...
if (PC)
{
// You have your controller
APawn* ThePawn = PC->GetPawn();
if (ThePawn)
{
// Get that controllers pawn
AYourProjectPlayerClass* Player = Cast<AYourProjectPlayerClass>(ThePawn);
if (Player)
{
// You have your Character Class
}
}
}
This is from the top of my head i am at work so i have no chance of testing it for you atm.
Hope it helps.
Edit: BTW to get the IDs for players, you can get the GameState and then use PlayerArray in AGameState::
And credits to Arshia001 for the interation code.
PC->GetPawn() is returning null…
Hmm hard to say you can try.
APlayerController::GetPawnOrSpectator
Sorry, I can’t try it. Unreal has decided to keep throwing an error saying it cant find the *.generated.h file for a few of my headers. Looking for a fix for that, than i’ll test your code
Ok try to right click on your .uproject and press “Generate Visual Studio Project Files” Thats should do the trick.
Thanks for the tips man! Your code works