• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ProgrammerGuy123

Game Design With Update and Draw Functions

4 posts in this topic

Objects in my game have both update and draw functions as expected, the update function sometimes need a delta time variable and the draw function sometimes needs a window object.

 

I have seen lots of engines pass in the delta time variable through every update function and pass in the window object through every draw function like so:

//C++
void State::update(float deltaTime)
{
     sprite.pos += vel * deltaTime;
}

void State::draw(Window window)
{
     window.draw(sprite);
}

The problem with passing these objects through every single update and draw method is that sometimes the're not used. For example, a main menu does not need a delta time variable but does need a window object, but you can't just omit the parameter if you don't need it because the main menu inherits from the game state object which contains the parameters.

 

So would it be better in design to never pass these objects into functions but instead make them static like so?

//C++
void State::update()
{
     sprite.pos += vel * GameManager::deltaTime;
}

void State::draw()
{
     GameManager::window.draw(sprite);
}

I'm asking because this is a very fundamental concept to game engines and the latter example just seems to make more since but it seems like the former example is favored.

 

Which is better code design? Thanks.

0

Share this post


Link to post
Share on other sites

I would suggest go ahead have them and passing them in, but I would pass the Window by reference to avoid unwarranted copied.

 

What you can do then is have all objects that require an update and a draw be stored globally in common lists, and can all be looped through and called the same way:

class GameObject
{
public:
  GameObject() {}
  virtual void Update(Window& window) {}
  virtual void Draw(double deltaTime) {}
};
 
class SomeEnemy : public GameObject
{
  ...
};
 
class SomeBackgroundItem : public GameObject
{
  ...
};
 
// in Code somewhere:
std::vector<GameObject> GameObjects;
 
void MyGame::Initialize()
{
  // add all enemies to GameObjects
  SomeEnemy thisEnemy;
  GameObjects.push_back(thisEnemy)
  SomeBackgroundItem  backgroundItem;
  GameObjects.push_back(backgroundItem);
  ...
}
 
void MyGame::MainLoop()
{
  double deltaTime = GetDeltaTime();
  for (uint32_t i = 0; i < GameObjects.size(); ++i) {
    GameObjects[i].Update(deltaTime);
  }
  for (uint32_t i = 0; i < GameObjects.size(); ++i) {
    GameObjects[i].Draw(MainWindow);
  }
}
1

Share this post


Link to post
Share on other sites

I'm not saying which is better(as honestly, it is more of a preference kind of thing anyway), but Unity does it through static classes, where for example there is a Time class, and to access the deltaTime, you use Time.deltaTime.  Note that I'm using the "Time" which is the actual name of the class, not an instance of the class.

0

Share this post


Link to post
Share on other sites

A static delta is another dependency. The update function that takes a float can compile with any code that has floats - any C++. The other example means that your game object now needs to be aware of the GameManager namespace or class. It also no longer starts out with the delta time in a register. It has to load it from memory.

 

If it's getting ignored almost all the time, though, it might make more sense to use those registers to pass in other, more important things.

 

Anything passed in as a parameter is very flexible to play around with for debugging or game feature purposes. However, too many unused parameters can lead to bad style as some programmers might just pass 0 or NULL in when they realize some parameter isn't used, and then things break when you do use them. It's also just unwieldy to have too many parameters.

 

In the end, it's not really a big deal which way you go, but I prefer using parameters until I start to feel bad about too many parameters everywhere.

0

Share this post


Link to post
Share on other sites

Objects in my game have both update and draw functions as expected...

 

OK, I know this isn't what you are asking about, but I'm going to say it anyway. Don't have a "draw" or "update" method per object. Instead have collections of "drawables" and "updaters" in your game objects. That way you can easily combine behavior and create more complex components without having to subclass a billion different objects. Also, if your object doesn't need to draw itself or update itself, this works well. 

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0