LabAPI icon indicating copy to clipboard operation
LabAPI copied to clipboard

[IDEA] Player::SetRole

Open ui2506 opened this issue 3 months ago • 2 comments

Goal Description

I suggest changing the Player::SetRole method to return the resulting PlayerRoleBase instead of void.

Currently:

public void SetRole(RoleTypeId newRole, RoleChangeReason reason = RoleChangeReason.RemoteAdmin, RoleSpawnFlags flags = RoleSpawnFlags.All)
{
    ReferenceHub.roleManager.ServerSetRole(newRole, reason, flags);
}
ev.Player.SetRole(RoleTypeId.Tutorial);
PlayerRoleBase roleBase = ev.Player.RoleBase;

It could be changed to:

public PlayerRoleBase SetRole(RoleTypeId newRole, RoleChangeReason reason = RoleChangeReason.RemoteAdmin, RoleSpawnFlags flags = RoleSpawnFlags.All)
{
    PlayerRoleManager manager = ReferenceHub.roleManager.ServerSetRole(newRole, reason, flags);

    return manager._curRole;
}

This would allow developers to immediately access the role that was assigned by SetRole, without having to retrieve it separately afterward.

Alternatively, SetRole could return the PlayerRoleManager itself if that is considered more useful for the API.

Notes

No response

ui2506 avatar Sep 01 '26 19:09 ui2506

"Alternatively, SetRole could return the PlayerRoleManager itself if that is considered more useful for the API." How is that gonna be more useful for you?

davidsebesta1 avatar Sep 13 '26 10:09 davidsebesta1

For example:

PlayerRoleBase  role = ev.Player.SetRole(RoleTypeId.Tutorial);

Timing.CallDeleyd(1f, () => {
  if (player.RoleBase != role)
    return;
  
  //This method helps handle cases where, for instance, you need to give items to a player, but they have changed roles—such as upon death
});

ui2506 avatar Sep 17 '26 20:09 ui2506