Role_Authority and isLocallyControlled() Question

So I’m seeing some really weird functionality with the isLocallyControlled() function.

While running into some problems (I too quickly set bIsReplicated in the GoKart.cpp to false leading to some crazy shenanigan’s that required a few hours of debugging to get everything going again, but that aside) and in an attempt to solve them I decided to see what isLocallyControlled() returns on different clients/servers.

Autonomous Proxy works pretty much exactly as I expected (returns true)

but when checking it for Authority well… that’s were things went weird.

So in the GoKart.cpp I decided to output it into the log as a bool with the following

// Called every frame
void AGoKart::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	
	DrawDebugString(GetWorld(), FVector(0, 0, 100), GetEnumText(GetLocalRole()), this, FColor::White, DeltaTime);
	DrawDebugString(GetWorld(), FVector(0, 0, 150), GetEnumText(GetRemoteRole()), this, FColor::Red, DeltaTime);

	if (GetLocalRole() == ROLE_Authority) {
		UE_LOG(LogTemp, Warning, TEXT("isLocallyControlled for %s returns %s"), *GetEnumText(GetLocalRole()), (IsLocallyControlled() ? TEXT("True") : TEXT("False")));
	}
}

And here was the result…

So… turns out authority both Is and Isn’t player controlled? I mean it… kind of makes sense in a weird way, but at the same time looking at the isLocallyControlled function in the engine for APawn/AController i can’t imagine a situation where it would return anything other than true, especially in this case cause it’s just returning the GetLocalRole check

 bool AController::IsLocalController() const
 {
 	const ENetMode NetMode = GetNetMode();
 
 	if (NetMode == NM_Standalone)
 	{
 		// Not networked.
 		return true;
 	}
 	
 	if (NetMode == NM_Client && GetLocalRole() == ROLE_AutonomousProxy)
 	{
 		// Networked client in control.
 		return true;
 	}
 
 	if (GetRemoteRole() != ROLE_AutonomousProxy && GetLocalRole() == ROLE_Authority)
 	{
 		// Local authority in control.
 		return true;
 	}
 
 	return false;
 }

Anyone have any ideas why it might be doing this?

This is exactly the case where it would return false, if the pawn is controlled remotely.

I suspect the reason you are getting different result in the log is that you have multiple pawns. Try printing the name of the actor so that you can identify the different objects logging out. IsLocallyControlled should be false for all pawns that are autonomous proxy on the clients.

Ah yep, that was it! Was still thinking that only one car would have the role of authority, but forgetting that actually all three do technically count as having the role of authority since they’re all controlled by the server and just take input from the user

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

I can’t count how many times I’ve been caught out by this. Always worth printing the name of the actor or component.

Privacy & Terms