11 ms·
Game Programming Patterns (2014)
- elyseum 6y agoSame author as http://craftinginterpreters.com/ http://craftinginterpreters.com/ Can’t wait for this one to appear in print.
- airstrike 6y agoYou might enjoy this discussion https://news.ycombinator.com/item?id=22788738 https://news.ycombinator.com/item?id=22788738
- aidos 6y agoHe’s an amazing writer. I couldn’t put this book down when I first read it. At the time I sent him a message saying that it was the best technical book I’d ever read and his response was along the lines of “thanks! It’s the best (though, only) one I’ve written”. I’m glad that’s no longer true. Even reading his writing about writing risks reducing you to tears. http://journal.stuffwithstuff.com/2020/04/05/crafting-crafting-interpreters/ http://journal.stuffwithstuff.com/2020/04/05/crafting-crafti...
- signaru 6y agoThis guy really deserves an award. I wonder decades from now, how many people would look back and say how much these books have influenced them.
- munificent 6y ago> This guy really deserves an award. I have been amply rewarded by both sales and really really nice words from people over the years, so I think I've got more awards than anyone could ask for. What's interesting is how circular the process is. If my first book hadn't been successful, I certainly wouldn't have written my second book, so I owe its existence to many other people.
- gww 6y agoI have always wanted to program a 2D game engine in C++ for fun. Mostly as a learning experience but whenever I decide to do it I get intimidated trying to figure out how to design it and give up. Does anyone have any recommendations of resources to get over figuring out how where to start?
- krapp 6y agoJust start anywhere. You're going to end up throwing it all out anyway. Accept that failure is the path to success and just do it.
- meheleventyone 6y agoPick a simple game and make that game. Pick a new game and make that game. Take the things that fit the second game from the first game. Now you have at least some basics that do input, rendering and audio. Pick a bit to improve towards your target. Making “an engine” isn’t really something you can do unless you have an idea about what making a game actually means. Then you can work out what it is that you actually value.
- gww 6y agoThanks, I am going to try to start with something easy like tetris.
- skocznymroczny 6y ago
- lachlan-sneff 6y agoI haven't had time to read the book yet, but the web version of it is just so beautifully displayed. So, kudos for that :)
- munificent 6y agoThank you, I put a lot of time into the design and CSS.
- rimher 6y agoLoved this book! Reads really well. Author here: https://twitter.com/munificentbob/ https://twitter.com/munificentbob/
- isoos 6y agoAlso author here on HN: https://news.ycombinator.com/user?id=munificent https://news.ycombinator.com/user?id=munificent
- formalsystem 6y agoThis is the book that finally helped me grok software design patterns. Gang of four reads like a dictionary while the examples of Game Programming patterns have stayed with me. If I'm rendering a forest of course I don't want to copy the data for trees a 1000 times so of course I need to use the flyweight pattern The decoupling patterns chapter is particularly good and will help you understand how to turn each entity in a game into a server which sends events to other entities. That way you end up with many small pieces of a code as opposed to a single giant game loop. The entity component system (ECS) is one of the main reasons why programming games in Unity is so pleasant. If it wasn't obvious by now, this is not a book about game programming patterns it's the best book on software design patterns that I've ever read. Most software design books seems to think I'm only interested in designing accounting or banking software, why not a game?
- anchpop 6y ago> The entity component system (ECS) is one of the main reasons why programming games in Unity is so pleasant. I agree programming in Unity can be very pleasant. To avoid confusion, I wanted to point out that traditional Unity programming isn't really a typical example of an ECS. Unity's system actually lacks most of the performance benefits you can get from one. This is relevant because Unity is actually currently developing a "real" ECS architecture they call DOTS (data oriented technology stack).
- meheleventyone 6y agoTo be ultra confusion avoiding you don’t necessarily get great perf benefits from the merely organising things as per the ECS pattern. You still need to organise your data around how it is used. Which is why Unity uses archetypes which trade fairly automatic organisation based on entities component signature for increased cost if you add and remove components from them (as it triggers reorganising where that entities components are stored). There are other patterns with different trade offs. In terms of just mental overhead any form of composition is useful be it Components in the Unity sense or Components and Systems in the ECS sense.
- gentleman11 6y ago
- Kapura 6y agoI've been programming in the games industry, first at indie scale and now at AAA, and I use almost all of the patterns described in the book in some way. This is an invaluable resource for people to learn not only the sorts of programming patterns used in games, but also the sorts of problems that game developers are challenged to solve. I've found that google and stack overflow are now worthless for anything outside of checking syntax for a C++ lambda. Everything else I work on is essentially a novel problem set.
- erikbye 6y ago> novel problem set Examples?
- Kapura 6y agoWell, I've spent the past week tuning our boss fight camera algorithms so that the camera does its best to track the boss if they dash across the room or leap into the air. Nobody can really tell me how to do this outside of our company, because nobody else is dealing with a boss that moves like ours, nobody else has the same camera language constraints, and frankly dealing with algorithmic camera motion is not something most programmers ever have to do. I can look at our other code, I can look at other cameras in game & film, but ultimately it's just me down there in the code deciding whether the pull the camera in or pitch down or lengthen the lens or how to handle blends or whatever.
- vvanders 6y agoOh man camera systems. We had one dev and an animator spend months getting our camera system to the point where it felt natural. Definitely one of those things that takes a ton of work and only gets noticed when it goes wrong.
- mmoll 6y ago> Everything else I work on is essentially a novel problem set. It‘s a rotate! ;-)
- robmaister 6y ago
- DethNinja 6y agoThis is one of the best books available on the subject of design patterns. I highly recommend it for all programmers even those not in gamedev. Keep in mind that some novice programmers assume design patterns are strict rules and you have to copy all idioms, naming rules etc. but they are just simple guidelines, feel free pick them apart and combine them on your code.
- marvinblum 6y agoAh yes I remember. This site teached me so much, even if I'm not doing game programming anymore. It shows some generally useful patterns and algorithms not only relevant to game development. So if you're a programmer, give it a shot!
- Konryan 6y agoDo you have any recommendations for books about Game Architecture?
- meheleventyone 6y agoGame Engine Architecture is a great overview and on it’s third edition.
- hesdeadjim 6y agoFirst version of this book was an immense help for me early in my career.
- sizeofchar 6y agoI used the second edition extensively on my masters. Truly a gem!
- dang 6y agoSee also... the largest previous threads: 2017: https://news.ycombinator.com/item?id=14475489 https://news.ycombinator.com/item?id=14475489 2014: https://news.ycombinator.com/item?id=7646985 https://news.ycombinator.com/item?id=7646985 2014 ('now finished'): https://news.ycombinator.com/item?id=7634734 https://news.ycombinator.com/item?id=7634734 2013: https://news.ycombinator.com/item?id=6004885 https://news.ycombinator.com/item?id=6004885 About specific chapters: 2018: https://news.ycombinator.com/item?id=17845334 https://news.ycombinator.com/item?id=17845334 2014: https://news.ycombinator.com/item?id=7543158 https://news.ycombinator.com/item?id=7543158 2014: https://news.ycombinator.com/item?id=7466711 https://news.ycombinator.com/item?id=7466711 The first thread was https://news.ycombinator.com/item?id=874080 https://news.ycombinator.com/item?id=874080 from 2009, and the discussion is about how incomplete everything is. That's interesting, because how many items with a discussion like that ever actually do get completed? If I missed any interesting discussions let me know and I'll add to the list.
- deleted 6y ago[deleted]
- Sprakle 6y agoDoes this book have much relevance for someone already working in an existing engine like Unity?
- trynewideas 6y agoYes, absolutely, 100%. It's engine and language agnostic; many of the examples are written in C++ in lieu of pseudocode, but he also goes into where C# differs in behavior enough to affect the pattern.
- zwaps 6y agoI think it's probably good to know for every engine and besides, it's a beautiful book. That being said, armed with my baby knowledge of programming patterns, I then did find it frustrating that today's engines are very high level and quite opinionated about how you code. Take for example Godot. It's not 'quite' an ECS and there are certain ways to script things which you should follow. How to fit the patterns from this book into a Godot game is a whole other ballgame. This book makes me want to write my own engine. Not to deal with the technical details, but to be in control of the program logic. This excellent book makes you want to worry about the stuff that engines abstract from in their own special way. That's not efficient from the standpoint of getting a game done - things are done in Unity or Godot in their specific way for good and battle-tested reasons.
- moonchild 6y ago> This book makes me want to write my own engine Do it! I've been working on a game engine for several months now. It's a similar kind of hell to working on an OS (though easier to debug...until you start using threads), but it's fun.
- fao_ 6y agoSo far with game programming the most difficult problem I've had is input states. There doesn't seem to be a clean way to manage it that doesn't get very messy, very fast. Even though it should be easy to manage since it's just a state machine!
- simonbw 6y agoBy input states do you mean stuff like knowing whether a key is currently pressed or a mouse button is currently held down? Could you explain what you mean by it gets messy?
- fao_ 6y agoI mean segregating mouse actions. So: Drag, Click, etc. Also handling selection events and so on. I've been using love2d, which basically means you have to set up those manually. Then building a state machine in lua gets very messy. I dunno, maybe I'm just overthinking it
- krapp 6y agoHave you tried SDL2? I don't know what your criteria for "messy" is but its event stack makes it pretty straightforward to get input from the mouse, controller and keyboard.
- softfalcon 6y agoYeah, before you know it, you’re in the land of colliding mouse down events. Best I’ve been able to do is group interaction views together (think inventory and character menus vs the character controller). Separate these view groups so they are triggered by a mode state using an event system that tells each view group which is active. Only update the active view group. This isolates each view group to receive mouse/key events by itself. It also keeps menus that interact with one another consolidated together in one place. This helps you reason out the mouse/key events more easily since you know this view group won’t disrupt the other inactive view groups. Within a view group though, a focused control pattern is a good idea. A simple way is via hit bounding and z-ordering. The top-most, mouse over bounds control is active to receive click events. Everything else is ignoring input. tldr; hierarchy your input controls into groups so you can ignore the input on inactive/not visible ones.
- edem 6y agoThis book is pure gold. I recommend this to any programmer!
- s3cur3 6y agoIf you'd like a summary of the book, my study notes are here: https://tylerayoung.com/2017/01/23/notes-on-game-programming-patterns-by-robert-nystrom/ https://tylerayoung.com/2017/01/23/notes-on-game-programming...
- tgb 6y agoWhat's the gist of the setup for how a complex RPG rule system gets coded up? Like in D&D or a Diablo-like, you have base stats, equipment or classes modify those stats, sometimes the modifiers depend upon the circumstances, and maybe the modifiers get modified. So clearly you don't want to have a Belt of +4 Strength just do wearer.strength += 4 since at some point you'll have to take off the belt or are in an anti-magic field, etc. But what's a good and/or standard design for encoding all that kind of logic?
- formalsystem 6y agoThere's a combination of patterns. IMO the best way to learn how this stuff works is to look at the code of a production level MMO/MOBA/RTS. Here is a really good one https://assetstore.unity.com/packages/templates/systems/ummorpg-51212 https://assetstore.unity.com/packages/templates/systems/ummo... Worth every penny.
- MrStonedOne 6y agoYou have calculate it on read based on the user's state at that time. getStr() { output = this.str;foreach (equip) output += equip.getStrMod() }
- munificent 6y agoI've run into this exact issue in my roguelike [1]. I don't think I have a fully baked solution yet, but the direction I'm going [2] is a combination of the Observer pattern and caching. For derived stats, it's not enough to just always recalculate its value from scratch every time you access it. At least in my game, I also want to tell the user when the value of a derived stat changes. If you equip a piece of armor that raises your strength, I want the game to tell you "You feel stronger!". So each derived stat stores its current value and then has a refresh() function to recalculate itself. The stat is refreshed at the end of each turn. It recalculates the value and then compares its previously cached one. If the value changed, it logs the result and then updates the cache. [1]: https://github.com/munificent/hauberk https://github.com/munificent/hauberk [2]: https://github.com/munificent/hauberk/blob/master/lib/src/engine/hero/stat.dart https://github.com/munificent/hauberk/blob/master/lib/src/en...
- lonk 6y agoIt's a good book, but it contains lines of code that appeal to the subconscious like: int destY = unit->y() - 1;
- toastal 6y agoI wish there was a complete FP engine. Most 'patterns' in FP are (surprise) just functions. There's a lot of blogs with some simple examples from scratch, and that's really cool, but there's a lot of room for a complete system. What isn't needed is all this encapsulation and inheritance I see in many other big engines.
- kuharich 6y agodang, prior comments: https://news.ycombinator.com/item?id=8555285 https://news.ycombinator.com/item?id=8555285
- rahulpyd1 6y agoThis book is well written. It has the right amount of detail and simplicity. When I started out in game development the book helped me lot in understanding game development.
- dakom 6y agoI had a bit of difficulty with this book. Loved reading it in every way (style, content, clarity, etc.) - but in parts[0] of every section I found myself thinking, "wait - of course that's what I'd do... but that's just, like, the thing to do... why is it a pattern? Does it really matter if I call it that pattern or if I don't know the pattern name?" Maybe it's because I didn't learn coding in university, or because most of my career has been spent in very small teams with little need to use pre-established jargon, but I get slightly annoyed when I see discussions about this pattern or that pattern and I have no clue what they're doing, even if I've myself used that "pattern" dozens of times without knowing the accepted name for it. YMMV [0] the other parts would be when it got into the nitty gritty of solving a specific problem instead of the generic pattern. For example, propagating transforms using the |= trick. That stuff was great for me.
- haihaibye 6y agoBy giving it a name, you can refer to it more quickly and precisely with other programmers.
- codemonkey-zeta 6y agoI totally agree with you, but I think you're understating the value of the pattern language. Of course any good practitioner will have internalized these "patterns" to the point that they're second nature, in the same way a good carpenter will know all the uses for his tools. The problem with carpentry though is that you require (for the most part) a mentor to pass on this first-hand experience. Programmers benefit from being able to describe our craft with this design pattern language, so that other practitioners can understand very quickly what our implementations look like, and more importantly (IMO), neophytes can quickly and independently familiarize themselves with the patterns to more quickly become experts. But I don't just think this book is for beginner programmers or people who don't understand the idea behind the patterns. Even competent designers like yourself ought to know the names for these concepts purely for discursive purposes, maybe teaching new engineers or writing blog posts, or designing with other engineers at the same level. The pattern language is just an efficiency layer on human-human communication, which for many of us is one of the most challenging parts of our jobs.
- 6y ago
- rectang 6y agoAccording to the "Game Loop" chapter, the "intent" of the Game Loop pattern is to "Decouple the progression of game time from user input and processor speed." http://gameprogrammingpatterns.com/game-loop.html http://gameprogrammingpatterns.com/game-loop.html The chapter goes on to assert: > Game loops are the quintessential example of a “game programming pattern”. Almost every game has one, no two are exactly alike, and relatively few programs outside of games use them. It seems to me like audio programs would typically be organized using something very close to the Game Loop pattern, though? A loop with where a batch of data is rendered on each iteration, decoupled from the processor because of the need to match the sample clock. Is there a resource out there that describes audio programming patterns for organizing the whole program? Most of what I've read describes how to use an existing framework — e.g. how to write a plugin — and while that's not surprising, I'd really like to explore all the possible options for high-level organization of a real time audio app. I haven't ever seen something similar to this "Game Loop" chapter for audio.
- mcv 6y agoYeah, I imagine anything that needs to produce output continuously independent from user input, while quickly responding to user input, needs this kind of loop. At the same time, while most games these days need it, I don't see why a purely turn-based game would need it. If you only produce output when the user gives input, you probably won't need it. Those kind of games are rare these days, but roguelikes are still getting made apparently.
- golyi 6y agoI love this book and will not only will it help you for game development, but all software engineering in general. Highly recommended.
- munificent 6y agoI know this book! :) I had occasion to dig through my sales data, so if anyone is curious, since I first put this book out there, I've sold 26,009 print copies and 13,066 Kindle copies. I didn't count the ePub and PDF sales. It's been translated to Korean, Chinese, Japanese, German, and Polish. It's incredibly gratifying to see it still helping people six years later. I can't wait until I'm through building the print edition of my second book [1] so people can get their hands on that too. [1]: http://craftinginterpreters.com/ http://craftinginterpreters.com/
- nivethan 6y agoThank you for the book and I can’t wait to get a copy of it! I did the first part in typescript and it’s crazy how much of your java code just works with minimal massaging. Would you recommend brushing up on C before jumping into the second half, I don’t want to just be copying code and miss the real picture. Just some a stream of consciousness as I read the book and had my mind blown a few times. https://github.com/Krowemoh/ts-lox https://github.com/Krowemoh/ts-lox
- munificent 6y agoIt depends on your level of C expertise. It's not very advanced, sophisticated C, so if you're mostly got pointers loaded into your head, you'll be fine. If a few `` and `&` will leave you scratching your head, it's not a bad idea to practice a little before you tackle the book. The introduction chapter suggests this exercise: > To get some practice with pointers, define a doubly-linked list of heap-allocated strings. Write functions to insert, find, and delete items from it. Test them.* If you can handle that, you should be fine for the rest of the book.
- gentleman11 6y agoThis book is fantastic. If the author is reading this, please don’t feel shy about expanding the chapter on state machines. It is an extremely deep and valuable subject with a lot of “get your hands dirty” learning to do. Thanks for writing what you did!