customEvent - Convert Object into Hashtable

Options
SeDorn
edited November 2014 in Native
I am currently porting my game from RakNet to Photon and ran into a problem:
In my Network class i am sending my movement data to Photon:
void Network::sendMovementData(PlayerMovementData *data){

	byte eventCode = ID_PLAYER_MOVEMENT; //1
	ExitGames::Common::Hashtable* evData = new ExitGames::Common::Hashtable();
	
	//X -> Float
	evData->put("X", data->getMovementX());
	
	//Y -> Float
	evData->put("Y", data->getMovementY());
	
	//Z -> Float
	evData->put("Z", data->getMovementZ());
	
	//Rotation -> Float
	evData->put("roatation", data->getRotation());
	
	//JumpForce -> Float
	evData->put("jumpForce", data->getJumpForce());
	
	//Crouching -> bool
	evData->put("crouching", data->isCrouching());

	//Send Data
	peer->opRaiseEvent(false, *evData, eventCode);
}

After that I recieve the data over the virtual listener function which returns a Common::Object:
void Network::customEventAction(int playerNr, nByte eventCode, const ExitGames::Common::Object &eventContent){}
If I convert this Object into a JString, i see that my Client recieves all the data sucessfully. My problem is that I don't know how to get the values back to a float.

I tried already this, which returned 0:
float x = ExitGames::Common::ValueObject<float>(eventContent).getDataCopy();

Is there a way to convert that Object into an Hashtable where I can access all my values?

I am using Photon Windows SDK v3-2-5-5

I hope someone can help me.

Comments

  • Kaiserludi
    Options
    Hi SeDorn.

    The received Object is just a wrapper object around whatever you have passed in on the sending side. So as you have passed in a Hashtable on send, your Object contains a Hashtable on the receiving side, not a float, so you can't simply do something like
    [code2=cpp]float x = ExitGames::Common::ValueObject<float>(eventContent).getDataCopy();[/code2]
    If you could do that, then how should Photon be able to determine which float you want to get back? There would be no way for Photon to know when you want to get back the value of x and when the one of y.

    So as eventContent in your case actually holds a Hashtable, instead of
    [code2=cpp]ExitGames::Common::ValueObject<float>(eventContent)[/code2]
    you should write
    [code2=cpp]ExitGames::Common::ValueObject<ExitGames::Common::Hashtable>(eventContent)[/code2]

    A call to getDataCopy() on that Object will return a Hashtable that should contain the entries that you have put into the Hashtable on the sending side.

    You then can access those entries by calls to Hashtable::getValue():
    [code2=cpp]ExitGames::Common::Hashtable evData = ExitGames::Common::ValueObject<ExitGames::Common::Hashtable>(eventContent);
    if(evData.getValue(L"X"))
    x = (ExitGames::Common::ValueObject<float>(evData.getValue(L"X"))).getDataCopy();[/code2]

    Please tell me if that helps or if you need further information.

    PS:
    As movement data is something that normally gets sent very often, you might want to consider using byte keys instead of string keys or to exclusively use strings that are each only one character long for string-keys, to optimize your network traffic.

    PPS:
    With Photon it is good practice to use widestrings L"" instead of narrow strings "" for string literal to save on the cost of an unneeded unicode conversion. Normally the performance impact of those conversions should be negligible for networking code, so this is not critical.
  • Thanks for the fast reply, it now all works fine :)

    Also thanks for the optimization tipps, I will now try to lower the network traffic and make the movement data more smoother.

    Bye!