When setting up the Unit storing for the player only, the way it’s implemented in the course is by using
OnStartClient()
and then checking if we have authority.
Wouldn’t it also work - and be easier - to just use
OnStartAuthority()
directly? Since it’s the same, but only called if we have authority, we wouldn’t have to worry about checking via an if statement, whether or not we have authority.
Or am I missing something here?
So after experimenting a bit, I’ve noticed that there’s a difference, but I’m not entirely sure what the difference is.
So on the course the code used, is:
public override void OnStartClient() {
if (!isClientOnly || !hasAuthority) { return; }
AuthorityOnUnitSpawned?.Invoke(this);
}
public override void OnStopClient() {
if (!isClientOnly || !hasAuthority) { return; }
AuthorityOnUnitDespawned?.Invoke(this);
}
What I tried is doing the exact same, just using OnStartAuthority(), to only have to do one if Statement, rather than checking for authority too.
public override void OnStartAuthority() {
if (!isClientOnly) { return; }
AuthorityOnUnitSpawned?.Invoke(this);
}
public override void OnStopAuthority() {
if (!isClientOnly) { return; }
AuthorityOnUnitDespawned?.Invoke(this);
}
While this worked the same as the code from the lecture, this also resulted in the list only being updated on the Server/Host side, and never on the side of a client that just joined the server.
Removing the if statement would however cause duplicates to appear.
Now I’m confused over the “isClientOnly” bool, or rather why it’s apparently false for both the unit on the client, as well as the unit on the host/server.
With experimenting a bit, I figured out that replacing the “!isClientOnly” with “isServer” would result in the same behaviour as in the lecture.
public override void OnStartAuthority() {
if (isServer) { return; }
AuthorityOnUnitSpawned?.Invoke(this);
}
public override void OnStopAuthority() {
if (isServer) { return; }
AuthorityOnUnitDespawned?.Invoke(this);
}
So while it works, I’m not sure why it works, or rather what the difference is.
Basically why does checking “isClientOnly” work if we’re starting in the OnStartClient method, but not when starting in the OnStartAuthority method?
Also the exact difference between isClientOnly, isClient, isServer and isServerOnly.
Cause while I’m happy I got it to work, it would help if I understood why it’s working with the changes I made, and didn’t work before, as that would make tracking down issues easier for me in the future.