Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Make button available only for selected player

Discussion in 'Netcode for GameObjects' started by izmash, Nov 6, 2022.

  1. izmash

    izmash

    Joined:
    Oct 31, 2018
    Posts:
    2
    Hi, I would like to ask you for help with my problem. Long story short Im making turn based game where i want to make some ServerRpc functions only allowed to call by selected player. I thought the best and probably the only one way to do it would be changing visibility or SetEnabled property of buttons (Im using Unity UITollkit), so i figured it out by making separate Ui document only for these buttons and changing thier visibility by using NetworkShow function (https://docs-multiplayer.unity3d.com/netcode/current/basics/object-visibility/index.html), but unity netcode doesn't allow to hide network object from server (in my case im not using dedicated server architecture but one player act as host). Is it any way to workaround or it is just impossible at current state?

    Unity error:
    VisibilityChangeException: Cannot hide an object from the server
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,508
    The UI needn't (shouldn't) be networked at all.

    You can have the client request via ServerRPC whether it should show/hide a certain UI element, or let the client determine that altogether by himself.

    For example, assuming you have like 4 decks of cards from left to right at the bottom, you'd only make visible the top card for the player that owns the deck simply based on the client's player index - both server and client know their index, and the server can map or assign player index from a clientId.
     
  3. izmash

    izmash

    Joined:
    Oct 31, 2018
    Posts:
    2
    Ok, finally figured how to make it work. I've used ClientRpc that run functions on client side to disable certain ui elements using player ID that is changing each turn. So every turn im initializing new ClientRpcParams and puting current player id inside TargetClientIds which looks like that:


    Code (CSharp):
    1. ClientRpcParams clientRpcParams = new ClientRpcParams()
    2.         {
    3.             Send = new ClientRpcSendParams()
    4.             {
    5.                 TargetClientIds = new ulong[] { CurrentPlayerId }
    6.             }
    7.         };
    and then i'm calling some ClientRpc functions with that clientRpcParams e.g.:

    Code (CSharp):
    1. boardUIMenager.DisableDiceButtonClientRpc(clientRpcParams);