[IDEA] Make the new Player.Scale a sync var on a component outside of roles.
Goal Description
The new Scale system is great on paper, but comes with a lot of issues:
- Scale is on the FpcRole, meaning on role change you'll have to manually re-apply the scale.
- It doesn't work in the role changed event (crashes the server, assuming this is going to get fixed) making you require to delay the call to change it to work.
- ~~It uses RPCs, which while are set to use the
Reliablechannel, aren't guaranteed to be ordered. If a player drops an incoming role change message, and your scale change message is received beforehand, the player won't receive the new scale.~~ I was corrected with the fact SL has the default channel as Reliable Ordered. - A lot of plugin developers are also using fake role sync. Anytime you send a fake role sync message you'll also have to re-apply the scale. Fake role sync generally also requires delays if you do it on role change, to make sure it doesn't collide, which means now you'll have to delay the fake role sync message, and then delay the scale message some more.
Please consider making scale a SyncVar that lives on a component on the player outside of the role system. Developers can then just reset the scale on role change if they wish, and we don't have to worry about the scale being messed up.
Additionally, this makes it even easier for you, because you won't have to send a message to players that connected & authenticated to sync it. Sync vars will take care of it for you, just hook onto receiving it on the client side.
My current workaround is going to be slapping a component on the player/dummy that just sends a scale sync message to every authenticated player, every X frames/seconds. Just to be absolutely certain it gets received.
EDIT: Works, and somewhat guarantees the scale remains proper even with fake role sync, if anyone else has the same issue as me.
Calling SetScale within one frame after SetRole (including immediate calls which crash the server) will cause it to fail.
My current solution is to use a coroutine that waits for two frames after the call.
public static void SetScaleNextFrame(this Player player, Vector3 scale)
{
Timing.RunCoroutine(SetScaleInternal());
return;
IEnumerator<float> SetScaleInternal()
{
yield return Timing.WaitForOneFrame;
yield return Timing.WaitForOneFrame;
player.Scale = scale;
}
}
Yeah, as explained in the issue, delaying works, but it's hardly reliable, nor clean. Add fake role sync on top of that, and the only way to make your code "reliable" is to track every coroutine delaying said calls, destroying them if the player changes role again, and praying there's no packet loss or change in the ordering of the RPCs.
TLDR: the only way to make the new Scale system work right now is flimsy code.
I stand corrected on the message ordering - SL's default channel is Reliable Ordered.
However, switching to a SyncVar is probably still a good idea.
I stand corrected on the message ordering - SL's default channel is Reliable Ordered.
However, switching to a SyncVar is probably still a good idea.
I agree. At the very least, SetScale should be callable immediately after SetRole.
I also suggested sending the scale in the role sync message as well, which would help.
The upcoming fixes for this resolve all of the issues above, since roles are sent before PlayerRoleChanged and not a frame later. All delays for scaling or fake role sync are now unnecessary. You can set the scale, and send fake role sync in PlayerRoleChanged.
In your fake role sync method, just send a scale message after the fake role sync message if the player's scale isn't Vector3.one
Thank you @davidsebesta1
Can SetScale now be called immediately after SetRole without issues?
Can
SetScalenow be called immediately afterSetRolewithout issues?
No, this has not been fixed yet. But we can provide you with a patch / steps how to fix it for now.
So the above actually ended up with me waking up to our servers on fire. The game really doesn't like the missing delay before sending the role data.
However, we tried another solution which does solve the problems:
- In
PlayerRoleManager.ServerSetRole(), only callPlayerEvents.OnChangedRole()immediately if the new role is Destroyed or None (unsure None is needed but w/e) - If it isn't, hold the changed role args.
- In
Update(), after sending the role sync data, if there's a pending event, execute it.
Resolved in 14.1.3