The Great Script.rpy Refactoring
Added 2021-03-20 10:56:39 +0000 UTCIt'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!
Comments
And with good functions that have been broken out to their own code, you can then re-use those functions elsewhere. Through good commenting within the code, the actual function is described which allows for examination of it for other purposes, so long as the call and variables are standardized. That should help coding in new stories and to expand on prior functions and their utilization to make sure they fit what you add into the game. Plus, by having those known functions for one purpose, if you find a need for a similar purpose later on, you can copy that system and change the variables going into and out of it. A stable function that works as described is a tool that can be re-used. It is this sort of insight that really brings a good insight into the game development and I appreciate it!
ajacksonian
2021-03-23 14:40:09 +0000 UTCEven guys from patreon talk about uranium... time to sell :D
Adam Król
2021-03-21 08:18:52 +0000 UTCYellowcake Uranium is right, three days is super impressive for a refactoring of this scale, well done!
2021-03-20 16:11:34 +0000 UTCThis kind of information I find fascinating and simultaneously horrifying. It helps me continue to feel better about staying away from straight programming and sticking to other nebulous problems in IT networking and servers. Thanks for putting in the effort to make this game great! I am sure this time investment will pay dividends in the future!
Elmer Fudd
2021-03-20 15:49:05 +0000 UTCWell done dude!
Jababda
2021-03-20 13:17:15 +0000 UTCI'm surprised it took just 3 days. That thing was a MONSTER!
Yellowcake Uranium
2021-03-20 12:59:54 +0000 UTCgrats on completing the refactoring! its a tough but necessary part of every dev cycle, and we dont congratulate the people who do it enough.
Sirlanceabit
2021-03-20 12:07:27 +0000 UTC