Data Structures and refrences!

Started by
15 comments, last by Zahlman 15 years, 9 months ago
I have almost completed the basic setup of my game but I have one simple question. I am currently trying to eliminate all my global variables, and for now i am using references to change them. Heres my basic set up.

while(!done){
gameloop(variables go here);
}

gameloop(typedef &variables){
   switch{gamestate){
      case menu: logicmenu(certain variables)
                 rendermenu(certain variables);
                 break;
   }
}

So wouldn't it be better just to put all my variables into one struct and then pass that? My other idea was to create a couple of structs instead of one giant one like put all the x,ys in one struct and maybe logic stuff in an other one. So which way should I go about using variables?
Advertisement
Can you give me some more info about what's exactly in the structs, and what the structs are meant to represent?

For example, the struct with the coordinates, is that a character struct that also contains hp, etc.?

But yeah, if you have a lot of certain types of variables you want to pass, you should pass it as a struct.

[Edited by - Moonshoe on July 7, 2008 6:22:30 PM]
Here are most of the variables that most of my functions will need

BOOL	done=FALSE;		// Bool Variable To Exit LoopGameState_E Gamestate=MENU;int selector=0, max_selector;GLubyte Ycolor = 255;GLfloat Width, Height;GLfloat	roll;			// Rolling Texturebool colordown=true;GLfloat yOffset=0;bool change=false;


some of these variables are for specific functions only... And I am planning on having a character class instead of a struct. The only thing is that I don't want to keep having to add to my function certain variables. so instead of
while(!done){gameloop(variables go here);}gameloop(typedef &variables){   switch{gamestate){      case menu: logicmenu(certain variables)                 rendermenu(certain variables);                 break;   }}

It would be like
struct GameVariables;while(!done){gameloop(GameVariables);}gameloop(struct &GameVariables){   switch{gamestate){      case menu: logicmenu(GameVariables)                 rendermenu(GameVariables);                 break;   }}


This way If i wanted to add a new variable I would simply add it to the struct.
Ok, that's good!

But when you have the character class, you're going to pass that too, right?

struct GameVariables;class Character;while(!done){gameloop(GameVariables, Character);}gameloop(struct &GameVariables, class &Character){   switch{gamestate){      case menu: logicmenu(GameVariables)                 rendermenu(GameVariables);                 break;   }}
Yup I'll pass the charater class too. So should I go with a struct?
If you have a gigantic struct of whatever you need that you're passing around everywhere, you may as well just go with globals. The struct doesn't really gain you anything.
Well I was taught that global variables are bad and that I should at least limit them when I can
Quote:Original post by Shadowwoelf
Well I was taught that global variables are bad and that I should at least limit them when I can

Were you taught why global variables are bad?
Spaghetti code he basically called it since anything in the program can change it(and harder to debug). Of course the current way I've done referencing that still allow some functions to change things they shouldn't.
Quote:Original post by Shadowwoelf
Spaghetti code he basically called it since anything in the program can change it(and harder to debug).

"Spaghetti code" is generally used to refer to control flow rather than data sharing, but yeah, that's a good description of the problem.
Quote:Of course the current way I've done referencing that still allow some functions to change things they shouldn't.
Exactly. Since everything in the program will be touching that struct, anything in the program can change it. You're running into the same issue as people who use singletons because they were taught that globals are bad: What's bad about globals is how they're used, not what they're named.

BTW, this thread has some good info, particularly from Rydinare.

This topic is closed to new replies.

Advertisement