Search Unity

Multiplayer card game: controlling your own cards?

Discussion in 'Multiplayer' started by The-Oddler, Jul 3, 2015.

  1. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133
    Hey fellow developers,

    tl;dr: How do you have multiple objects controlled (and possibly created) by one client, while other objects might be controlled by other clients? Also how do you transfer ownership?

    I'm making a multiplayer card game, and was just trying out the new networking system. I created a player object that can create cards, and you can double click cards to tap them. However I would like the player to control his own cards (possibly transferring ownership during the game). But I can't seem to get it to work.

    So, I have a simple scene with a scaled cube as the floor on which the cards are placed. I also created a player prefab with some very simple code to create a card when pressing space:

    Code (CSharp):
    1.     [SerializeField]
    2.     private Card _testCard;
    3.  
    4.     void Update()
    5.     {
    6.         if (!isLocalPlayer) return;
    7.  
    8.         if (Input.GetKeyDown(KeyCode.Space))
    9.         {
    10.             RaycastHit hit;
    11.             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
    12.             {
    13.                 CmdPlayTestCard("Test", hit.point);
    14.             }
    15.         }
    16.     }
    17.  
    18.     [Command]
    19.     void CmdPlayTestCard(string name, Vector3 position)
    20.     {
    21.         var card = Instantiate(_testCard);
    22.         card.transform.parent = transform;
    23.         card.transform.position = position;
    24.         card.Name = name;
    25.         NetworkServer.Spawn(card.gameObject);
    26.     }
    My card also implements `IPointerClickHandler` to check for double clicks. It sets a bool "tapped" to true, which is synced using syncvar. This works, I can tap cards on the server with this code, but not on the client. Even when I created the card (by pressing space) on the client.

    I also tried putting the code from the command `CmdPlayTestCard` directly in the update method. It then creates the card only on the client. The client is then able to tap the card, but the server doesn't have this card.

    So, my question. How would this be made with UNet? I need a player that can control his own cards, move them, tap them, create them, ... It should also be possible to transfer ownership of the card to another player, some cards have this as an effect.

    Thanks!
     
    Last edited: Jul 3, 2015
    Acegikmo likes this.
  2. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    I would love to know this as well
     
  3. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116
    You have to register the prefab for the card on each of the clients else it won't be spawned on the clients.
    ClientScene.RegisterPrefab

    AS for the tapping itself, do you have localPlayerAuthority enabled on the NetworkIdentity?
     
    Last edited: Jul 3, 2015
  4. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133
    It's not a problem spawning them on the client. The problem is that only the server can call "NetworkServer.Spawn" (which makes sense with that name), so the client doesn't control it.
    Does that registerprefab do the same thing as adding the prefab to the "Spawnable Prefabs" in the network manager?

    I did indeed check the localPlayerAuthority on the card, but that doesn't seem to change anything.
     
  5. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133
    I found "NetworkServer.SpawnWithClientAuthority" in the release notes for Unity 5.2 beta 1. This sound like what I'm looking for, guess I'll have to wait until 5.2 releases:

     
  6. dazman76

    dazman76

    Joined:
    Jul 23, 2013
    Posts:
    2
    It does indeed :)

    Nice, thanks for the heads up on that. I'm prototyping at the moment, and this will definitely be handy.
     
  7. The-Oddler

    The-Oddler

    Joined:
    Nov 26, 2010
    Posts:
    133
    No problem :D Shame I can't try the beta though, don't have pro :( Guess I'll have to wait until september to try the networking again :p