Jump to content
  • Advertisement
Sign in to follow this  
Shadowwoelf

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.

If you intended to correct an error in the post then please contact us.

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

Share this post


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

Share this post


Link to post
Share on other sites
Here are most of the variables that most of my functions will need


BOOL done=FALSE; // Bool Variable To Exit Loop
GameState_E Gamestate=MENU;
int selector=0, max_selector;
GLubyte Ycolor = 255;
GLfloat Width, Height;
GLfloat roll; // Rolling Texture
bool 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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?

Share this post


Link to post
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 this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!