Archived

This topic is now archived and is closed to further replies.

update(), draw(), and global variables.

This topic is 5012 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

Whenever I see the code for someone''s main game loop, it almost always looks something like this: while(gameRunning){ update(); draw(); } Just like that. update and draw typically take no parameters. If anything, maybe update takes a double representing the time since last called. What about all the data structures and stuff that make up a game? Under the (very reasonable) assumption that draw() will be referring to the same data structures that update() updated, how do most people do this? Is everything global or what? Seems to me if you dont want all your data to be global, then you have to pass everything you need to update into the update function, and then again to the draw function. But no one does this. So is everyone making their data global?

Share this post


Link to post
Share on other sites
Well if the two functions are on the same object, then they both have access to the object's member data.

Passing ALL the data needed for drawing would get a little ridiculous for a big game. A good strategy is to put it all in one object, called the Model, and give the drawing object access to this object. Either you a) make the model global, b) make a static accessor function that returns the model, c) initialize the drawing object with a pointer to the model, or d) call draw() with a pointer to the model.

But anyway, there are lots of ways for a function to get the data without declaring every single variable as global.

[edited by - pinacolada on March 23, 2004 11:15:48 PM]

Share this post


Link to post
Share on other sites
I dont think I''ve ever seen draw() and update() both be member functions of the same object. In fact, I dont think Ive ever seen them as member functions period. Are you saying that you would do something like this:

while(gameRunning){
game->update();
game->draw();
}

where the game object contains all level data and all item, monster, everything data in the game?

Some of the things you suggested make sense, I guess. But I was just wondering what was the most common way of doing this.

Share this post


Link to post
Share on other sites