0

I am working on multiplayer game using Photon Server. I am send sending some data using HashMaps through loadBalancingPeer.opRaiseEvent but i am not able to get data in onEvent function of LoadBalancingClient.

Here is my custom event code.

public void sendSomeEvent(int playerPosition) {

        HashMap<Object, Object> eventContent = new HashMap<Object, Object>();
        eventContent.put("key1", "ABC");

        this.loadBalancingPeer.opRaiseEvent((byte) 1, eventContent, false,
                (byte) 0); // this is received by OnEvent()
    }

Here is onEvent() code.

@Override
public void onEvent(EventData eventData) {
    super.onEvent(eventData);

    switch (eventData.Code) {
    case (byte) 1:
        String value = (String) eventData.Parameters
                .get("key1");
        ApplicationManager.onEventReceived(ab);
        break;      
    }
    // update the form / gui
    ApplicationManager.onClientUpdateCallback();
}

In this function i am getting null value, but it shows patameter data in this form {-11={key1=ABC}, -2=1}. Please help me where i am getting wrong. Thanks in advance.

faisal ahsan
  • 41
  • 2
  • 10

1 Answers1

1

I have not used Photon but after looking at API docs, I think you are doing things wrong. EventData is also kind of a Map.

In case of raised event the value of eventData.Code should actually be OpertationCode.RaiseEvent and your event ( byte ) 1 will be mapped with key EventCode.Code in the map.

In this EventData, event code is mapped with key EventCode.Code and your data is mapped with key ParameterCode.Data.

So... to get your sent hashmap out of EventData you need to do a eventData.get( ParameterCode.Code ).

I think following should work fine. Try it, and give me a detailed error log, if this does not work.

switch ( eventData.Code ) {
    // If it is a RaiseEvent
    case OperationCode.RaiseEvent:
        switch ( eventData.get( EventCode.Code ) ) {
            // If it is your event.
            case (byte) 1:
                HashMap< Object, Object > map = (HashMap<Object, Object>) eventData.get( ParameterCode.Data );

                HashMap< String, String > smap = new HashMap< String, String >();
                for( Object key : map.keySet() ) {
                    smap.put( (String) key, ( String ) map.get( key ) );
                }
                // Now smap is the HashMap<String, String> that you sent.
                // do something
                break;
        }
        break;
}

Note :: Now I am pretty confident that it should work.

sarveshseri
  • 13,738
  • 28
  • 47