unity – Find out how to make an authoritative server initialize 3 playing cards as soon as 2 “Shoppers” be part of

unity – Find out how to make an authoritative server initialize 3 playing cards as soon as 2 “Shoppers” be part of

[ad_1]

I considerably perceive [Command], [ClientRpc], and [TargetRpc]. Nonetheless, I’m at the moment struggling to find out learn how to get my SetupInitialHand() technique to run. When referred to as from the identical space that checks the participant rely, it simply disconnects the 2nd shopper after the be part of. Known as from anyplace else, it nonetheless does not work, and it solely pulls null.

The present objective is for 3 random playing cards to being drawn into every shopper’s hand. The server ought to simply place the first shopper’s Preliminary hand on the backside and 2nd shopper’s Preliminary hand at high (server solely will every participant will see their playing cards at backside). Making an attempt to remain in keeping with the 1 server + 2 shopper design method @Zibelas beneficial.

Shoppers
enter image description here

Server
enter image description here


public class HandManager : NetworkBehaviour
{
    public GameObject handPanel; // Reference to the panel the place playing cards will likely be displayed
    public GameObject PlayerCard; // Reference to the prefab for the cardboard UI

    // Networked checklist to retailer card IDs within the hand
    public readonly SyncList<int> handCardIds = new SyncList<int>(); // Marked as readonly

    // This technique will likely be referred to as on all shoppers when the handCardIds checklist adjustments
    non-public void OnHandCardIdsChanged(SyncList<int>.Operation op, int index, int oldItem, int newItem)
    {
        // Replace the hand UI when the handCardIds checklist adjustments
        UpdateHandUI();
    }

    // Replace the hand UI primarily based on the handCardIds checklist
    void UpdateHandUI()
    {
        // Clear the hand panel
        foreach (Rework little one in handPanel.remodel)
        {
            Destroy(little one.gameObject);
        }

        // Instantiate card UI prefabs for every card ID within the hand
        foreach (int cardId in handCardIds)
        {
            // Discover the cardboard information primarily based on the cardboard ID from the cardboard database
            Card cardData = CardDatabase.GetCardById(cardId);
            if (cardData != null)
            {
                GameObject newCard = Instantiate(PlayerCard, handPanel.remodel);
                // Arrange the cardboard UI primarily based on the cardboard information
                SetupCardUI(newCard, cardData);
            }
        }
    }

    [ClientRpc]
    // Arrange the UI of a card primarily based on its information
    void SetupCardUI(GameObject cardUI, Card cardData)
    {
        // Set the sprite of the cardboard UI
        cardUI.GetComponent<Picture>().sprite = cardData.spriteImage;
        // Set different card UI components (identify, price, description, and so forth.) if wanted
        // For instance:
        // cardUI.GetComponent<CardUI>().SetName(cardData.cardName);
        // cardUI.GetComponent<CardUI>().SetCost(cardData.price);
        // cardUI.GetComponent<CardUI>().SetDescription(cardData.cardDescription);
    }


    // Technique to arrange the participant's preliminary hand
    public void SetupInitialHand()
    {
        Debug.Log("Establishing preliminary hand...");

        // Randomly choose playing cards from the database and add them to the participant's hand
        for (int i = 0; i < 3; i++)
        {
            GameObject card = Instantiate(PlayerCard, new Vector2(0,0), Quaternion.identification);
            card.remodel.SetParent(handPanel.remodel, false);
        }

        Debug.Log("Preliminary hand setup full.");

    }

    // Deal with interactions with the playing cards within the hand (e.g., clicking or dragging)
    // Add strategies for dealing with card interactions as wanted

    // AddCardToHand technique for native participant (not utilized in networked model)
    [ClientRpc]
    public void AddCardToHand(Sprite cardSprite)
    {

    }

}


public class TurnManager : NetworkBehaviour
{
    // Record to carry the identities of gamers
    Record<NetworkIdentity> _identities = new Record<NetworkIdentity>();

    // Index of the present participant
    non-public int _currentPlayerIndex = 1;

