如何通过远程过程调用(RPC)发送一个字典

3

我正在使用Unity制作一款多人游戏。 我已经建立了一些字典来保存分数。 主客户端处理这些字典。

在主客户端更改字典后(例如因为有玩家死亡),并计算出谁处于领先地位后,我想将数据发送给客户端,以便他们可以更新他们的记分板、Hud等。

然而,我似乎无法直接通过rpc访问字典。例如:

[RPC]
void PassScoresToClients (Dictionary plyScrs, Dictionary teamScrs,int leadingTeamID, int leadingTeamScore){

}

词典这个词只是变成了红色。

有没有办法将这些数据发送给客户端?

为了举例说明,我会简单介绍一下它的使用方法。第一个词典通过玩家的用户名作为主键,使用得分类型(击杀数、死亡数、助攻数和团队ID)作为次要键来保存玩家的击杀数、死亡数、助攻数和团队ID。其结构如下:

Dictionary<string, Dictionary<string, int> > playerScores;

子字典会在需要时像这样创建/填充:

子字典会在需要时像这样创建/填充:

public void SetScore (string username, string scoreType, int value)
{
        Init ();
        changeCounter ++;

        if (playerScores.ContainsKey (username) == false) {//if the player has not been recorded in the dictionery yet.
                    playerScores [username] = new Dictionary<string, int> ();// Creates a new dictinerey(sub dictionery) for that player.
        }

        playerScores [username] [scoreType] = value; // the players score is now the value of value
}

第二个字典是一个简单的字典,只是用来跟踪团队得分。它使用团队ID作为键,并结构化如下:
Dictionary<int, int> teamScores;

字典是由主客户端填充的,因此客户端只有互补的空字典,直到它们可以接收数据。
那么有没有办法将数据发送给客户端?
哦,如果您发布代码,能否请加上注释。我对很多事情仍然很新手,并且很容易迷失。
1个回答

0

你应该在你的RPC中使用通用字典:

[RPC]
void PassScoresToClients (Dictionary<int, int> plyScrs, Dictionary<int, int> teamScrs, int leadingTeamID, int leadingTeamScore){

}

DictionaryDictionary<int, int>之间不能自动转换。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接