c++ - 如何从 Photon eventContent 字典中获取数据

标签 c++ dictionary photon

我们在触发事件时使用 ExitGames Photon Realtime 引擎接收此回调

customEventAction(int playerNr, 
                  nByte eventCode,
                  const ExitGames::Common::Object& eventContent)

如果对象是一个字符串,我们使用这段代码来提取它

ExitGames::Common::JString str = 
    ExitGames::Common::ValueObject<ExitGames::Common::JString>(eventContent).getDataCopy(); 

但是,发送的对象是一个字典。它是使用 BroadcastEvent 从服务器发送的。

我们如何从中获取数据?

我们试过了,但没有任何意义:

ExitGames::Common::Dictionary<byte,ExitGames::Common::Object>  pdic
    = ExitGames::Common::ValueObject<ExitGames::Common::Dictionary<byte,ExitGames::Common::Object>>(eventContent).getDataCopy();

我找到了从哈希表中获取数据的代码,但这也不起作用。

谢谢

肖恩

最佳答案

ExitGames::Common::Dictionary<nByte, ExitGames::Common::Object> dic = ExitGames::Common::ValueObject<ExitGames::Common::Dictionary<nByte, ExitGames::Common::Object> >(eventContent).getDataCopy();

绝对正确,适合我。

问题的原因必须在另一行内。

当您将其中一个 Photon C++ 客户端 SDK 中的 demo_loadBalancing 中的 sendEvent() 和 customEventAction() 的实现替换为以下代码片段时,该演示将成功发送和接收字典:

发送:

void NetworkLogic::sendEvent(void)
{
    ExitGames::Common::ValueObject<ExitGames::Common::JString> obj(L"test");
    ExitGames::Common::Dictionary<nByte, ExitGames::Common::Object> dic;
    dic.put(1, obj);
    mLoadBalancingClient.opRaiseEvent(false, dic, 0);
}

接收:

void NetworkLogic::customEventAction(int /*playerNr*/, nByte /*eventCode*/, const ExitGames::Common::Object& eventContent)
{
    EGLOG(ExitGames::Common::DebugLevel::ALL, L"");
    ExitGames::Common::Dictionary<nByte, ExitGames::Common::Object> dic = ExitGames::Common::ValueObject<ExitGames::Common::Dictionary<nByte, ExitGames::Common::Object> >(eventContent).getDataCopy();
    const ExitGames::Common::Object* pObj = dic.getValue(1);
    ExitGames::Common::JString str = ExitGames::Common::ValueObject<ExitGames::Common::JString>(pObj).getDataCopy();
    mpOutputListener->write(L"received the following string as Dictionary value: " + str);
}

这在接收客户端上给出了以下输出行:

received the following string as Dictionary value: test

关于c++ - 如何从 Photon eventContent 字典中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35603884/

相关文章:

c++ - 如何将结构传递给另一个类中定义的 C++ 函数?

python - 构建列表列表的字典理解 : referencing the current value for a key during comprehension

c# - 将 Dictionary<string, string> 转换为 xml 的简单方法,反之亦然

javascript - 您如何将安全的 websockets 与 Photon 实时 Javascript SDK 一起使用?

c# - 相机跟随实例化光子多人游戏

c++ - 如何将自动 lambda 参数限制为指向成员函数的指针?

c++ - 为什么以下正则表达式代码在 VC++ 2013 Debug模式下崩溃

c# - 使用 C++ 中的 COM 对象,在 C#.NET 中返回对象 []

python - 提取字典的子集后如何保持顺序?

unity-game-engine - 在 Unity 3D 中使用 PUN2 进行 VR 多人游戏体验时出现问题