This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
How We Used Object Oriented Programming #69
carsakiller
announced in
Dev Thoughts
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Introduction
Hi there!
In this post, I want to talk about how we used Object Oriented Programming (OOP) patterns to make our lives much easier. While it may seem complex if you are unfamiliar, it definitely helped us a lot with code readability and speeding up feature implementation.
Planning
Originally, we were planning on taking a more functional approach to Carsa's Commands v2, as was done in v1. However, it can become quite messy when there are functions everywhere and things are not organized. Since we are dealing with data like players, vehicles, and roles, I figured it would be a good idea to try and turn them into objects so that we could have a
Player
object that can then contain methods likePlayer.equip()
. This should help with organizing and keeping all of the functions I need for players within thePlayer
object scope.In order to properly implement OOP with classes in Lua, you can use metatables, unfortunately, we cannot access metatables in the Stormworks implementation of Lua. Luckily, tables are themselves objects in Lua and allow for us to implement a sort of OOP in Lua.
Our Implementation
Now, let's take a look at how we implemented OOP using tables and started our second rewrite for Carsa's Commands v2.
The Role "Class"
First, lets look at our
Role
"class"The first thing to note is the line
This is creating a table for us that will contain all of the values and methods of our role objects. All objects we create later will contain everything that this
Role
table contains.Next, we have a constructor function that is inside the
Role
table. This should look similar to how a constructor looks in other languages such as JavaScript. We are going to call this function when we want to create a new role. This constructor uses theself
keyword to assign some values to this role such asname
andactive
, which are passed in to the function as argumentsor
have default values.Finally, the constructor calls a
save()
method onself
which is used for saving the object intog_savedata.roles
so the data can persist.We will get to what the
serialize()
function later.Creating a Role Object
Now that we have the blueprint for a role, we need to actually create a role object. For this, we created a
new()
function, heavily inspired by JavaScript'snew
operator.This function allows us to create a new object from a class like
Role
. All it really does is copy all functions from the class and set theself
argument as a new empty table. It then calls the constructor function on the new object, passing the additional arguments and returns the new, now initialized, object.It can be used like this:
This creates a new role called
Admin
that is inactive but hasadmin
andauth
permissions.Saving An Object to g_savedata
When saving our object to
g_savedata
, we will want to remove the functions from the object as there is no need to include them. We just need to save thenumber
,string
, etc. values.serialize()
does this by creating a new table that only contains non-function values from the passed object and then returning it.Reading An Object From g_savedata
Now there is the problem that when we go to read our role from
g_savedata
, all of it's methods are missing. Now we have to deserialize our data and re-initialize our objects. We do this inonCreate()
, where we get all of our data fromg_savedata
.G_roles.create()
is a function in aRoles
object that contains a list of all of the roles. AllG_roles.create()
really does is callnew()
with the data it is passed (like we did above) and then adds it to it'sroles
table.Conclusion
While using OOP for Carsa's Commands may have taken up more characters, and took a long time to rewrite everything, it was definitely worth it. It allowed us to keep everything much more organized and allowed us to implement new features quickly. I definitely recommend you give it a try.
Beta Was this translation helpful? Give feedback.
All reactions