Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /mnt/web022/e3/96/54323296/htdocs/wp-includes/formatting.php on line 4477 Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /mnt/web022/e3/96/54323296/htdocs/wp-includes/formatting.php on line 4477
|

State Machine

septiembre  2014 / 16 No Comments

I want to approach this article on a practical way, if you want to know a little more theory, I found an article on wikipedia that could possibly be of your interest.

Read Finite state machine on Wikipedia

If you’re already bored of wikipedia here is the rest of the article:

A good practice in video game programming is organize the code in what is known as state machines, we can separate the logic into small pieces, by this way we achieve flexibility, organization and adaptability in the code of a game.

 

Practice example

Do you want to create an enemy with the following behaviours:

  1. The enemy must make a guard around the scene
  2. There are some points in the scene where the enemy stops and checks that there aren’t outsiders
  3. If he find an intruder, goes to attack

We can see with these 3 points we can start to plane the programming of the enemy, we will begin to think them in «actions» to program our state machine.

For example, point 1, we can deduce that an action is MOVING
STOP (IDLE) and LOOKING and are two actions that can accept point 2
ATTACK, is another action that we can take the point 3

With the «actions» of the previous paragraph, we can start building our state machine in pseudocode C ++

 

enum EStates
{
  IDLE,
  MOVING,
  LOOKING, 
  ATTACK,
};

 

Create and initialize a new variable for the states

 

EStates currentState = EStates::IDLE;

 

We create an «Update» method for each state, on this methods there are the specific operations for each state

 

// IDLE
void UpdateIdle(float elapsed)
{
  // Do something
}

// MOVING
void UpdateMoving(float elapsed)
{
  // Do something
}

// LOOKING
void UpdateLooking(float elapsed)
{
  // Do something
}

// ATACK,
void UpdateAtack(float elapsed)
{
  // Do something
}

 

We also need a method that allows us to change state

 

void ChanteState(EState theState)
{
  currentState = theState;
}

Now that we are already able to change states, would see as the main game loop.

 

// Main
void Update(float elapsed)
{
  switch(currentState)
  {
	case EStates.IDLE:
	  UpdateIdle(elapsed);
	  break;

	case EStates.MOVING:
	  UpdateMoving(elapsed);
	  break;

	case EStates.LOOKING:
	  UpdateLooking(elapsed);
	  break;

	case EStates.ATTACK:
	  UpdateAttack(elapsed);
	  break;
  }
}

This would be the basics of a state machine, I recommend you try it and you’ll see how quickly you questions and needs arise, to go a point beyond, we can add a method to our states start and an end state, if IDLE would thus

 

void UpdateIdle(float elapsed)
{
  // Do something
}

void IdleInit(void params)
{
  // Init Idle
}

void IdleEnd(void params)
{
  // End Idle
}

State machines can be applied to any programming language, so far I have explained the basics and pseudocode, then I leave the game code I’m doing along with 10 comrades as draft Master Thesis Inanis and Horo you can visit the website and get updates http://www.inanisandhoro.com

 

In the game we have some spikes up and down after a while, then I leave the code the state machine I programmed, the code is in lua, you will see that there are certain functions in or out of states without code, it is still highly recommended them, do not know at what point a designer to ask a new feature will come.

 

pinchosinanis

 

It is often read from the browser’s complicated, so here’s the code to download it

SpikesTimer.lua

 

Thanks for readme!



Warning: Undefined variable $fields in /mnt/web022/e3/96/54323296/htdocs/wp-content/themes/profession/profession/comments.php on line 16

Leave a reply

Your email address will not be published. Website Field Is Optional