84: Design Patterns: Update Method.

Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categories:

What is the update method design pattern? The update method behavioral pattern works with the game loop pattern and lets you manage the behavior of multiple game characters or items that each override a method called update. If you’re building a game that has a fixed number of game elements or at least a fixed number at the start of the game and each one just sits there until one piece is moved each turn, then this pattern is not the best choice. This pattern is perfect for an adventure game where the hero can stumble across a pack of wolves. Some of the wolves will rush the hero while others might try to circle around and attack from the rear. Or even if there are no monsters involved at all and the hero is just walking through town. Unless this is a ghost town, there should be other people and each character will be busy with personal tasks and errands. Without this pattern, how would you manage all these diverse situations? Maybe you could put four separate wolves in the game loop but this would quickly make a complete mess of your game loop. Not just with trying to keep track of each of the wolves but more importantly trying to keep track that there should be wolves in the first place. If it wasn’t for the common application of this technique within game loops, this might not have ever reached the status of a design pattern. It’s really just a collection of derived classes that all override a method called update. What makes this a pattern is that you’ll likely need to do the same thing in any game that has multiple independent game characters. The episode also describes three design decisions you’ll need to be aware of and gives you some guidance for ways to handle them. The game loop will call update on each game object one after another. Over the course of many frames, this has the appearance of updating each game object at almost the same time. This can cause problems though. Especially when the objects interact with each other. Anytime you have a collection that you’re iterating through, you need to be careful with modifying the collection while trying to visit each item in the collection. What’s the scope of the game object collection? You won’t be able to update every object in the entire game in every frame. So you need to plan carefully how to keep track of objects that are too far away to be of immediate concern. The problem though is that the objects could come back into scope and if you handle this wrong, then the game can seem fake. If you’d like to read the book that describes this pattern along with diagrams and sample code, then you can find Game Programming Patterns at the Resources page. You can find all my favorite books and resources at this page to help you create better software designs. Listen to the full episode or read the full transcript below. Transcript The basic description says that this pattern simulates the behavior of multiple independent objects by updating each one, one frame at a time. We’ve actually finished going through all the patterns in the Gang of Four book. There’s a really good book called Game Programming Patterns by Robert Nystrom that I recommend that documents several more patterns applicable to game development. If you’re building a game that has a fixed number of game elements or at least a fixed number at the start of the game and each one just sits there until one piece is moved each turn, then this pattern is not the best choice. You might still be able to use it if the game pieces have some independent behavior. Maybe they randomly taunt the player or have some unique animation. But if they just wait for their turn to move, then this pattern is not the right choice. This pattern is perfect for an adventure game where the hero can stumble across a pack of wolves. Some of the wolves will rush the hero while others might try to circle around and attack from the rear. Or even if there are no monster