LabAPI icon indicating copy to clipboard operation
LabAPI copied to clipboard

[IDEA] Make the new Player.Scale a sync var on a component outside of roles.

Open xCynDev opened this issue 1 year ago • 11 comments

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 Reliable channel, 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.

xCynDev avatar Jun 12 '25 21:06 xCynDev

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.

xCynDev avatar Jun 12 '25 22:06 xCynDev

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;
    }
}

hatulaile avatar Jun 13 '25 06:06 hatulaile

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.

xCynDev avatar Jun 13 '25 07:06 xCynDev

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.

xCynDev avatar Jun 13 '25 10:06 xCynDev

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.

hatulaile avatar Jun 13 '25 17:06 hatulaile

I also suggested sending the scale in the role sync message as well, which would help.

xCynDev avatar Jun 13 '25 19:06 xCynDev

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

xCynDev avatar Jun 16 '25 12:06 xCynDev

Can SetScale now be called immediately after SetRole without issues?

hatulaile avatar Jun 16 '25 13:06 hatulaile

Can SetScale now be called immediately after SetRole without issues?

No, this has not been fixed yet. But we can provide you with a patch / steps how to fix it for now.

davidsebesta1 avatar Jun 16 '25 13:06 davidsebesta1

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 call PlayerEvents.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.

xCynDev avatar Jun 16 '25 22:06 xCynDev

Resolved in 14.1.3

davidsebesta1 avatar Aug 01 '25 21:08 davidsebesta1