That Time My Game Broke (and How I Fixed It)
RFYL_BackupJune25 - 89% Saved...
RFYL_BackupJune25 - 100% Saved
I wanted to jot down some problems I had this past week and how I went about fixing them. I'm still barely a hobbyist coder, but if any of these insights are helpful, OR if you notice things which can be further improved upon, I'm all ears!
The first part of this post is just a diary-like reporting of the A to B to C, scroll past for a shortened list of fixes and steps that helped me - your mileage may vary. This is all probably super obvious to any programmer worth their salt, but that does not describe me, and maybe it doesn't quite describe you either.
Last week I was finishing up some polish on my game, Run For Your Life, in preparation for a Demo release. Some YouTube channels were set to produce content / stream the game, and I was going to finally create a Steam store-page. This was going to be an early push to try to get more people interested in the project, help with my motivation, and just overall progress my game one step closer on it's path. I was finishing up a resolution-scaled GUI, my game was running at beautiful speeds in the editor, everything was awesome.
Then I got 30fps in the editor. Not much better in the build. All I had done was adjust some canvas properties, how the **** could this have crashed my game SO hard?! I tried resetting the GUI properties. Nothing. I tried simplifying the GUI, and then just disabling it. Nada. I felt a massive drop in my stomach when nothing I could think of would get the game running back to where it was, literally 15 minutes ago.
Hmm. Well, it's extremely hot in my city right now, not to mention humid beyond belief. I've noticed some performance issues like this before, and it's what prompted me to buy a cooling pad for my laptop. I could also use a clean-up of the system, I figured. So I got some utilities like SpeedFan and used resmon to check out speeds and temperatures. Definitely running a bit hot, okay - let's just put this away for a few days, try not to freak out, I'm sure it's fine. So I spent a few days away from the laptop entirely and just chilled. Pretty good time, aside from the lingering fear that somehow I'd completely trashed my project , and in such a way that I was unable to resolve.
A few days later, and a few degrees cooler, I loaded the project up, to find - this thing runs like garbage. I spent the next few hours / days going through all of my code and trying my damndest to revert things to what I thought things were before the crisis. None of this helped. I went to my most recent back-up, June 5th.. and similar issues existed. I make a point of doing backups once the current issues are handled, and performance is running well. I've ALSO fallen out of the habit of backing up with any regularity - the most recent backup before this point was May 14th, well before a lot of major features and additions to the project. I felt completely defeated and honestly, kind of nauseous. I've been working like a maniac on something I've wanted to do for a long time, managed to get things working really well - only to find "nope, it's all trash".
I tried doing things like loading the May 14th backup, and still found it to be jankier than I remember. Not to mention,t he amount of work I'd lost. If I had to proceed from this point forward, so be it, but something just didn't fit right. Right around here, I decided to look at the laptop itself yet again. Maybe there was some new hardware failing I could address. That's when I noticed this:
There were several posts about whether or not it SHOULD show 100%, but for it to consistently stay at such a low %, even when the system was being fully taxed, I figured it couldn't hurt to look into. I did a clean boot and a registry edit to get this up to 100%, even just to test, and sure enough, a lot of my problems dissipated. I can't recommend anyone do this, only that it helped me and it may be something to read into. Running at max frequency can be bad for your CPU, and in the case of a laptop can contribute to overheating. YMMV. I also opened up the laptop to remove any dust or particles clogging the fan - general upkeep anyone should exercise, with caution and care.
Okay so that should be it right? Well no, things were still not running at peak performance, despite the significant boost I achieved by adjusting the CPU speed. During my mad panic state, simultaneous to troubleshooting my PC, I was also looking at any CPU optimization tips for within Unity. Here's a few pieces of code-fix that, again, have helped me, and may be worth looking into:
1) Adjusted my coroutines:
Instead of constantly calling a time-wait in my (many) coroutines such as: "yield return new WaitForSeconds(5f)", instead, declare the WFS at the start of the script., ie:
public class ExampleScript : Monobehaviour
{
WaitForSeconds delay;
void Start()
{
delay = new WaitForSeconds(5f);
}
IEnumerator Coroutine()
{
doStuff;
yield return delay;
}
2) OnBecameVisible:
A lot of animation, rendering, and general game-feel things happen within my scripts, such as a door shaking when it's hit by a player or enemy, or it's shader changes when damaged, etc etc. By making use of void OnBecameVisible and a bool isVisible, I was able to remove a lot of work being done off-screen. The camera in Unity automatically culls renderers, but if you have any physics related code happening away from the player, I highly suggest looking at ways to disable these functions when off-screen. It will vary for everyone's needs - I couldn't disable my enemies from being able to attack things while offscreen because this would have significant impact on the gameplay, for example. Particle systems are not auto-disabled or stopped by the camera's culling, etc etc.
3) Continuous vs Discrete Collision Detection
RFYL has A LOT of props, as it's one of the foundational aspects of the game. For whatever reason, I had set a good amount of these to detect collisions via Continuous detection. By simply changing all the props to Discrete, I noticed no obvious impact on gameplay, and another significant boost in fps. I probably set them to continuous based off of an article, where it was required for that developer. Always test things and question decisions,about everything.
4) Move things out of FixedUpdate
This is something I've always been fairly cognizant of, so I was surprised to see how much I had running in FixedUpdate that didn't need to be there. Things slip past you, or maybe there was a good reason at one point. I was able to adjust a few scripts, and will continue to monitor gameplay to see if perhaps they really need to be in there. Just something to keep in mind.
5) Be more diligent about backing-up stable versions of your project
Absolute no-brainer. I was doing this almost daily, but the complacency hits hard. One of the simplest ways to prevent forest fires.
The performance of RFYL is not quite what it once was, but it's not in the toilet-terrible-everything is ruined state that it was 24 hours ago. My only explanation for this is some kind of cognitive biasing, it probably wasn't ACTUALLY running as fantastic as I remember, but given the sharp drop in performance, the memory looks god-tier by comparison. But it's working well enough to proceed. I don't have to lay in bed feeling like I've wasted 5 months with nothing in hand. I wish you the best and hope you never run into this kind of feeling - but if you do, keep your cool, take a break, refocus and go piece by piece. My plans were delayed, but a delay beats the hell out of a total critical failure. Thanks for reading!
Get Run For Your Life
Run For Your Life
Run. Smash. Hide.
More posts
- Run For Your Life - 6 Month Alpha Release!Jul 28, 2020
- Time-Gated Music Loops!May 30, 2020
- BIG HOLE updateApr 14, 2020
- Some Fancy ScreenshotsApr 11, 2020
Leave a comment
Log in with itch.io to leave a comment.