View Single Post
Old Jan 25th, 2007, 4:10 AM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by MR.T View Post
Also, should I put my variables that are used by multiple function, inside a class that they are at least somewhat related to, so I can use there scope to change them, instead of making them global? Would that be more efficient?
It's not about being efficient, per se. It's more about flexibility. You can only have one set of global variables, but you can potentially have more than one object of a certain type.

For instance, say I was designing a single player game, and created certain global variables for health, player position, etc. This would work okay, but then what if you wanted to add an extra player? If all your variables were in a Player class, it would simply be a case of instantiating a new Player.

Or say you had a global variable holding a database connection. What happens if your database is then divided up into two for efficiency reasons? Again, having a object you pass around is more flexible than a single global variable.

Generally speaking, global variables should be used as little as possible, as they're more inflexible than variables on objects. It also helps organise your program more cleanly.

Other than that, Game_Ender is right on the money. And don't worry about efficiency of function calls - if you get to the point where it matters, you probably should be farming out some functionality to a binary library in C or something anyway.
Arevos is offline   Reply With Quote