Public Group

Data Structures and refrences!

This topic is 3697 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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){
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?

Share on other sites
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]

Share on other sites
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.

Share on other sites
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;   }}

Share on other sites
Yup I'll pass the charater class too. So should I go with a struct?

Share on other sites
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.

Share on other sites
Well I was taught that global variables are bad and that I should at least limit them when I can

Share on other sites
Quote:
 Original post by ShadowwoelfWell 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?

Share on other sites
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.

Share on other sites
Quote:
 Original post by ShadowwoelfSpaghetti 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.

1. 1
2. 2
3. 3
4. 4
frob
20
5. 5
JoeJ
20

• 11
• 11
• 12
• 13
• 9
• Forum Statistics

• Total Topics
632213
• Total Posts
3004853

×