Okay, I am making a finite state machine for a lab. I have here a 2 files with the code for the FSM. I know it isn't finished yet, I know what needs to be put in. The only things I would need help on are the errors that I get.
#ifndef _WARRIOR_
#define _WARRIOR_
#include "State.h"
class Warrior
{
private:
fsm_state* m_currentState;
private:
void Update()
{
m_currentState->Execute(this);
}
void ChangeState(fsm_state *newState)<-//Here I had a const but when I added the m_currentState = newState it said that the value type cannot be assigned an entity type "fsm_state"
{
delete m_currentState;
m_currentState = newState;
}
public:<-//Keeps saying I need a declaration
bool EnemyFOV()
{
int loc = rand()%20;
if(loc >= 50)
{
returntrue;
}
else
{
returnfalse;
}
}
};
#endif
#ifndef _STATEPATTERN_
#define _STATEPATTERN_
#include <iostream>
#include <time.h>
#include "Warrior.h"
class fsm_state
{
public:
virtualvoid Execute(Warrior * Warmaster = 0);
};
class Patrol_state : public fsm_state
{
virtualvoid Execute(Warrior * Warmaster) = 0
{
if(Warmaster->EnemyFOV())
{
Warmaster->ChangeState(new Attack_State());<-//the ChangeState gives me an error that it's inaccessible. Also the Attack_State() gives me an error of "object of abstract class type "Attack_State" is not allowed"
}
else
{
Warmaster->Move();
}
}
};
class Attack_State : public fsm_state
{
virtualvoid Execute(Warrior *Warmaster) = 0
{
if(Warmaster->EnemyFOV())
{
Warmaster->ChangeState(new Death_State());
}
elseif(Warmaster->EnemyFOV())
{
Warmaster->ChangeState(new Patrol_State());
}
else
{
Warmaster->Attack();
}
};
#endif
If anyone can help me fix there errors because I've tried to figure out what it could be. Also, if anyone can give me an alternate way of doing this FSM. This will be greatly appreciated.
ChangeState is inaccessible because it isn't inherited - the access modifier is private in the base class. It needs to be at least protected for the subclass to inherit it.
I made a FSM at the end of last year. I don't have the code to hand, but I created a StateMachine object that contained pointers to the previous and current state. In each iteration of the loop, it would run the current state's Update function.
The state was an abstract base class with Enter, Update and Exit functions. This allows you to apply "one-off" settings when you enter and leave states. It gives a bit more flexibility.
I don't have the code to hand for this, I'm afraid. I can tell you that Matt Buckland's "Programming Game AI by Example" was an excellent resource.