Jump to content

Order of events


MonsieurBlutbad
 Share

Recommended Posts

Hello,

I am working on a turn based game and I am trying to set up a Event/Signal chain for handling the turn sequence. Right now I have a endTurnEvent and a newTurnEvent. For the endTurnEvent I have some listeners that do clean up the ended turn and for the newTurnEvent I have some listeners that do the preparation for the new turn. Now in one of my endTurnEventListeners I also want to dispatch my newTurnEvent. But the way in which the events are handled seems to be that the eventListeners for the newly dispatched event get handled before those eventListeners left over from the older event are handled.

 

So what I wished for is this:

1. endTurnEvent gets dispatched

2. one endTurnEventListener dispatches newTurnEvent

3. all other endTurnEventListeners are handled

4. all newTurnEventListeners are handled

 

But what I get is this:

1. endTurnEvent gets dispatched

2. one endTurnEventListener dispatches newTurnEvent

3. all newTurnEventListeners are handled

4. all other endTurnEventListeners are handled

 

Is there a way to get the behavior I wish? 

If not I guess I have to rethink the way in which I use events, any tipps for a solid event structure for turn based games would be very appreciated as well.

 

Thank you 

 

Link to comment
Share on other sites

Yeah, code will help but your 'pseudo-code' does explain where it looks like the problem is.

Your endTurn listener snags the first event and the fires immediately for a newTurn, since events are synchronous (usually) it gets fulfilled immediately i.e. before the other endTurn listeners respond to the event. If you need all endTurn listeners to trigger before the newTurn then either use only one or, if they have asynchronous operations, make sure they have all finished before dispatching your next event, this would be a good use-case for promises although there are lots of ways to skin this cat (if indeed that is your problem).

Events and event listeners are best suited as responders i.e. they are good when you dont know the order in which they will occur, such as reacting to user input (there is a reason the DOM is filled with all those addEventListeners :) ). It sounds like you have a synchronous control flow and so pub/sub may not be particularly suited your use case (hard to say without more info).

Link to comment
Share on other sites

Quote

Any chance you could accompany your post with actual code? It'd be easier to understand your situation and help you out. ;)

Yeah I know, but it would have been a bit complicated to get the code snippets together, because they are scattered across multiple objects and I thought I could lay out the problem without them. But I will sure to include some actual code next time. Sorry about that.

I think you are right mattstyles, thank you very much for your answer. It looks like promises seem to be the way to go. Phaser doesn't support them out of the box right? So am I free in my choice of libraries or is there something to consider? I am leaning towards Q.

 

Link to comment
Share on other sites

Promises are almost available everywhere natively, and they would cover your use-case. I normally just polyfill and follow the spec, using the babel polyfill usually, although that is largely just core-js polyfills.

In the past I've used Q and Async (going back a long time now!) but both provide much more control than I ever need.

There's always async/await if you're feeling particularly brave :) I use it fairly extensively now (using babel to transpile) and its lovely

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...