Issue with pointers in a state machine

Hello All!

I am currently trying to make a state machine based on the one on http://lazyfoo.net/articles/article06/index.php and I would like some help.

One of the functions in the Gamestate class is the changeState function, which is used to switch between the current states. It's only parameter is a Gamestate pointer, which is created in the main and then modified within the function. However I am getting an error where even though the code runs and the state is switched, when it leaves the function the state is reverted. How can I solve it?

The code is below n.n
Thanks in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Gamestate::changeState(Gamestate *state){
	if (nextState != currentState){ // if you need to switch state...
		switch (nextState){ // switch to the relevant state
		case STATE_MAIN_MENU:
			state = new Main_Menu();
			break;

		case STATE_BATTLE:
			state = new Battle();
			break;
		}

		currentState = nextState; // change the current state val
	}
}
Note that the state pointer is passed by value to the function so the state variable inside the changeState function is a copy of the variable that you passed from main. Changing the copy will not modify the original pointer in main. Maybe you want to pass the pointer by reference?
Topic archived. No new replies allowed.