Translating OOP practices to Defold

I’ve been learning a lot about object oriented programming over the last year, and there are a bunch of great design patterns and principles that can be used in OOP languages.

But how would I translate that knowledge into something like Defold, where certain things are missing, like interfaces, structs, and static typing? And how would I design a UML diagram for a Defold project?

I know hxdefold exists, and I could use OOP practices there, but I’d like to know how you do this with Lua.

Take a look at middleclass or classic. These simple libraries provide you ability to use classes with OOP style interface, inheritance, etc :+1:

Keep in mind that you also need to interact with Defold API so use OOP in your logic wisely.

2 Likes

While you can of course adopt OOP to Lua I will tell you something else :smiley: : I’m using OOP in my daily job and it suits it very, very well and I like how can I organize everything there, though in Defold and generally in gamedev it is more convenient to use ECS. You can of course stretch Lua to fit OOP approach, but if I were in your shoes, I will try to learn a new approach (which is really comfortable, even for devs used to OOP) :wink:

In fact Lua tables are like objects:
https://www.lua.org/pil/16.html
Think of Lua tables as objects in which you can put everything, including behaviour (functions). They still won’t provide you a direct way to inheritance (but it’s possible), however when you tinker here a little bit you will probably see it’s conveninent - as you can “extend” tables however you like :wink:

Check out also:


6 Likes

very good info until you mention here I didnot notice them yet. thanks @Pawel for the info

1 Like

If you like writing OOP, you may also be interested to know that it possible to use Defold with Haxe: https://github.com/hxdefold/hxdefold

1 Like

Ok @Haath thanks I will check it

I came across this article and find it interesting on how OOP could be implemented (more or less) in Lua:

2 Likes

I don’t think OOP is a panacea. It can result in obfuscated code (OOOP).
I suggest considering a more functional and data driven approach, which I prefer.
Lua modules (and closures) are great for this, I don’t see the attraction of faking OOP.
Basically be in control of your data and avoid side effects.

4 Likes

I’ve been watching an edx CS50 course focused on game development. The course uses the LÖVE game engine and I was hoping to adapt some principles and techniques to Defold because of the common use of Lua. The course starts with Pong and the instructor makes use of a Lua script class. The script can be found in a LÖVE utility named hump ( Helper Utilities for Massive Progression).

Here is the link: https://github.com/vrld/hump. Look for class.lua. To see an example of its usage see this link: https://github.com/games50/pong/tree/master/pong-5.

Definitely!

OOP in Lua might be treated as just a pattern, like any other generic enough functionality is a pattern in other languages (like e.g. implementing own iterator). Lua is better suited for functional programming, but devs used to object oriented programming will be searching for this “way of thinking” in Lua anyway :wink:

1 Like