    // SyncVar to synchronize present participant throughout community
    [SyncVar(hook = nameof(NextTurnEvent))] public uint currentPlayer = 0;

    // UnityEvent to invoke when switching to subsequent participant
    public UnityEvent<uint> nextPlayer;

    // UnityEvent to invoke when a participant is registered
    public UnityEvent<NetworkIdentity, int> playerRegisteredEvent;
    
    non-public HandManager handManager;

    // Observe the variety of gamers which have joined
    non-public int playersJoined = 0;

    non-public void Begin()
    {
        // Set the identify of the GameObject to TurnManager
        gameObject.identify = "TurnManager";

        // If this occasion is a server, add a null entry to the identities checklist
        if (isServer) _identities.Add(null);
    }

    non-public void Replace()
    {
        // Examine for the "X" key press
        if (Enter.GetKeyDown(KeyCode.X))
        {
            CurrentList();
        }
    }

    [Server]
    public void RegisterPlayer(NetworkConnectionToClient connection)
    {
        //Deal with right here if reconnects can occur
        Debug.LogFormat("Payer added: {0}", connection.identification);

        // Add the participant's identification to the identities checklist
        _identities.Add(connection.identification);
        
        // Invoke the playerRegisteredEvent UnityEvent with the participant's identification and switch
        playerRegisteredEvent?.Invoke(connection.identification, GetPlayerTurn(connection));


        // Increment the rely of gamers which have joined
        playersJoined++;

        // If all gamers have joined, print the present variety of gamers and whose flip it's
        if (playersJoined == 2)
        {
            Debug.LogFormat("Each gamers have joined. Present variety of gamers: {0}", playersJoined);
            handManager.SetupInitialHand();
        }
    }

    [Server]
    public void NextPlayer()
    {
        Debug.LogFormat("Earlier flip: {0}", currentPlayer + " participant");
        Debug.Log(currentPlayer + " is the earlier participant");

        // Transfer to the following participant
        _currentPlayerIndex++;
       
        // If it exceeds the rely of identities, loop again to the second participant
        if (_currentPlayerIndex >= _identities.Rely) _currentPlayerIndex = 1;
        
        // Set the present participant to the following participant's netId
        currentPlayer = _identities[_currentPlayerIndex].netId;

        Debug.LogFormat("Present flip: {0}", currentPlayer + " participant");
        Debug.Log(currentPlayer + " is the present participant");
    }

    [Server]
    public bool IsCurrentTurn(NetworkConnectionToClient connection)
    {
        // Examine if the offered connection's identification matches the present participant's identification
        if (_identities[_currentPlayerIndex] == connection.identification) return true;
        //Despatched a message to the shopper the motion is out of flip
        //_errorManager.TargetErrorMessage(connection, "Not your flip");
        return false;
    }

    [Server]
    public int GetCurrentPlayerIndex()
    {
        // Return the index of the present participant
        return _currentPlayerIndex;
    }

    [Server]
    public uint GetPlayerByIndex(int index)
    {
        // Return the netId of the participant on the specified index
        return _identities[index].netId;
    }

    public int GetPlayerTurn(NetworkConnectionToClient connection)
    {
        // Return the flip of the participant related to the given connection
        return _identities.IndexOf(connection.identification);
    }

    void NextTurnEvent(uint oldPlayer, uint newPlayer)
    {
        // Invoke the nextPlayer UnityEvent with the brand new participant's netId
        nextPlayer?.Invoke(newPlayer);
    }

    [Server]
    // Operate to output the present _identities checklist
    non-public void CurrentList()
    {
        // Output who's going first
        if (_identities.Rely > 1 && _identities[1] != null)
        {
            Debug.LogFormat("Going first: {0}", _identities[1].identify);
        }

        Debug.Log("Present _identities:");
        foreach (NetworkIdentity identification in _identities)
        {
            if (identification != null)
            {
                Debug.Log(identification.identify);
            }
        }

        Debug.LogFormat("Present flip: {0}", _identities[_currentPlayerIndex].identify);
    }
}
```

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply