Guess I'll have to rethink most of my coding. Hopefully this will give me less headaches :o Thanks for the help
Printable View
Guess I'll have to rethink most of my coding. Hopefully this will give me less headaches :o Thanks for the help
No one said C++ design was easy ;)
Good luck!
Well I didn't think I should start a new thread since this thread caused all the problems, but I am having trouble figuring out how to manage the vector.
If I create a separate class called Engine or something that basically holds the vector then runs the state thats on top of the vector I have no way of telling Engine to push back a new class. (Maybe if I use friend that way it might be able to access it or static? Not sure which one is better).
Also isn't it a bad thing if I create a class for only one object?
Basically I am clueless on how to update the vector since my objects function(MENU_State.update etc.) are the ones that tell me what I should add/pop. Any hints or suggestions?
You wouldn't pass the contents of the vector around; you'd pass the Engine object around (by reference, probably). Each update or whatever can read the top one; if it needs to push, it's got the Engine right there ready.
Edit: And I think the point of the Engine class was that it would be "the state of play": it would contain (a vector of) players, and (a vector of) game states, and (a vector of) inventory, and (a vector of) enemies, or whatever you have in your game there.
But if I pass around the Engine class by reference wouldn't that defeat the purpose of not passing around the vector by reference?
The idea behind not passing the vector by reference was not that it couldn't be done, just that it wasn't (or might not be, I mean it's your game) a good design choice.
Edit: I mean, passing a vector by reference:
Rather contrived, but you get the idea.Code:void process_vector(std::vector<int> &bunch_of_ints) {
int total = 0;
while (total < 100 && !(bunch_of_ints.empty()) {
total += bunch_of_ints.back();
bunch_of_ints.pop_back();
}
}
So should I do it like this?
Main
EngineCode:Engine Engine
...
Loop
Message pump
Engine.run();
....
StatesCode:public:
vector states=new state;
run(){
States.back()->run(&this or &Engine (Not sure which));
}
Something like that?Code:if[key=pressed]{
Engine.vector.pushback new state
}
Without knowing what you're doing in run() I can't be very specific. But I wouldn't see why Engine.run would need to call a different run function. You're already running, just do the thing already.
Well the run function for my states runs the update function then the render function in order. I guess I could run it from the engine and save some memory and then I only have to pass the engine object once because update only needs it.
btw instead of passing a reference each time update is called shouldn't I just have a reference object(Not sure if I could do this) in my state class and on creation I just refrence the object(thus reducing the overhead for function calls).
So this the revised pseudo code
Main
EngineCode:Engine Engine
...
Loop
Message pump
Engine.run();
....
StatesCode:public:
vector states=new state;
run(){
States.back()->update(&this or &engine [not sure which is correct yet]);
//Might not even need to pass it if I could just put it in the state.
States.back()->render();
}
Is that better?Code:if[key=pressed]{
Engine.vector.pushback new state(&this or &engine [not sure which is correct yet]);
//This way the new state would have access to the engine without needing to constantly get a new reference to it.
}