Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: State Machine to handle player actions?  (Read 2006 times)

0 Members and 1 Guest are viewing this topic.

Veritas

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
    • Email
State Machine to handle player actions?
« on: April 25, 2014, 12:55:45 pm »
So far I've been making some small example games using a command system to handle input. Depending on the input a command simply gets sent to the target and changes it's attributes. Now I was wondering how I could handle cases where the input behaves differently in different contexts. For example the arrow keys could make the character run after a certain time of being pressed, different action buttons could be chained to make combos etc. You can't handle that directly using commands. So I thought that a hierarchical state machine would work pretty well in this case. I am not sure what the responsibility of commands would be though. Should they be used simply to create a layer of indirection and give all the responsibility of handling the input to the state manager or should they still retain some elements of control on the player's actions?
"Lasciate ogni speranza, voi ch'entrate"

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: State Machine to handle player actions?
« Reply #1 on: April 25, 2014, 01:32:46 pm »
Personally I'd probably go with an actual message system, where there can be x number of listeners, so if you for example press an arrow key, not only would your player receive the key press, but also your UI, etc. Each can then handle how they want to react to these messages (see Tank's excellent article).

Getting back the issue at hand, this would be rather easily solvable with some special listener class(es). As an example you'd have a combo of ABBAB, no the class would listen to the A and B key press events. Then it could save the inputs and use a clock to make sure the inputs are "fresh". If it detects that ABBAB was pressed within a certain amount of time, it could issue the special combo message, which would be received by the player, etc.
The same thing works easily with holding some keys.

How exactly you'd shape the classes is also important, but depends on your needs and exact use case.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Veritas

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
    • Email
Re: State Machine to handle player actions?
« Reply #2 on: April 25, 2014, 02:09:26 pm »
What I had in mind was having actions dependant on state. Say that I press the up button. I want our hero to handle it differently depending on whether he is in water, he is walking or riding. I may want him to to do a ground slam when jumping if I press the attack button. That's what I am trying to address and I am not sure if a state machine is a good choice for this and if it is whether it should take full responsibility of the hero's action by listening to input or if I should have another object that listens for input and sets the states on the state manager.

Great article by the way, I am definitely going to look some more into it.
"Lasciate ogni speranza, voi ch'entrate"

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: State Machine to handle player actions?
« Reply #3 on: April 25, 2014, 02:23:26 pm »
I guess there are many approaches to this, the most simple thing that comes to my mind and that I've been using in the past, is simply having a state variable in the player class and react to the input differently depending on the state. If something else than the input can change the states, you might want to add some access function or use a command.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Veritas

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
    • Email
Re: State Machine to handle player actions?
« Reply #4 on: April 25, 2014, 02:45:17 pm »
Yea the state manager I have in mind is simply a state hierarchy. Each substate holds its own variables to handle time specific events for example. So you have a pointer to a BaseState called state :p. The listener responsible for handling the input calls the state's handleInput virtual function. That handles the input passed and sets the state variable to the necessary substate. All substates have this function and if they don't handle the input passed they go back one hierarchy level. This way you don't have to deal with a huge amount of state variables.
"Lasciate ogni speranza, voi ch'entrate"