Organizing Code

Hello! I’m back to my loop/struggle of finding out a proper way or good way of organizing code… I can’t really find any good open source projects, they all seem to have pretty messy unclear code, and most of the time I see people suggest modular coding, whilst all the open source projects usually dont do this. I was curious if you guys have any tips or information on organizing code/scripts, structuring it, etc.

I’ve seen and understood plenty of simple examples but I need something more in depth/advanced, because I run into issues once my code is bigger

4 Likes

Try finding examples from Quenty or Maximum_ADHD. They are pretty good in terms of standards.

4 Likes

everyone has different needs for organization. all depends on what type of games youre making (e.g a round based game will have a much different structure than one on one competitive matchmaking), what types of libraries/frameworks you have, and what type of tools you have to your disposal (e.g file searching and keyword searching throughout your project)

if youre like me and youre lazy and really dont want to concern yourself with file organization you can use a predetermined framework or like the person above me said look at the codebases of solid programmers and replicate their styles

if you dont want my quick and lazy answer tho if i had no predetermined frameworks and were to make like a round based obby racing game i would approximately structure it like this:

+- server
|  +- init.server.luau
|  +- RoundManager.luau
|  +- NoPlayerCollisions.luau
|  +- Datastore.luau
|  +- GhostPlayers.luau
+- client
|  +- init.client.luau
|  +- GhostPlayerVisibility.luau
|  +- ui
|  |  +- init.client.luau
|  |  +- Announcements.client.luau
|  |  +- Store.client.luau
|  |  +- components
|  |  |  +- Label.luau
|  |  |  +- Button.luau
|  |  |  +- Frame.luau
+- common
|  +- Networking.luau
|  +- GlobalState.luau
|  +- library
|  |  +- Logging.luau
|  |  +- Option.luau
|  |  +- Result.luau
|  |  +- Signal.luau
|  |  +- State.luau
|  |  +- UIBuilder.luau
|  |  +- Buffers.luau

in this particular case, since I have a pretty simple game, I also have a pretty simple file structure and don’t really need to stress all too much about folder hierarchy and stuff

since I also don’t have like weapons and character types and other stuff like that, the organization that comes with classes doesn’t apply either

it all depends on what ya maken

theres not exactly a “proper” or “improper” way to organize, and with the nature of programming everyones going to be at least a little bit unsatisfied with other’s codebases, so don’t reach for perfection but rather make it more complex as you grow instead of being paranoid and making it complex from the beginning

5 Likes

Really nice explanation! I was going to say something similar.

1 Like

I wrote several articles on the subject. I have 10 years of industry experience and am in the process of writing a book on system design and architecture.

You can find the articles under my profile or here:

5 Likes

I read through this before, and I really enjoyed it a lot, but I still had some issues with more advancvd things and wasnt sure how to proceed. I had also asked some things in comments but they werent replied to loll… I’m not sure how to go about getting past the issues i had

3 Likes

What are the issues you have had?

3 Likes

It just started feeling kinda messy and unclear, im sure this is user error but im not sure of how to improve from it… I could send you a place file if you want to review it or something. But as my systems/scripts grew it felt more messy and disorganized, and harder to add things to

2 Likes

I did look into studying the concepts more, but honestly I dont think I found good sources, and got kinda confused with it

1 Like

I suggest learning how to correctly use luau static typing. It really helps with large scale applications. Another thing is, learn the SOLID principles, they work wonders.

Writing good software is hard, I read several books on the subject and you may not be ready to grasp some of the advance concepts of system architecture. I say this because at a high level, programming becomes extremely abstract and fragmented. If you did not develop a good foundation you will struggle with the complexities of writing good code.

4 Likes

Here are some books I suggest taking a look at.

2 Likes

Would you be able to do a code review? It’s not too much, but if you have the time it would be amazing. Just to tell me what I’ve done wrong as a push in the right direction, and thank you either way!

3 Likes

Organize your code however you like.

Please do NOT use any architecture that remotely resembles this, for future reference.
Use one script for the server and one script for the client, avoid hardcoding.

2 Likes

unless im mistaken this should be approx. the one server script and one client script file structure just in the format of rojo… if you sync it it should just be one server script and one local script with all the other files in the folder being modulescripts that are children of the respective scripts

i kno not everyone uses rojo so this mighta seemed odd, my b

3 Likes

I also had some issues actually putting it into a game, I understood it for specifics but wasnt sure how to actually execute it for gameplay

1 Like

I understand it a fair bit, and I understand some of SOLID. I was trying to find some more stuff on it but I have the issue of executing the information I got into the actual scripts, like reading your tutorial, it all made sense, and it all seemed good, but I was stumped when trying to actually put that into play for gameplay. It’s easy for certain objects i.e. npcs, objects, etc, but im stumped at more base things like UI, basic gameplay stuff like combat, sprinting, etc

1 Like

Sure, go ahead and send me a friend request on discord.

samdev22

3 Likes

Alright, thank you! Sent a friend request, my user is blu.ee

1 Like

This is roughly what I do every project

Rojo default.project.json file:

{
  "name": "Game-Name,

  "tree": {
    "$className": "DataModel",

    "ReplicatedStorage": {
      "$className": "ReplicatedStorage",

      "Dependencies": {
        "$className": "Folder",
        "$path": "src/shared/Dependencies"
      },

      "Remotes": {
        "$className": "Folder",
        "$path": "src/shared/Remotes"
      },

      "Packages" : {
        "$className": "Folder",
        "$path": "Packages"
      }
    },

    "ServerScriptService": {
      "$className": "ServerScriptService",

      "Scripts": {
        "$className": "Folder",
        "$path": "src/server/Scripts"
      },

      "Dependencies": {
        "$className": "Folder",
        "$path": "src/server/Dependencies"
      }
    },

    "ReplicatedFirst": {
      "$className": "ReplicatedFirst",

      "Scripts": {
        "$className": "Folder",
        "$path": "src/client/Scripts"
      },

      "Dependencies": {
        "$className": "Folder",
        "$path": "src/client/Dependencies"
      },

      "Interface": {
        "$className": "Folder",
        "$path": "src/client/Interface"
      }
    }
  }
}
2 Likes

This is bad practice. Instead, focus on using a few server scripts and a few client scripts to separate systems, and store everything else in modules.