Changing states

I am making a gaming engine (of sorts) I decided to implement a system where I can add "States" so I have a class "State":
State.h
1
2
3
4
5
6
7
8
9
class State
{
public:
	State()
	{
	}
	virtual void Load();
	virtual void Run();
};

State.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "Run.h"

void State::Load()
{

}

void State::Run()
{

}


And I have a class that inherets from this class:

Game.h
1
2
3
4
5
6
7
8
9
class Bob : public State
{
public:
	Bob()
	{
	}
	void Load();
	void Run();
};


Game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Run.h"



void Bob::Load()
{
		engine->GetSprite().push_back(Sprite());
		engine->GetSprite()[0].LoadTexture("ball.bmp", D3DCOLOR_XRGB(255, 255, 255));

}

void Bob::Run()
{

}




void Run::EngineLoad()
{
	engine->AddState(Bob());
}


Now what I want to happen is to call the functions in class Bob but for some reason it just won't any idea.... Here's the code to do that (in a nutshell)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Run::AddState(State newState)
{
	states.push_back(newState);
}


void Run::ChangeState(unsigned int id)
{
	if(states.size() < id)
	{
		return;
	}
	mCurrentState = id;
	states[id].Load();
}

//this is in a loop
if(states.size() > 0)
{
 states[mCurrentState].Run();
}
// 

states is a vector of the states, thanks for any help anyone can give
Last edited on
You need to work with pointers ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <vector>
using namespace std;

struct Fruit
{
    virtual void Speak() {}
};

struct Apple : public Fruit
{
    void Speak()
    {
        cout << "Hi! I'm an apple!\n" << endl;
    }
};

struct Pear : public Fruit
{
    void Speak()
    {
        cout << "Hi! I'm a pear!\n" << endl;
    }
};

struct AnnoyingOrange : public Fruit
{
    void Speak()
    {
        cout << "( http://www.youtube.com/user/realannoyingorange )\n";
        cout << "Hey Cabbage, know why you're so smart?\n";
        cout << "'Cause you have a big head. :D\n" << endl;
    }
};

template <class T>
void cleanup(vector<T*> v)
{
    for (unsigned i=0; i<v.size(); i++)
        delete v[i];
}

int main()
{
    vector<Fruit*> fruits;

    fruits.push_back(new Apple);
    fruits.push_back(new Pear);
    fruits.push_back(new AnnoyingOrange);

    fruits[0]->Speak();
    fruits[1]->Speak();
    fruits[2]->Speak();

    cleanup(fruits);

    cout << "hit enter to quit..." << endl;
    cin.get();
    return 0;
}
Last edited on
Topic archived. No new replies allowed.