Question

The way I want to manage input for my game is to poll a TChan, and then create an Event when an eTick happens. But will the way I'm trying it work?

data UAC = UAC (AID,PlayerCommand) deriving Show

makeNetworkDescription :: forall t . Frameworks t =>
                      TChan UAC ->
                      AddHandler () ->
                      TChan GameState ->
                      Moment t ()
makeNetworkDescription commandChannel tickHandler gsChannel = do
   eTick <- fromAddHandler tickHandler
   bCChannel <- fromPoll $ grabCommands commandChannel
   let eCChannel = bCChannel <@ eTick
...
   reactimate ...



grabCommands :: TChan UAC -> IO [UAC]
grabCommands unval = do
  (atomically $ readTChan unval) `untilM` (atomically $ isEmptyTChan unval)

from the documentation for fromPoll "Input, obtain a Behavior by frequently polling mutable data, like the current time. The resulting Behavior will be updated on whenever the event network processes an input event."

Am I understanding this correctly? The TChan is being populated from other code and then every eTick I empty it and get another Event t [UAC]?

Maybe my understanding is wrong, or this computation is too expensive for fromPoll. In that case what's a better direction to go in?

Was it helpful?

Solution

I was facing the same question in my game. I decided to try to keep the event network as free as possible from implementation-specific stuff (such as input protocols). Instead I block in a IO thread outside of the event network and send processed events to the event network from there (using something like the EventSource "design pattern" used in the reactive-banana examples.

The reason for this is that this way the event network has to process only well-defined and simple input commands and fromPoll is not needed. The particulars (such as if the input is coming from local input or a network, if the input events are well-formed, how errors are handled) are done in other parts of the program.

On the other hand, if the design of your game is such that input is handled only during the game ticks and input events must be buffered, then you will need some place to hold them. A TChan will do the trick as well as any other way, I suppose.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top