I am developing a game where the game logic / state itself is managed by an Akka FSM actor. One of my states is WaitingForAnswer
and the business logic related to this state is that a player (also an actor) must answer within 20 seconds, otherwise he/she loses a point. So I defined the state like this:
when(WaitingForAnswer, 20 seconds) {
// event handling logic here
}
So far so good. Now what I want to do is to have some kind of reminder - after half of the state timeout has elapsed (after 10 seconds), I want to send a reminder to the player, that he has to answer soon.
My ideas:
Using
setTimer(name, msg, interval, repeat)
when entering theWaitingForAnswer
state by setting theinterval
to 10 seconds. This way my FSM actor will get a reminder, reminding him of reminding the player actor. As you can see this is a bit ... well not cool.Use the scheduler and once my FSM actor enters the
WaitingForAnswer
state, schedule a message for 10 seconds in the future with the player actor as a recipient. Of course in this case I would have to make sure that I cancel the scheduled message when the player actor responds in time.
Is there any other more natural way to achieve this? Some kind of hook? Some way to use setTimer
with a different recipient?
The way you explained your problem, it seems that your state WaitingForAnswer is actually 2 different states that behave quite similarly. You could define the state timeout to 10 seconds, if the player answers, logic applies, otherwise, you do a state transition to WaitingForAnswerAfterReminder with again 10 seconds state timeout which is handled by the same business logic as the previous state.
To me it looks that you try to model two states within WaitingForAnswer: before and after the reminder.