• Create Account

### #Actualparinho7

Posted 01 January 2013 - 11:07 PM

I understand the problem now, thank you.
The call to the function was this:

CStateManager::change_state(this, nextState);

How can I modify it so that "this" is passed by reference? (If it's possible at all)

Ah, I see. No, it is illegal to modify "this", at least in C++. This means your design has failed, and you need to rethink it. Why do you need different classes for State_Main_Menu, State_Exit, etc...? Is an enum flag not enough? What are you trying to do, exactly? It looks like you are trying to implement a state machine for your GUI, but this is clearly not the right approach here, as the language is very clearly telling you "don't do this".

It's my first try to use a finite state machine in my game (the point of the whole game was to learn how to write a FSM), to navigate through the menus. Apparently I need to find some other way to do this. But as I say to my original post I did almost an exact console application that worked this way.

Here is the code

void CStateManager::change_state(GameStates *cur_state, int next_state){

if (next_state != STATE_NULL)
delete cur_state;

switch (next_state){
case STATE_MAIN_MENU: cur_state = new State_Main_Menu(); break;
case STATE_PLAYING: cur_state = new State_Playing(); break;
case STATE_EXIT: cur_state = new State_Exit(); break;
}
}

nextState = STATE_NULL;
}

cin >> nextState;
}

CStateManager::change_state(this, nextState);
}

cout << "MAIN MENU" << endl;
cout << "Press '3' to play"  << endl;
cout << "Press '0' to continue in Main Menu"  << endl;
//	system("cls");
}

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

GameStates *myState = new State_Main_Menu();

while (myState->Get_nextState() != STATE_EXIT){

myState->Event();
myState->Update();
myState->Render();
}

return 0;
}
and it worked as intended, never get an exception by that code. All I was doing was somewhat copying that and adding the SDL stuff!

### #2parinho7

Posted 01 January 2013 - 11:06 PM

I understand the problem now, thank you.
The call to the function was this:

CStateManager::change_state(this, nextState);

How can I modify it so that "this" is passed by reference? (If it's possible at all)

Ah, I see. No, it is illegal to modify "this", at least in C++. This means your design has failed, and you need to rethink it. Why do you need different classes for State_Main_Menu, State_Exit, etc...? Is an enum flag not enough? What are you trying to do, exactly? It looks like you are trying to implement a state machine for your GUI, but this is clearly not the right approach here, as the language is very clearly telling you "don't do this".
It's my first try to use a finite state machine in my game (the point of the whole game was to learn how to write a FSM), to navigate through the menus. Apparently I need to find some other way to do this. But as I say to my original post I did almost an exact console application that worked this way.

Here is the code
void CStateManager::change_state(GameStates *cur_state, int next_state){	if (next_state != STATE_NULL)		delete cur_state;	switch (next_state){		case STATE_MAIN_MENU: cur_state = new State_Main_Menu(); break;		case STATE_PLAYING: cur_state = new State_Playing(); break;		case STATE_EXIT: cur_state = new State_Exit(); break;	}}State_Main_Menu::State_Main_Menu(){	stateID = STATE_MAIN_MENU;	nextState = STATE_NULL;}void State_Main_Menu::Event(){	cin >> nextState;}void State_Main_Menu::Update(){	CStateManager::change_state(this, nextState);}void State_Main_Menu::Render(){	cout << "MAIN MENU" << endl;	cout << "Press '3' to play"  << endl;	cout << "Press '0' to continue in Main Menu"  << endl;//	system("cls");}int main(int argc, char* argv[]){		GameStates *myState = new State_Main_Menu();	while (myState->Get_nextState() != STATE_EXIT){				myState->Event();		myState->Update();		myState->Render();	}	return 0;}
and it worked as intended, never get an exception by that code. All I was doing was somewhat copying that and adding the SDL stuff!

### #1parinho7

Posted 01 January 2013 - 11:04 PM

I understand the problem now, thank you.
The call to the function was this:

CStateManager::change_state(this, nextState);

How can I modify it so that "this" is passed by reference? (If it's possible at all)

Ah, I see. No, it is illegal to modify "this", at least in C++. This means your design has failed, and you need to rethink it. Why do you need different classes for State_Main_Menu, State_Exit, etc...? Is an enum flag not enough? What are you trying to do, exactly? It looks like you are trying to implement a state machine for your GUI, but this is clearly not the right approach here, as the language is very clearly telling you "don't do this".

It's my first try to use a finite state machine in my game (the point of the hole game was to learn how to write a FSM), to navigate through the menus. Apparently I need to find some other way to do this. But as I say to my original post I did almost an exact console application that worked this way.

Here is the code

void CStateManager::change_state(GameStates *cur_state, int next_state){

if (next_state != STATE_NULL)
delete cur_state;

switch (next_state){
case STATE_MAIN_MENU: cur_state = new State_Main_Menu(); break;
case STATE_PLAYING: cur_state = new State_Playing(); break;
case STATE_EXIT: cur_state = new State_Exit(); break;
}
}

nextState = STATE_NULL;
}

cin >> nextState;
}

CStateManager::change_state(this, nextState);
}

cout << "MAIN MENU" << endl;
cout << "Press '3' to play"  << endl;
cout << "Press '0' to continue in Main Menu"  << endl;
//	system("cls");
}

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

GameStates *myState = new State_Main_Menu();

while (myState->Get_nextState() != STATE_EXIT){

myState->Event();
myState->Update();
myState->Render();
}

return 0;
}

and it worked as intended, never get an exception by that code. All I was doing was somewhat copying that and adding the SDL stuff!

PARTNERS