Noob OOP questions

I’ve been making my first app for a few months and at this point it pretty much works, but the thought of adding to it seems like a nightmare. After watching some vids I’ve learned I programmed in the procedural style, and a lot of the spaghetti-ness of the code could potentially be solved by learning OOP. Of course some of that could probably be solved by better coding practices period, nonetheless, I’ve been exploring OOP and the logic really speaks to me. So I decided to make some classes/objects in Solar2d and I have a couple questions, not specific code questions, but more big picture things that I’m struggling with.

My biggest gap in knowledge, is how are objects supposed to communicate with one another? I’ve read about getting and setting, but I’m still missing a piece. It’s my understanding that objects are supposed to be modular, so perhaps I’m taking it too far, but is it wrong to build in methods that directly reference other objects and their methods? Or is the logic supposed to happen outside of that, where outside of the object I ‘get’ some info from one object and then pass it to another object to ‘set’? Maybe without the specifics of what I’m making these questions are too vague, I can give examples of how it relates to my app if necessary.

Also, I’m struggling with the idea of classes regarding display objects. If I make a class out of say, a rectangle, should that rectangle be visible? This may sound silly, but I read that the class is supposed to be a template in a sense, and the instances are the real thing. Does this mean I should leave the display part out and just include the methods, and then create the rectangle during instantiation?

I’ve done searches in the forum, and I’ve found some resources, but a lot of them are broken links that don’t work anymore, and a lot of them are getting started type posts, and many are ‘here is how you do OOP in Lua so now you can apply all the OOP stuff you already fully understand’. If you have any references, even in a different language, that would be helpful, or just comments to help me gain some understanding. Thanks!

Well, this all really really depends on your particular project.

As Solar2D projects are generally event driven, i.e. the user touches something, an object collides with another object, a timer runs out, etc. you can get by with either type of programming. Often the procedural style is simply easier and more fitting for the purpose and by trying to twist things into OOP, you’ll just make things harder for yourself.

When it comes to programming, the most important thing is having a clear vision of what you are trying to accomplish as this helps you with writing modular code, avoiding spaghetti code, as well as knowing where procedural programming and OOP would best be applied. If you don’t have a vision and a plan of what you are doing and how, then OOP can lead to spaghetti code just as easily.

If you consider Solar2D’s display API, then that’s pretty much OOP all the way. If you create a rectangle, it’ll automatically come with a host of useful methods, like :scale(), :setFillColor(), :removeSelf(), :translate(), etc. Since display objects in Solar2D are just fancy Lua tables, you could add whatever you need to them at a later point.

I would recommend providing some more concrete examples of what you are doing and what you are trying to accomplish. For instance, there are lots of ways to make objects communicate with one another, but what is it exactly that you need them to do? Why do you believe that you need OOP?

You can also read through this tutorial as it explains OOP in Lua quite well: https://www.lua.org/pil/16.html

2 Likes

Usually we use this library to mimic the OOP, you can use it directly, but at some point it would be good that you read the documentation that @XeduR gave you to understand what is actually happening.

You are going to make a lot of mistakes before achieving a deep understanding of this and the other paradigms, so just do it, read a lot, code a lot, try different things and sooner or later you will be achieving it, just don’t be afraid of being wrong. (actually, there is no “wrong”. If it works… it works! from then on there is only the “better”.

I really recommend you to read this book. I’m sure it will give you a good overview of what kind of problems you can face and how can you solve them. Are there perfect solutions?

I think everything else was already said in the comment above.

Hope this has helped you in some way :slight_smile:

I’ve taken a look at the library and the book, thank you for your suggestions and encouragement!

Thank you so much for your reply. Everything you’re saying makes perfect sense. Now that my app is working I see a couple parts that may work better with OOP style. Also, I didn’t have a vision when I started. I had a loose idea, but there were many questions marks regarding specifics. At some point I realized I wanted to be able to save state, and going back in and adding that near the end I think is what added to the spaghetti.

So I think for round 2, I’m going to try rewriting certain parts to see if I can make it more manageable. Thank you for all your suggestions!!