How to pass callback to custom event listener?

edited February 2015 in JavaScript
Thanks for opening this forum!

The JavaScript SDK documentation gives 'callback' as the second param of addCustomEventListener(). But how do I pass it a callback? raiseEvent() sends a JSON object, not a function (and that's what happens in the chat and realtime demo apps). Or can I use the JSON object to pass the callback (I'm not too familiar with JSON) (yet!).

Comments

  • You can pass in a function name only to execute that function when the callback happens
    addCustomEventListener('myEvent', onMyEvent);
    

    ... or inject the function body directly, e.g. ...
    addCustomEventListener('myEvent', function () { alert('am called by myEvent'); });
    
  • edited June 2012
    Thanks for your reply, Stefan.

    [EDITED]
    I see now that I misunderstood the interaction between addCustomEventListener() and raiseEvent(). I've got a clearer understanding now, and will attack Construct 2 plugin development from a different angle.
    [/EDITED]
  • Your code should send along the id of the player thats moving. Then on the receiving side you can us that id to lookup its object and apply the move data you got from event '1'
    If its only a single object per player, you can use the peer of the sender itself to identify it, if its more than 1 object per player you better get a room wide UID system in place to identify them.

    Thats the same it would be on any platform if you send through the informations through a single 'for all entities in game' pipeline.
  • [EDITED]
    Thanks for your reply, dreamora. On the receiving side, the addCustomEventListener() callback gets the the id of the player that's moving as this.myActor.photonId, so that's not a problem.
    [/EDITED]
  • are there any further problems where some input will hopefully allow you to progress?

    Not that I intend to use Construct2, but I might come back to ask you about your experience for usage with Game Maker HTML5 :)
  • dreamora wrote:
    are there any further problems where some input will hopefully allow you to progress? ...
    Many thanks for your kind offer, dreamora. I'll certainly keep it in mind - I've no further questions at this stage. Having jumped the callback hurdle, I'm now making progress with drawing remote players/actors.

    Please feel free to pick my brains (such as they are) for input with your GM HTML5 project.
  • Thanks for your reply, dreamora. On the receiving side, the addCustomEventListener() callback gets the the id of the player that's moving as this.myActor.photonId, so that's not a problem.
Sign In or Register to comment.