Spanish



  Home

  Features

  API

  License

  Contact



  Download


 

StateCore v0.5 (Beta)

StateCore is an generic finite state machine (FSM), based on the state pattern and written in C++. StateCore was created with the idea to helping the developers. This was created under the LGPL license. This means that it is completely free to use for commercial and non commercial applications.

You can use this to apply states to:

  • applications, ... (networking, videogames, ...)
  • objects, guns, characters, enemys, ... (cinematics, videogames, ...)
  • neural nets, pathfinding, A*, artificial intelligence, ... (AI)

Example using the implementation class

This code snippit is a short example that show how create a state machine using the implementation class.

// We are including the main header file it that contains
// the implementation class.

#include <ceStateCoreImpl.h>
#include <cstdio> // For printf.

// We are using the namespace "ce" (CorEngine) by default (Optional).
using namespace ce;


// We are creating our own states class, inheriting the iterface class
// for states.
class CGlobalState : public IState

{
  public:

    // This will execute when the state is entered.
    inline void OnEnter(const IStateManager *Owner)
    { 
std::printf("\n + Entering in the > Global state \n"); };

    // This is called by the owner's update function each update-step.
    inline void OnExecute(const IStateManager *Owner)
    { std::printf(" > This is the > Global state \n"); };

    // This will execute when the state is exited.
    inline void OnExit(const IStateManager *Owner)
    { std::printf(" - Leaving the > Global state \n"); };

} GlobalState;

class CGamePlayState : public IState

{
  public:

    inline void OnEnter(const IStateManager *Owner)
    { std::printf("\n + Entering in the GamePlay state \n"); };

    inline void OnExecute(const IStateManager *Owner)
    { std::printf(" > This is the GamePlay state \n"); };

    inline void OnExit(const IStateManager *Owner)
    { std::printf(" - Leaving the GamePlay state \n"); };

GamePlayState;

class CMenuState : public IState

{
  public:

    inline void OnEnter(const IStateManager *Owner)
    { std::printf("\n + Entering in the Menu state \n"); };

    inline void OnExecute(const IStateManager *Owner)
    {
      std::printf(" > This is the Menu state \n");
      // Change to a new current state.
      Owner -> ChangeState(
&GamePlayState);
    };

    inline void OnExit(const IStateManager *Owner)
    { std::printf(" - Leaving the Menu state \n"); };

MenuState;

class CIntroState : public IState
{
  public:

    inline void OnEnter(const IStateManager *Owner)
    { std::printf("\n + Entering in the Intro state \n"); };

    inline void OnExecute(const IStateManager *Owner)
    {
      std::printf(" > This is the Intro state \n");
      Owner -> ChangeState(&
MenuState);
    };

    inline void OnExit(const IStateManager *Owner)
    { std::printf(" - Leaving the Intro state \n"); };

IntroState;

// We are creating our own manager class, inheriting the implementation class.
class CStateManager : public IStateManagerImpl<
CStateManager>
{
  public:

    inline void Example(void) const
    {
      // Set a global state that will be called every time the FSM is updated.
      this -> State -> SetGlobalState(&GlobalState);
      // Set a current state that will be called every time the FSM is updated.
      this -> State -> SetCurrentState(&IntroState);

      // This function will trigger update on the global state the current state
      this -> State -> Update();
     
this -> State -> Update();
      this -> State -> Update();
    };

    // Operator -> (Optional).
    const
CStateManager* operator -> (void) const
    { return this; };

};


int main(int argc, char **argv)
{

  // We are creating a instance of our manager class.
  const 
CStateManager State;

  //
Verify the integrity of the manager class (Optional).
  if (
State -> VerifyStateManager() == 0) return 1;

  State -> Example();

  return 0;

}

Example using the interface class

This code snippit is a short example that show how create a state machine using the interface class.

// We are including the main header file it that contains the interface class.
#include <ceStateCore.h>
#include <cstdio> // For printf.

// We are using the namespace "ce" (CorEngine) by default (Optional).
using namespace ce;


// We are creating our own states class, inheriting the iterface class
// for states.
class CGlobalState : public IState

{
  public:

    // This will execute when the state is entered.
    inline void OnEnter(const IStateManager *Owner)
    { 
std::printf("\n + Entering in the > Global state \n"); };

    // This is called by the owner's update function each update-step.
    inline void OnExecute(const IStateManager *Owner)
    { std::printf(" > This is the > Global state \n"); };

    // This will execute when the state is exited.
    inline void OnExit(const IStateManager *Owner)
    { std::printf(" - Leaving the > Global state \n"); };

} GlobalState;

class CGamePlayState : public IState

{
  public:

    inline void OnEnter(const IStateManager *Owner)
    { std::printf("\n + Entering in the GamePlay state \n"); };

    inline void OnExecute(const IStateManager *Owner)
    { std::printf(" > This is the GamePlay state \n"); };

    inline void OnExit(const IStateManager *Owner)
    { std::printf(" - Leaving the GamePlay state \n"); };

GamePlayState;

class CMenuState : public IState

{
  public:

    inline void OnEnter(const IStateManager *Owner)
    { std::printf("\n + Entering in the Menu state \n"); };

    inline void OnExecute(const IStateManager *Owner)
    {
      std::printf(" > This is the Menu state \n");
      // Change to a new current state.
      Owner -> ChangeState(
&GamePlayState);
    };

    inline void OnExit(const IStateManager *Owner)
    { std::printf(" - Leaving the Menu state \n"); };

MenuState;

class CIntroState : public IState
{
  public:

    inline void OnEnter(const IStateManager *Owner)
    { std::printf("\n + Entering in the Intro state \n"); };

    inline void OnExecute(const IStateManager *Owner)
    {
      std::printf(" > This is the Intro state \n");
      Owner -> ChangeState(&
MenuState);
    };

    inline void OnExit(const IStateManager *Owner)
    { std::printf(" - Leaving the Intro state \n"); };

IntroState;

int main(int argc, char **argv)
{

  // We are creating a instance of the manager class.
  
IStateManager *State = CreateStateManager();
  if (State == NULL) return 1;

 
// Set a global state that will be called every time the FSM is updated.
  State -> SetGlobalState(&GlobalState);
 
// Set a current state that will be called every time the FSM is updated.
  State -> SetCurrentState(&IntroState);

 
// This function will trigger update on the global state the current state.
  State -> Update();
  State -> Update();
  State -> Update();

 
// Release the instance of the class manager.
  DestroyStateManager(&State);
  return 0;

}

So here you know the basics for their use, for more information, I recommend reading the API documentation. For support or with any questions or suggestions, feel free to contact me.

Copyleft (cc) 2010 by FX Programmer. Some Rights Reserved.