使用GetPlayerController传入对应的index,获取的就是对应的PC,然后你可以cast对应的子类。
如果你不知道index填几,可以给你想要获取的PC派生类中自己做个标记,然后遍历PC,筛选出来。
While writing some testing code using the unreal’s automation framework, I tried to import a variable from the PlayerController of my project, then I found out that I can get the pointer of PlayerController using UGameplayStatics::GetPlayerController(), but only the server version. The variable I need only resides within the client version of PlayerController.
Is there a way to designate which version of the code to access?
Thanks for the advise. Now I can get the correct PlayerController like this:
const TIndirectArray<FWorldContext>& WorldContexts = GEngine->GetWorldContexts();
for (const FWorldContext& Context : WorldContexts)
{
if (Context.World() != nullptr)
{
if (Context.WorldType == EWorldType::PIE)
{
world = Context.World();
for (auto Iterator = world->GetPlayerControllerIterator(); Iterator; ++Iterator)
{
APlayerController* PlayerController = Iterator->Get();
if (!PlayerController->HasAuthority())
{
// This is the client playercontroller I need, do whatever the rest here.
}
}
}
}
}