Asylum Dev Diary 93 – Code Profiling Part 2
After last weeks very successful debug session I wanted to continue looking through the heavy functions and routines to see if there were any others that could be improved.
First up was my player interaction script. This script is attached to objects that the player can perform an action on, be it a door or an item that can be picked up. The script checks to see if the player is within a certain range and if he is the little white dot is displayed. When the player moves the mouse over the dot it changes to the relevant action icon (pick up, examine, use, etc).
The script has been on my mind for a while because I was concerned that it is active a lot on each level and the distance check is performed all the time regardless of where the object is. So, if the level had 50 actionable objects on the map all of those would be checking distance every frame and to me that felt like a potential issue.
This weekend I adjusted it to use a much cheaper initial check to see if the object is actually on the screen. If it isn’t then the subsequent distance check isn’t needed. This small changed didn’t make a massive change to performance but every little helps.
Whilst testing this change I noticed that one of the light casts was popping out of existence as the player moved away. I had another look at the shadow and light code to see what might have caused it. I had added a padding to the detection of the shadows so that it looked at an area larger the screen but this wasn’t the issue.
I worked out that the padding was applied to the wrong part of the script. Rather than checking for shadowcasters slightly outside the view area it should have been the actual light sources. I added in the margins to the light check routine and removed them from the shadowcaster code adn the results surprised me. The framerate had gone up by another 100fps! One part of the first level was now clocking in at 850fps. This must have been because of the reduction in the shadowcaster detection area which originally had the extra padding on it.
With all the light code being debugged and improved I realised that my system of automatically switching lights on and off as they leave the screen is probably no longer needed. It’s going to take a bit of work to remove it but I think it will make working with the lights easier for me and so is worth doing. I’ll need a big block of time so this is being scheduled for my next 5 day code bash.