Download

HasAuthority() returning true for Client

Hi,

I am starting to add multiplayer to my prototype and wanted to try a little around so I fired up a new C++ ThirdPersonTemplate and just wanted to mess around a little. The first thing I tried was printing a text if I am playing on the Server so I added the Tick() function to the C++ character and this 2 lines of code


if(HasAuthority())
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Test"));

I didnt change anything else but the message is printed on both Client and Server and I can’t figure out why.

EDIT
Ok am I missing something here. If I add those line in any other function then the Tick() function it’s working. But why?

I’ve found authority is a bit ambiguous. The client can seemingly have authority over some things and the server others… PERHAPS the tick function give the client authority? Just a guess…

You should watch this tutorial series: https://www.youtube.com/watch?v=TbaOyvWfJE0&list=PLZlv_N0_O1gYwhBTjNLSFPRiBpwe5sTwc&index=1

It is really great and explains all the things you need to start understanding networking in Unreal Engine.

The Client will have Authority for that actor if it spawned the object.

But if that only servers could spawn replicated actors, maybe you mean:
1 - the client call some rpc on server that spawns the actor
2 then some code inside the engine understands this and puts this client having authority over the actor?

Is that correct?
Thanks in advance
Paulo

In practice you have to use HasAuthority along with IsLocallyControlled / IsServer. As Client-side spawning (non-replicated actors) plus Set-Owner overrides along with Listen-Server setups, means HasAuthority isn’t reliable enough to use alone (too many edge cases). You can even use NOT-Standalone / IsServer=False along with IsLocallyControlled logic instead of HasAuthority.:wink:

Clients can still spawn actors - but if they do, the actor will only exist locally and the Client will therefore have authority over it. They won’t be able to call RPC’s, since the actor only exists on their machine. There are many cases where a Client can have Authority over something, another example is if you “Tear Off” a replicated actor, Clients will then have Authority over their local instance of that actor once the torn off state replicates.

HasAuthority() simply means “I can do what I want with this actor” - it doesn’t neccesarily mean that actor is Server-Authoritative.