9 ms·
Dirty Coding Tricks
- codyb 12y agoGreat article. I'm not a game developer but I enjoyed reading about the constraints they face daily and the solutions they used and how those eventually backfired. I've always figured I'd never want to be a game developer but this gave the feeling I might enjoy it a bit more than I think. Boy, the experience of seeing a fully 3D game run and and run well, that must feel pretty good. I really liked the final problem's solution! Anyways, just rambling a little :-).
- barbs 12y agoI totally agree with you. For me, this is what programming is all about - creative problem solving :).
- probablyfiction 12y agoSome of these are dirty, but I think the majority are smart hacks in the best sense of the word, especially the final story about memory allocation
- jheriko 12y agoThat packing the controller id into a pointer, which you couldn't use to point at a controller id because it gets freed before you can use it? I must misunderstand something because the hack is going to fail too, except that you are going to end up with some controller id in your allocator's free list or some other such nonsense... That last one is best. After having to struggle and fight for 640k for /the heart of a renderer/ if someone had left 2MB lying around that would have been great. Then again so would not using loads of memory for all sorts of weird and wonderful things that are much less important... like C struct padding, or making everything into memory pools, before you have any memory problems. :)
- cbhl 12y agoYou'd probably only need two or three bits to pack a controller id. Depending on how much memory the system has, and and things like data alignment, you can find some bits in the pointer that will always have a specific value (say, zero). Then, you can store the ID in those bits, and mask them away when you actually want the pointer. Edit: You probably shouldn't do this in new code, but in the context of memory-constrained C code it's a nice hack.
- United857 12y agoInteresting enough, the current 64-bit ObjC runtime does something similar, with so-called "tagged pointers". https://www.mikeash.com/pyblog/friday-qa-2013-09-27-arm64-and-you.html https://www.mikeash.com/pyblog/friday-qa-2013-09-27-arm64-an...
- jheriko 12y agoright, but then why can't you take that same opportunity to pull your pointer out and replace it with a zero to avoid it being free'd ?
- AceJohnny2 12y agore: controller id into a pointer If I understood correctly, the pointer is just a field in a common struct (common in event-handling code). As he mentions, it's usually used to point to extra data that you allocated separately in case you need it for that specific type of event, but what if you're having trouble with allocation and your data conveniently fits in the pointer field itself? So he just cast his controller id into the unused pointer parameter. He's not mangling any allocator data. Which, if you're not going to use that pointer parameter otherwise, I think is perfectly sensible (and as long as you don't have code along the line of if (ptr != NULL) free(ptr);). I do it all the time... cough. For example, when creating a thread, you can pass it a void* to some data for it. I just cast a threadID integer into there. Hell, you'll see just that in the first example on a google search for "pthread example"!
- AmirS2 12y ago
- dcole2929 12y agoThat last one about the guy who tucked away 2 extra mb of memory for a rainy day is one of the most bad ass things I've ever read about a programmer (never thought I'd say those words). That's next level, old vet, been around the block a few times, stuff right there.
- netcraft 12y agoI came across code in a legacy web application one day, on a page that was probably the most used part of the application that was really nothing more than sleep(1000); I went through the version control history and found that is was a previous employee that would go in periodically and lower it any time he wanted to act like he had been working - he had started around 3.5 seconds. Which reminds me of being a kid and mom telling me to turn down the tv - I would "accidentally" hit the wrong button and turn it way up for a few minutes, then just turn it back down to where it was.
- rqebmm 12y agoI first heard of "The Speed Up Loop" here: http://thedailywtf.com/articles/The-Speedup-Loop http://thedailywtf.com/articles/The-Speedup-Loop
- nostrademons 12y agoThis is a pretty common sales & negotiation technique. Anchor high, with a much higher profit margin than you actually need. Do your best to get the customer to buy at the high price. Periodically, when you want to seem benevolent, offer a "sale" or "concession" that gets you down to what you were willing & able to do in the first place. Every time you buy stuff on sale at the grocery store, you're seeing it in action, but you probably don't think much about it.
- 3pt14159 12y agoIf I ever found out someone did this not only would I fire them, I wouldn't keep it a secret. I'd make sure everyone on the team knew exactly why he was fired. That is acting is gross bad faith. Think of the time it had cost all those people around the world just because he wanted a day off here and there. Blows my mind that someone would do that.
- koala_man 12y agoI knew some people who wrote a graphical demo at a small, local demoscene party. One of their effects had a static, vertical tearing artifact that moved around based on their parameters but which they weren't able to entirely get rid of. One of them noticed that the projector screen that the demos would be shown on had a vertical splice. He eyeballed the location and tweaked the parameters to make the artifact line up with it. No one noticed.
- marcosdumay 12y agoMy assembly classes at undergrad had a final project consisting of writting a tetris clone in x86 assembly. Shortly after the deadline, my program had a bug that made pieces fall up, instead of down, and I was completely unable to find it's cause... In the end, I just rewrote the score checker to verify the top of the game, instead of the bottom, and delivered a game where pieces felt down. Everybody not just noticed, but I had a line of people wanting to play it.
- mVChr 12y agoSeen this a few times before, but I am always entertained on reread and enjoy the reminder that sometimes you need a scalpel and other times a sledgehammer.
- ConnorG 12y agoAlways is a great read. Can't help myself into rereading these stories every time I see this post.
- _mikz 12y ago(2009)
- AceJohnny2 12y agoYeah, I was wondering about that. I had certainly read it before, but was confused about the "Copyright 2015" at the bottom of the page. If you follow the link to the non-print version of the article you get the real date.
- eterm 12y agoAh ha, I was wondering this too since I've seen it do the rounds before. The print version is so much more readable with a wider body and no comment clutter!
- raverbashing 12y agoThis is a good reminder for the "avoid technical debt" crowd and other advocates of sterilised development practices Sometimes the rubber hits the road and you don't have time or money or any more cognitive energy to understand why your code that calculates a lot of things from a lot of data is not working in that 0.01% of cases So you come up with a quick fix that's not exactly what it has to be done but it works it ships and it pays the bills. Of course I don't advocate such solutions on critical paths, ate least not without some double checking.
- MBCook 12y agoGames are something of a different beast, at least at the time the article was written, in that they have a fixed 'end' point. After the game was shipped you didn't go back to fix things later, things would only be fixed when you were starting the sequel (or some other game) with that engine. After all, what gets burned on a PS1 CD or PS2 DVD is permanent. I'm sure that's at least somewhat different today with so many games having scheduled DLC and other updates that make the technical debt more unpalatable, although I'm sure such things still go on at a high rate.
- gabriel34 12y agoOcarina of Time, and probably other games, had some glitches fixed, but sure, you have to ship your first client something playable beginning to end, because this client's media is permanent
- nostrademons 12y agoYou do want to have your default setting be "avoid technical debt", so that these sort of hacks are just that - hacks. Projects get into trouble when people do them as a routine matter; they're saved when a project that's otherwise healthy needs to do something naughty to work around a tricky bug.
- raverbashing 12y agoYou're right, however, there are two issues. 1 - People "avoiding technical debt" by overengineering. 2 - (As you mentioned) sometimes it's not even your fault that you need a hack. (It may not even be a bug on what you're using, but just an unfortunate impedance mismatch, like the Playstation problem mentioned)
- VBprogrammer 12y agoSpurred on by the success of "if A==bad then NOT A", I used this tool to patch several more bugs -- which nearly all had to do with the collision code. This reminds me so much of a developer I work with, why figure out the root cause when you can throw a try / except / pass around it. If that doesn't work upgrade a package and see if that helps. If that doesn't work throw in a hacky fix.
- raverbashing 12y agoOMG what about those They're also the first to blame "GCC/Python/JVM/etc" and saying there's a bug there.
- dvisbeen 12y agoLove these tricks
- debacle 12y agoI always try to explain to people I work with that you can't get away with hacks at the deadline if you've been hacking from the start, but if 90% of your system is an ivory tower, it usually doesn't hurt that 10% is a nefarious back alley.
- donatj 12y agoI find th CRC collision story rather unbelievable.
- yzzxy 12y agoIt's not a "true" almost impossible collision that occurred, but two files that shared the same content and filename and would collide by definition.
- delecti 12y agoThey shared the filename, but the story says "even though they were completely different", IE, not the same content. It was a hash collision; certainly rare, but enough project with enough files around the world and it's bound to have happened to somebody.
- dietrichepp 12y agoMost people find this kind of thing unbelievable, which is why they call it a paradox (the "birthday paradox"). Assuming a perfect 64-bit hash, and "tens of thousands of files" meaning 20,000 files, the chance of collision is one in 46 billion. However, if they reuse filenames, then the chance of collision is much higher. For example, assuming 1000 distinct filenames chosen at random, the chance of collision rises to about 1 in 10,000.
- FLUX-YOU 12y agoYou see, shortly after leaving his boss's office and learning about the hidden-allocated 2MB, that guy implemented his own CRC that would throw the same hash just before release.
- sixothree 12y agoMaybe they were using 32-bit integers as their crc?
- pjc50 12y agoApparently with CRC32 you only need about 77,000 items to have a 50% chance of a collision by birthday paradox.
- zomgbbq 12y agoWe once had a game demo that was crashing every time we tried to exit the game. Inserted an exit(0); in the shutdown code and it never crashed again.
- dbrower 12y ago+1. That should always work as an exmaple of "crash only" programming. It's nice to get shutdown working for valgrind, but it should never be necessary.
- shinymark 12y agoI'm Mark Cooke, I wrote the first entry in this article a few years ago. Of all of them, it's the least related to interesting code, but it does reflect the reality of the kinds of crazy last minute hacks that can go into shipping a game. Unlike many other types of reusable code, this sort of gameplay hack will be thrown away and won't be used again in a subsequent game. To an extent it justifies its use in a pinch. But no craftsman is ever happy shipping something like it. It has been my experience that at the end of game development projects many of these sorts of hacks start flying. Most of them are unnoticed by players. No game is ever perfect. With limited resources and time it's important to keep in mind what players care about and focus on those things. I care deeply about providing a good experience to players and one of the most interesting and challenging components of game development is deciding where to apply resources when under tight constraints to deliver the best experience. I haven't worked outside the game industry in a long time but I'm sure this is true in most consumer software businesses.
- barbs 12y agoOut of curiosity, what was the game you were working on in the article?
- barbs 12y agoAlso worth reading is the three-part article by Patrick Wyatt on the issues encountered whilst developing Starcraft[1], which were a huge factor in its delayed release. Of particular interest is the path-finding hack they used [2] [1] http://www.codeofhonor.com/blog/tough-times-on-the-road-to-starcraft http://www.codeofhonor.com/blog/tough-times-on-the-road-to-s... [2] http://www.codeofhonor.com/blog/the-starcraft-path-finding-hack http://www.codeofhonor.com/blog/the-starcraft-path-finding-h...
- dividuum 12y agoOne of my favorite tricks is this one (I thought I read about it in that exact collection but it isn't there, so here you go): Back on Wing Commander 1 we were getting an exception from our EMM386 memory manager when we exited the game. We'd clear the screen and a single line would print out, something like "EMM386 Memory manager error. Blah blah blah." We had to ship ASAP. So I hex edited the error in the memory manager itself to read "Thank you for playing Wing Commander."
- sreyaNotfilc 12y agoYes!! I love that!
- Kurtz79 12y agoMy choice would have been "640K ought to be enough for anybody". (This will be understood only by those who lost countless hours tweaking their autoexec.bat and config.sys to squeeze that last KB of memory needed to run the game they were supposed to spend the weekend playing ).
- GotAnyMegadeth 12y agoThese seem to come from here [0] which has more and some other general tips. [0] http://www.dodgycoder.net/2012/02/coding-tricks-of-game-developers.html http://www.dodgycoder.net/2012/02/coding-tricks-of-game-deve...
- Lewton 12y agoIt's the other way around, the gamasutra article is from 2009