Im Unity and Netcode for Game Objects. I want to create my own Cardgame, so I want to have a synced Cardlist wich is controlled over the Server/Host.
To get started, I wanted to synchronize a list (deck) from which individual clients can remove strings and add them to their list (PlayerCards). The deck should then be synchronized for everyone.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
public class PlayerNetwork : NetworkBehaviour
{
//Cards in the Deck to draw
List<string> Deck = new List<string>();
//Cards on each Player
List<string> PlayerCards = new List<string>();
private void Start()
{
Deck.Add("Card1");
Deck.Add("Card2");
Deck.Add("Card3");
Deck.Add("Card4");
Deck.Add("Card5");
}
void Update()
{
if (!IsOwner)
{
return;
}
if (Input.GetKeyDown(KeyCode.T))
{
CardServerRpc(OwnerClientId);
}
}
[ClientRpc]
private void UpdateCardsClientRpc()
{
Deck.RemoveAt(0);
Debug.Log("Id:" + OwnerClientId);
}
[ClientRpc]
private void AddCardsToPlayerCardClientRpc(ClientRpcParams clientRpcParams)
{
Debug.Log("Id:" + OwnerClientId +"Card:" + Deck[0]);
PlayerCards.Add(Deck[0]);
}
[ServerRpc]
private void CardServerRpc(ulong index)
{
if (Deck.Count >0)
{
AddCardsToPlayerCardClientRpc(new ClientRpcParams { Send = new ClientRpcSendParams { TargetClientIds = new List<ulong> { index } } });
UpdateCardsClientRpc();
}
}
}
2 Point:
- If i call from a Client a ServerRpc and the ServerRpc calls a ClientRpc and i have 2 Clients, should’t a Debug be called twice in the Consol? When I press T, I can only see the message in the console from the client I am currently using.
- Only the Client i open in Unityeditor, not a build one, is able to Add a Card to the Player