Search code examples
c++cocos2d-x-2.xphoton

How to obtain event data from ExitGames::Common::Object& eventContent?


Referring to this link "http://recruit.gmo.jp/engineer/jisedai/blog/cocos2d-x_photon/", I'm trying to run the sample that display simple networking feature using cocos2dx 2.2.6 and photon sdk v4-0-0-5. The guide suggested implementing customEventAction this way:

void NetworkLogic::customEventAction(int playerNr, nByte eventCode, const ExitGames::Common::Object& eventContent)
{
    ExitGames::Common::Hashtable* event;

    switch (eventCode) {
        case 1:
            event = ExitGames::Common::ValueObject<ExitGames::Common::Hashtable*>(eventContent).getDataCopy();
            float x = ExitGames::Common::ValueObject(event->getValue(1)).getDataCopy();
            float y = ExitGames::Common::ValueObject(event->getValue(2)).getDataCopy();
            eventQueue.push({static_cast(playerNr), x, y});
            break;
    }
}

Xcode gave error that says:

Cannot refer to class template "ValueObject" without a template argument list

I'm not familiar with templates myself, could anyone suggest a proper method that could allows me to extract event data so that it could be push to eventQueue? Or pointing out what have been wrong in the code above. Many thanks in advance!


Solution

  • Please try the following code:

    void NetworkLogic::customEventAction(int playerNr, nByte eventCode, const ExitGames::Common::Object& eventContent)
    {
        ExitGames::Common::Hashtable* event;
    
        switch (eventCode) {
            case 1:
                event = ExitGames::Common::ValueObject<ExitGames::Common::Hashtable*>(eventContent).getDataCopy();
                float x = ExitGames::Common::ValueObject<float>(event->getValue(1)).getDataCopy();
                float y = ExitGames::Common::ValueObject<float>(event->getValue(2)).getDataCopy();
                eventQueue.push({static_cast(playerNr), x, y});
                break;
        }
    }
    

    I have just changed ExitGames::Common::ValueObject to ExitGames::Common::ValueObject<float> for both, x and y.

    With templates the compiler needs a way to find out for what type it should create the template. As it is not possible for the compiler to get that info from the parameter event->getValue() and as it can't do it based on the return type, you have to specifiy the type of the expected payload data of the ValueObject-instance explicitly by writing ValueObject<type> instead of just ValueObject, so in your case ValueObject<float>.