The Great Script.rpy Refactoring (Patreon)
Content
It's been a little longer since my last dev post than I normally like, so let's fill you in on what I've been working on!
A few days ago I finished the first iteration of the Clarity system. With the core done I wanted to take some time and work on something else. A lot of you had good ideas for tweaks to the system or foresaw potential problems, and I wanted to turn it all over in my head. I decided to spend that time doing every programmers favourite task: refactoring.
For anyone not familiar with the term, refactoring is revisiting old code to update and tidy it. It's an important part of the software development cycle and critical for cutting down on "spaghetti code" where everything is held together by duct tape and string. It's also the least exciting part of the job, which is why it's often not done as often as needed.
My goal was to refactor the entirety of the script.rpy file. This is the default starter file of a Ren'py project, and over the length of LR2's development has been the standard place I wrote new classes, functions, screens, styles. If it wasn't narration and dialogue, it went into script.rpy. It's no surprise then that script.rpy was a big file, 12k lines of code big. For comparison, all of narration, description, code, and dialogue for Jennifer's special events and all conversations is a total of 4k (And spread over two files at the moment).
I thought this refactoring was going to be a quick one day project, but it ended up taking three (thus this post being later than I intended). script.rpy now only contains core Ren'py build information and start-of-game initialization information. All of the python classes have been split into their own files, which are then arranged in folders based on the purpose of class (ie. clothing system, business related, character related, ect.). All of the game screens, responsible for placing visual elements like buttons and text on the screen, have also been split into their own files and sorted by folders. Styles, transform definition, helper python functions, have all been moved into their own files. So on and so forth.
At the end of all this refactoring script.rpy is down from it's chonky 12k lines of code to a trim and proper 830 lines. From this one file I split out:
- 28 python classes
- 29 Ren'py screens
- 8 helper function files (containing ~30 functions for organising lists, formatting text, ect)
None of this changes how the code functions, but it will have a massive impact on my ability to develop well organized, easily maintained code in the future. It should also make the LR2 code base much easier to understand and browse through for modders (and those of you who are just curious how the magic happens), which is always a goal of mine.
Now it's back to refining the Clarity system and getting it ready for it's release at the end of the month!