messing up the functions from inherited classes from two different base classes

Started by
13 comments, last by Heelp 8 years, 2 months ago


But I prefer globals because the code seems clearer when the functions take no arguments.

This is something you should unlearn as quickly as possible.

While I can see that feeling cleaner, consider the fact that anything, anywhere, at any time, can access and modify the variables.

As the project you're working on starts to grow, this will become a problem. Suddenly, the variable isn't what you expect it to be, and finding out why is extremely difficulty and time-consuming.

On the other hand, if you pass the variables you need into a function, you can follow it a lot easier. You won't pass lots of parameters that aren't used (because it's tedious and boring to pass everything all the time), so you'll end up with functions of code, for the most part, only take the parameters they actually need.

Globals are bad.

There are some exceptions (e.g. globals that are read-only aren't as bad), but for the most part you should stay away from them unless you have very very good reasons not to.

Hello to all my stalkers.

Advertisement
I usually avoid offering advice when discussing globals, because it's much more effective for the person to discover just how bad it can get in their own codebase. It makes a bigger impression to see the problem first-hand than to trust people's warnings that you don't fully understand yet.

Nypyren, normally, you are right, but it depends on the person. Me, I don't like reinventing the wheel, you guys seem like you suffered a lot because of globals + my lecturer also told me that globals are bad, so I prefer to listen, and I removed them. Some things I prefer not to fully understand laugh.png

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.

Yeah, I got it. Just make the functions take arguments instead of using globals, less worries. Thanks to all for the help, really. I'm really productive when I don't spend a whole week figuring out something stupid on my own. Thanks again :)) wub.png

This topic is closed to new replies.

Advertisement