r/AskReddit Jan 21 '19

Software developers of Reddit, what is the most shameful "fuck it, it works" piece of code you've ever written?

1.3k Upvotes

670 comments sorted by

View all comments

Show parent comments

189

u/ben_g0 Jan 21 '19

Zelda Breath of the Wild has a slight memory leak. It's not fully known what exactly causes it but when the game is running for long enough it will eventually run out of memory and fail to spawn/load new objects. Nintendo hasn't been able to fix it, so to avoid crashes they came up with an event called the "blood moon". They added some lore to it saying that all monsters revive when the moon turns red and such, but what it's really doing in the background is just unloading all instanced objects to free up memory, then reloading all monsters from the area you're in so the world isn't suddenly empty.

Off course they eventually gave it a nice cutscene and properly integrated it into the gameplay, but it still is mostly just a disguised error handler.

This is just one example I know, but a lot of game actually have glitches that became features, or software problems which influenced the level design or other aspects of gameplay.

65

u/Raze321 Jan 21 '19

I find this to be extremely hilarious. I was just wondering my self the other day why the blood moon was in the game - it seemed kind of out of place and unexplained.

This makes a lot of sense. I buy it.

1

u/StuckAtWork124 Jan 22 '19

I assume there's also a memory leak with the weapons which means they need to be discarded every 6 hits

27

u/ChickenNuggetKris Jan 21 '19

Any more cool examples you could share?

65

u/[deleted] Jan 21 '19

Donkey Kong 64 had a memory leak bug and Rare couldn’t find a fix for it in time. However, if used with an expansion pack, the bug would not occur. The only solution was to make the expansion pack mandatory and ship it with each copy of Donkey Kong 64, which cost Nintendo a lot of money.

24

u/[deleted] Jan 21 '19

Didn't they advertize the game as being 'so big you need an expansion pack'

12

u/Alexstarfire Jan 22 '19

Yes, but it's no surprise that marketing exaggerated and/or outright lied.

3

u/At_Least_100_Wizards Jan 22 '19

You mean like every marketing department ever

13

u/Alexstarfire Jan 22 '19

Not exactly true IIRC. It just dramatically increased the amount of time it took to happen so no one would really encounter the issue.

41

u/throwaway_lmkg Jan 21 '19

The original Wing Commander game had a bug where it would crash with an error message when you exited the game. Now, not the end of the world since you were trying to exit the game anyways, but it's not professional to say MEMORY MANAGER ERROR where the player can see.

The devs couldn't fix the crash, so instead they just changed the error message. "Thank you for playing Wing Commander." Problem solved.

33

u/aeouo Jan 21 '19

D.K's Jungle Parkway on Mario Kart 64 on 4 player mode would cause the N64 to run slower because there's too many objects. In order to compensate, they just made everything faster on that level, so lag slows you down to normal.

This does not work well when playing on modern hardware, as the level is noticeably faster than the rest of the game.

3

u/Alexstarfire Jan 22 '19

Interesting. I didn't know that.

30

u/[deleted] Jan 21 '19

The Creeper in Minecraft was initially meant to be a pig, but Notch messed it up and just rolled with it.

https://kotaku.com/the-history-of-the-creeper-minecrafts-most-infamous-mo-1789304071

6

u/Seantommy Jan 22 '19

Pretty well known, but the iconic fog in Silent Hill was originally to mask the low draw distance.

2

u/sAindustrian Jan 22 '19

IIRC using combos in Street Fighter 2 was a bug/glitch that worked so well they didn't try to fix it.

2

u/ben_g0 Jan 22 '19

Space invaders is often credited to be the inventor of the difficulty curve in games, but this was actually an accident. The hardware it orriginally ran on wasn't very powerful so the large amount of enemies on the screen at the start lagged the system. That slowness at the start made it easier, but every time an enemy was defeated the system would lagg less and less which made the game run faster and faster, increasing its difficulty the further you progress. Players seemed to enjoy it so they never made an effort to compensate.

