I DID IT. I just made 2 global variables: GameState *currentState and Character *currentChar. I went to the main() function and wrote this: currentState = new Intro(); and currentChar = new Ryu(); and I made a change_state function that changes screens between classes Intro, Menu and Stage1. So when I am in Intro, when I press enter I go to Menu and if I press enter again, I go to Stage1, and the opposite works with Esc.
and I went to the Stage1::logic() and there I put currentChar->logic(). and then I went to the Stage1::handle_events and I put the currentChar->handle_events() and I did the same with the render() function. I was just confused because my professor told me not to use global variables, and I thought that I can do without them, but maybe he was telling this, because I was just starting with programming last year, so everything is fine now. Apparently WoopsASword is right - you can't do OOP without some globals haha. Lost 2 days for this simple thing
This is not a good solution. I'm sorry but you have to roll it back. (If you dont work with git yet, start to).
I've never stated you need a global, you do need an instance but definetly not a global.
I'll explain how your code should look like (This will be long then read carefully when you are not tired):
- Main entry point begins:
Usually you initialize your first instances.
for example the game class,
load the first level or the menu class.
-Then the rest of the game, usualyl a game has a game loop which executes Draw and Update around 60 times per second.
The real magic happens when you change the state of the game, for example a user chose to play a map.
Only then you should create your instance for that map and pass it to the relevant class which will hold the map and draw/update it.
To explain better what you should do and how to create/manage instance I'll show you some work of mine:
I've created a class called "Window" which inherits from "IWindow".
The window has some render which does the actual render and I register a render by calling SetRender.
This how it looks:
class IWindow
{
protected:
/* ctors + dtor*/
public:
// Sets the current render to the choosen render
void SetRenderer(IRender* render);
/* more fucntions here*/
};
then I create a new Window in the main class and use this method to set the actual render:
window->SetRenderer(new ShaderRender(mesh, window));
* Mesh is another object of the type Mesh and window is my window... ShaderRender inherits from IRender.
This way, I know when I initialized my instance + it is not a global.
------------------------------------
More C++ related topic, Use the std::shard_point and std::unique_pointer instead of raw pointers.
Raw pointers are good for managing pointers which are not the source of your memory but just a reference to it.