Tips on avoiding spaghetti code and building a modular code system

My games current codebase is rather messy and I plan on rewriting it at some point to be heavily modular.

So, any tips for…

  • Designing a module-based infrastructure

  • Avoiding spaghetti code in all areas of programming

  • And so on

Thanks

5 Likes

Take a look into software design patterns, software architecture and software engineering principles. When you plan out the structure of your game for specifics (for example, MVVC is great for testing) you can usually end up with a pretty good result.

2 Likes

Got any links or reading material?

This should be a good overview of MVC, which I’d suggest looking at:
https://blog.codinghorror.com/understanding-model-view-controller/

The idea is that you’ll split everything into Models, Views, and Controllers, and that link there gives a good distinction between them. But the core idea is that you’re separating your game logic from Roblox logic, so everything ends up being cleaner.

I was looking for stuff for the design patterns / software engineering principles but didn’t see much. But I think MVC should be enough to give you a good basis for your game.

4 Likes

There is one site I know for learning programming design patterns, and it’s from the perspective of a game developer, so it may be useful: http://www.gameprogrammingpatterns.com/
The web version of the book is a bit further down in the page.

4 Likes

One very easy trick is to plan everything out ahead of time. It seems very simple but if you know want you need to do and how to do it, it’ll be easier to create that system and other systems to work around it.

1 Like

You can make your own custom classes using object-oriented programming which will improve the code’s maintainability and re-usability, I taught myself OOP in two days off of this tutorial here its really good:
http://lua-users.org/wiki/ObjectOrientationTutorial

if you don’t know about the index and call metamethods there are chapters on those as well.

To add to this (for anyone revisting this topic) - MVC is great but if it’s a large (or frankly even small) codebase it may be worth choosing or making a framework such as AGF. My personal favourite is Knit.

In the two, nearly three, years since this topic, I’ve developed a fairly powerful codebase. I use Knit for client/server communication, and then have controllers for the client-side business logic, and a controller that connects to Roact for building the UI components (which then relay events/etc to other controllers).

When it comes to avoiding spaghetti, that’s more on writing good code and using consistant styleguides.

2 Likes