NetCode For GameObjects Unity: 客户端 Rpc 参数空引用异常

huangapple go评论76阅读模式
英文:

NetCode For GameObjects Unity: Client Rpc params null reference exception

问题

我不知道为什么这在我的其他客户端上无法运行,但我尝试了很多次。

这个游戏是扑克牌游戏,我需要帮助。在服务器上,rpc 在 foreach 中调用,将玩家的 ID 添加到 ClientRpcParams 中。

这样做的目的是在服务器绘制玩家的牌时触发事件。

问题在于客户端 rpc 似乎永远不会传递到我另一个正在运行的游戏实例,我认为问题是主机出现错误,指出高亮显示的行存在空引用异常,这是没有道理的,因为我可以在这之前打印传递的值。

请帮忙。

Rpc

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;

public class Server : NetworkBehaviour
{
    public static Server instance;

    private void Awake() 
    {
        DontDestroyOnLoad(gameObject);
    }

    public override void OnNetworkSpawn()  
    {
        instance = this;

        if (!IsServer) 
        {
            return;
        }
    }

    [ClientRpc]
    public void UpdatePlayerClientRpc(TablePlayer.TablePlayerData player, ClientRpcParams clientRpcParams = default) 
    {
        Debug.Log("Something, anything");
        var flag = false;

        // error is line below ---------------------------
        foreach (var item in clientRpcParams.Send.TargetClientIds) 
        {
            if (item == OwnerClientId) 
            {
                flag = true;
            }
        }

        if (flag) 
        {
            if (AppManager.Instance.playerNetworkObject.localPlayer == null) 
            {
                AppManager.Instance.playerNetworkObject.SpawnUi();
            }
            Debug.Log(Client.Instance.TablePlayer.tablePlayerData.position);
            Client.Instance.TablePlayer.tablePlayerData = player;
            AppManager.Instance.playerNetworkObject.ShowHand();
        }
    }

    //[ClientRpc]
    //public void BroadcastPotClientRpc(Pot newPot, ClientRpcParams clientRpcParams = default) {
    //    AppManager.Instance.game.pokerManager.CurrentPot = newPot;
    //}
}

调用(注意:从非行为对象中调用)

foreach (var player in Players) 
{
    for (int i = 0; i < CardsInHand; i++) 
    {
        var randInt = UnityEngine.Random.Range(0, Deck.Count);
        player.Value.Hand.Add(Deck[randInt]);
    }
    Debug.Log(player.Key);
    Server.instance.UpdatePlayerClientRpc(player.Value.tablePlayerData, new ClientRpcParams { 
                Send = new ClientRpcSendParams {
                    TargetClientIds = new ulong[] { player.Key }
                }
    });
}

如果您对这种情况可能发生的原因有任何想法,请告诉我。

谢谢!

我尝试将每个 MonoBehaviour 更改为 NetworkBehaviour,但没有效果,结果相同,而且没有找到关于为什么客户端 rpc 可能不会被调用的文档。

英文:

I don't know why this doesn't run on my other client, but I tried a lot.

The game is poker and I need help. The rpc is called on the server in a foreach which adds the ID of the player to ClientRpcParams.

The goal of this is to have an event to spawn the players cards when they are drawn by the server.

The problem is that the client rpc never makes it to my other instance of the game running, I think the issue is that the host gets an error that the highlighted line has a null reference exception, Which doesn't make sense as I can print the value passed in right before

Please Help

Rpc

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;

public class Server : NetworkBehaviour
{
    public static Server instance;

    private void Awake() 
    {
        DontDestroyOnLoad(gameObject);
    }

    public override void OnNetworkSpawn()  
    {
        instance = this;

        if (!IsServer) 
        {
            return;
        }
    }

    [ClientRpc]
    public void UpdatePlayerClientRpc(TablePlayer.TablePlayerData player, ClientRpcParams clientRpcParams = default) 
    {
        Debug.Log(&quot;Something, anything&quot;);
        var flag = false;

        // error is line below ---------------------------
        foreach (var item in clientRpcParams.Send.TargetClientIds) 
        {
            if (item == OwnerClientId) 
            {
                flag = true;
            }
        }

        if (flag) 
        {
            if (AppManager.Instance.playerNetworkObject.localPlayer == null) 
            {
                AppManager.Instance.playerNetworkObject.SpawnUi();
            }
            Debug.Log(Client.Instance.TablePlayer.tablePlayerData.position);
            Client.Instance.TablePlayer.tablePlayerData = player;
            AppManager.Instance.playerNetworkObject.ShowHand();
        }
    }

    //[ClientRpc]
    //public void BroadcastPotClientRpc(Pot newPot, ClientRpcParams clientRpcParams = default) {
    //    AppManager.Instance.game.pokerManager.CurrentPot = newPot;
    //}
}

Call (note: called from non behaviour if that matters)

foreach (var player in Players) 
{
    for (int i = 0; i &lt; CardsInHand; i++) 
    {
        var randInt = UnityEngine.Random.Range(0, Deck.Count);
        player.Value.Hand.Add(Deck[randInt]);
    }
    Debug.Log(player.Key);
    Server.instance.UpdatePlayerClientRpc(player.Value.tablePlayerData, new ClientRpcParams { 
                Send = new ClientRpcSendParams {
                    TargetClientIds = new ulong[] { player.Key }
                }
    });
}

If you have any idea on why this may be happening please let me know

Thank you!

I tried making every monobehaviour a network behaviour and that didn't work, same result and
researching why might a clientrpc not be called, I didn't find any documentation

答案1

得分: 0

设置clientRpcParams.Send.TargetClientIds会自动将值发送到正确的目标,检查它是多余的,在这种情况下会导致一切破裂。

英文:

Setting clientRpcParams.Send.TargetClientIds automatically sends the value to the correct target, checking for it is redundant and in this case, breaks everything

huangapple
  • 本文由 发表于 2023年6月22日 11:52:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定