Hey guys, I have variables that are used pretty much everywhere in my game. At the minute I have a class to hold all these variables and then i pass the object through the parameters of each function. Is this the way to do it or is there a better way?
That's hard to say without seeing without seeing the declaration for your class.
If I had to guess, I would guess that all your variables are public. That's a poor idea. It does not provide protection (encapsulation) for your variables.
The other question to ask yourself is does every variable that is a member of your Game class truly an attribute of the game? If it is, the question is "Does Game HAVE A xxxx" should make sense. For example, if a player has health and hit points, those should be attributes of a player class and not members of your Game class. Note that a Game class can have Players as members.
These are the variables I have inside this class so far. There will be more, there was in the last game, but I am only just setting up the basics of this program. So will this then be okay?
Questions:
-- The phrasing in your question implies that there will only ever be one instance of Variables. Is this correct? This is contradicted by your code and has important implications.
-- Why does everything in your game need to know the state of the game?
-- Why does everything in your game need to know the size of the window, and the maximum frame-rate?
-- Every instance of Variables represents a bunch of variables....? What's the point of the class?
Comments, in no particular order:
-- Your intent may be different than what's implied by the definition of this class.
-- There is very little advantage in trivial accessors and mutators. It may be prudent to keep them anyways because of the high cost of an implementation change given that this class is coupled to everything. This indicates bad design.
-- gameRunning and gamePaused are apparently duplicate because gameRunning is presumably always !gamePaused.
-- If there is more than one possible game state, consider using an enumeration
-- Global state should be avoided if possible.
-- If you pass a single stateful object to everything in your program, it's global state.
-- Constants are not stateful and should probably be marked staticconstexpr.
-- If there really is a need to share some information with everything in your game, make it global. (The guideline is "avoid", not "never use".) Consider placing it in its own namespace.
-- Don't feel obliged to put everything in a class. Universal object orientation is probably not the best approach to a solution.
-- You've made a typo on line 14 ("pasued").
-- Your accessors should be const member functions.
Feel free to ask about the rationale behind any of these comments.
I have changed the way I have done it now, I decided to make it so the game.h file includes "gameRunning" and "gamePaused" and then I have a separate class for the windowInfo which includes the info I require to make and change the window.
Game running and Game paused are different because the game can be running and paused at the same time. Paused stops all the update and runs a different event handler loop. Then if the game is not running, the program will shut down. So I do require both of them :)