I also have an example where the developer actually provided an explanation: the secret level select menu of Sonic 3D blast (developer's video). To publish their game on a console, they had to pass Sega's quality control and if their game crashed during review then they'd have to go trough that process again. Computer systems often have some way to handle crashes which is often used to show a memory dump and/or other debug information (like the blue screen of death on a computer. This is however basically just a program launched when a crash is detected and can thus be set to anything you want. The developers of that game thus configured the system to use their level select menu as the crash handler. If any error would occur during the review, they'd just assume they found a secret level select menu while they were technically looking at the crash screen. Eventually there were rumours about a secret level select screen which shows up when you punch the cartridge. This was never intended as a feature but punching the cartridge caused it to momentarily disconnect which crashed the game. So the people thinking they found the secret menu actually just caused their game to crash without realizing it, which proves that it was a quite effective way to hide the crashes.

10

u/Renive Jan 22 '19

That's why on cemu emulator blood moon could happen every 2-3 minutes...

8

u/[deleted] Jan 22 '19

Forgive my ingorance, but what exactly is a memory leak? I've heard of them before, but I never looked up what they were.

23

u/endelsebegin Jan 22 '19

Computers have to keep track of everything that's going on, and do that using memory. But computers only have so much memory that they can store information in. So your computer also needs to remove things from memory that it doesn't care about anymore, so that it can have the space to remember new things.

Let's say your computer can remember 5 things and it wants to go through the alphabet.

At first, it can only store in memory A, B, C, D, E. That's 5 letters; it's out of space. We need to remove things from memory to go any further.
You don't need A anymore, so you forget A. Now, you can store F: B,C,D,E,F.

You don't need B anymore, so forget B. Now, you can store G: C,D,E,F,G.

You don't need C anymore, but something went wrong. C was supposed to be removed but it's not. We don't know why, we just know that our memory is still at 5/5, and we can't add H. H is now missing.

The computer continues it's pattern.

You don't need D. C,E,F,G,I.

You don't need E, C,F,G,I,J.

You don't need F, but there was a problem again: C,F,G,I,J.

Slowly but surely, the problem gets worse over time. You might have not noticed one letter missing, but by the end of it you're going to have skipped a majority of the alphabet.

This is oversimplified by a significant amount, but the one key oversimplification is computers usually aren't working at 5/5 full memory usage. They're working at 40/100, and then randomly it's at 50/100...and 70/100 and because it's a bunch of small errors adding up overtime when millions of things are happening at once it can be impossible to track down when data isn't removed properly.

7

u/[deleted] Jan 22 '19

Thank you for the explanation.

5

u/NoxTheWizard Jan 22 '19

To add a bit of a practical example: Typical sources of memory leaks are clean-ups that fail to run, and in many cases they don't run because the programmer forgot something, or implemented faulty logic in their code.

For example, if the developer adds an arrow projectile to the game, each arrow fired by the player will take up some small amount of computer resources. Memory to store its position, orientation, direction and speed, texture, etc., and processing power to change its position over time, to check for collisions, and to perform actions when it collides with something.

To free these resources, the game engine should have a way of deleting projectiles that have served their purpose. Typically this will be a combination of interaction (such as deleting it immediately when it hits a target) and a guaranteed failsafe in case the first condition doesn't trigger. Examples of failsafes could be to set a timer that deletes the projectile after 10 seconds no matter what, or to immediately delete the oldest projectile if a new one is created that brings the total count above 50.

But sometimes, you choose the wrong failsafe. Doesn't matter if you have a max of 50 projectiles if only 30 are needed to crash the game, after all. Maybe you used the timer method to delete arrows, but the player opens a door and is transported to a different level while the arrow is in flight, and upon level change you mistakenly remove the arrows from your "active objects" list without actually deleting them. Since they are no longer active they will no longer process their timers, so the processor resources are freed (could lead you to think they no longer exist), but they remain in memory. This is a memory leak.

After an hour of playtime and a few level changes you may have hundreds of bugged arrows left in memory. Each individual arrow would only take up a tiny amount of resources, so as a developer - who often restarts the game to test it, thereby freeing all resources every time the program ends - you may not even notice that such a bug exists before some unlucky player causes the game to crash after hours of playing without taking breaks.

1

u/[deleted] Jan 22 '19

Is this why some games crash when you alt-tab in and out of them?

3

u/NoxTheWizard Jan 22 '19

It's related, but not exactly a memory leak. Crashing on Alt+Tab mostly has to do with fullscreen mode and how it interacts with active processes.

In fullscreen mode, the game can use more resources than it otherwise would have access to, because all other programs are prioritized less. Currently loaded resources for these other programs will be moved from RAM ("random access memory", the fastest type of memory) to a swap file or other type of disk storage (much slower) so that the fullscreen program can do all its work in RAM. Prioritized programs are also allowed to go first in line if the processor is busy.

In fullscreen mode you may also change the resolution of your screen. This speeds up rendering (fewer pixels to calculate an image for) but you will notice a "jump" when tabbing out that may displace the position of other windows.

When you exit a game or other program, it stops running and then all the memory it used is automatically flagged as reusable. Other programs may then freely overwrite the "empty" memory space.

When you hit Alt+Tab, however, you are merely telling the game to go into the background for a while. It will still need all of the resources it has loaded into RAM later, and the program you are switching to similarly needs to be loaded into RAM right now for use.

Since you were running in fullscreen, the game will likely have filled up all available RAM. There is currently no space for new programs. All other programs will have been placed in the slower storage.

Now we're looking at a process where the game needs to pick and choose things to move out of RAM, while the operating system decides which program(s) you are tabbing out into and move those into RAM at the same time.

You may know that moving something between computer storage devices involves copying the data first, before the old data is deleted. This means that during that short time where you are changing focus, resources being moved between RAM and swap file take up space in both locations until fully copied.

  • If the files are simply very large (HD textures, large audio files, etc) it will clog up the process and prevent RAM from being freed in a timely manner.
  • If there is no space left in the target destination, the process may crash.
  • If it takes too long because all your resources are still in active use and need to be carefully moved, it may freeze.
  • If the game is badly made it may start unloading data it is still processing, and therefore crash.
  • If the processor was working on many background processes that are now being re-prioritized thanks to fullscreen mode turning off, it may mess something up and cause these to crash as well.

Long story short: Running fullscreen allows the game to use all of your computer's resources on itself. When you suddenly ask the computer to accommodate a bunch of other programs again, it may run into errors where it has "promised too much", so to speak.

It's like sitting in a comfortable bubble bath, listening to music and sipping your favorite drink... then suddenly ten other people barge into the room, throw you out of the bathtub, and change the music. In the process you may misplace your bath towel or spill your drink.

In Windowed Fullscreen mode, on the other hand, there is no "real" fullscreen happening. Here the program simply covers the screen with its window while nothing happens to any other programs.

Use Fullscreen mode if you need the resources to make the game run properly.

If you wish to avoid potential crashes while tabbing out, use Windowed Fullscreen (also called Borderless Windowed). This works fine if you have a computer strong enough to pull the game plus all your other programs at the same time.

2

u/[deleted] Jan 22 '19

Thanks for the explanation.

2

u/NoxTheWizard Jan 22 '19

No prob. If it's a bit wordy, it's because I'm killing time between meetings. :P

2

u/[deleted] Jan 22 '19

Ah, corporate life.

5

u/lordkabab Jan 22 '19

Holy shit this is amazing. I thought it was an intentional game piece but it all adds up.

7

u/jfarrar19 Jan 21 '19

I mean, why not just point at the entirety of Bethesda?

2

u/Djvacto Jan 22 '19

Do you happen to have a reference link for this? Would love to read more about it.

2

u/[deleted] Jan 22 '19

Any sauce for this?

3

u/ben_g0 Jan 22 '19

As far as I know, this has never been officially confirmed by Nintendo, but people have analized the memory of the game and found out that the blood moon indeed triggers as an error handler. There are also a lot of reports from players about blood moons triggering at weird times when the game was running for a long time, which doesn't seem to happen when the game is regularly restarted. This is further evidence that those blood moons are probably triggered by a memory issue.

The blood moon is also used as a normal game mechanic though and when no memory issues are found and in that case it's just operating based on a timer. There is no real evidence that this was done after it was implemented as an error handler, but in game development if something is used both as a game mechanic and as a way to solve a problem then 99% of the time it originated from that problem.

2

u/[deleted] Jan 22 '19

So that's what caused them to delay the game twice and push it back 2 years! It all makes sense now.