Pycraft progress update!
Tags: programming, python, gamedev, opensource
Published: 2023-02-15 17:56:42

Hello there everyone, it's time for our weekly progress summary for the week commencing the 6th of February. This week has been a very interesting week, as the work we started this week will go on to set the trend for everything we do in Pycraft going forward. But before we get too into that, let's talk about what else we were up to this week!

We started this week then, as we started last week, by doing a lot of reading and research behind the scenes, as we had decided to begin the rollout of some changes we have been planning for a while behind the scenes. We are of course talking about the switch in some of Pycraft's utility programs to take advantage of the object-oriented approach to solving problems (often abbreviated to OOP, Object-oriented paradigm). This we have already seen to be very useful in theory and had revolutionised the way some utility programs for Pycraft operate.

It's important to note though that not all of Pycraft's source code will be switched over to this approach to programming, this is currently strictly focused on the utility programs for Pycraft, and even then, not all utility programs will need this switch. OOP takes advantage of the concept of objects, which are things that the program uses to interact with to solve problems. Therefore, these changes will be especially advantageous in sections like 'display_utils' which handles the way Pycraft interacts with Pygame's window, and provides additional functionality to it. So this week we have been working on these changes, and we expect this to continue for several weeks still.

Now, for a while, we have been developing Pycraft with the plan to switch from a procedural approach to OOP, and that is why we have included classes in Pycraft for some time. Classes in Python are commonly associated with OOP, although we regularly chose not to use them for their intended purpose. However, on Thursday we discovered Python and the way that classes interact, which would go on to change everything about the way we make Pycraft going forward.

So then, what was this discovery? Well to answer that, we need to talk about how Pycraft handles the transfer of data between programs. Previously this was done through copious use of the 'self' command, which stored the contents of a program we call the 'registry', this program stores the default values of variables used commonly across Pycraft. This worked well, except for two problems: readability and sharing data between programs running in parallel.

However, we thought we would address this in Pycraft v9.5.5 by completely changing the way data gets transferred between programs in Pycraft. To do this, we make a change that would force all utility programs to have parameters for the variables they needed, not just generic 'self', then they would need to return new data that would be transferred back into 'self' by the main program, where 'self' is the contents of the registry. This solved the two problems we faced: readability and the sharing of data between programs. However, this caused problems with scalability. Now, to change the parameters of a section of code, you would need to change all, sometimes hundreds, of references to that section of code. This is a problem.

The solution then is to backtrack on some of the changes we made in Pycraft v9.5.5 (we are by no means undoing everything we did in that update, we are however amending some features we introduced then) so now we are using classes properly, instantiate the registry, but using the updated values, and then adding parameters for everything we can't pass through the registry. This improves the scalability of new features and changes and improves the way data is transferred between processes running in parallel. This also improves readability, as we are significantly shortening the number of lines in Pycraft. Strictly speaking, this is a process from next week (a.k.a the future) but we manage to trim 2,000 lines off the source code for Pycraft in a single day, without changing any of the functionality of the source code we changed!

We hope that you have enjoyed this article, attached below is a preview of one of the sections of code we have applied these changes to, 'achievements.py', the right image shows the original, and the left one shows after we made the changes!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8og79ph7wtktk8e7nz3.png)