I’ve had this really cool single-player game prototype sitting untouched for a few months now, I had originally dropped it because I didn’t feel as if I was skilled enough, but now I have gotten more knowledgeable and feel like I could do better. However, a problem I am facing is that the structure of my game just feels inefficient. Here is an example of what I mean:
The code works, but it just feels inefficient and inconvenient having it all separated into different scripts and parented to different things. I’ve looked into multiple uncopylocked games, and I’ve noticed that it tends to be mostly modulescripts, with one or two localscripts/serverscripts to require the modules. I was wondering how to accomplish a structure like that. I had tried something like this:
where each module performed a different core player function, and then the client module looped through all of them and initiated them, but it didn’t work, and it just felt incorrect.
I’m not exactly looking for a direct answer that solves my problem completely, but does anyone have any tips for accomplishing a code structure like this that could lead me down the right path? Thanks.
You appear to be doing it correctly within the Second Image.
This is generally the better method to organize your code into classes or specific data storage for specific Events of your game
This would be the correct path to follow, Its better to do this so:
-
You stay motivated on Projects
Makes it less of a task to look for something than having to look through many scripts. -
More Advanced Usage
this can be used to more Advanced forms or Programming. -
Orgnization
To make your code less like a Messy Drawer or Inventory and more like a functional Office Space, so you can easily find your stuff and not go insane
(This is Connected to 1)
This isn’t true, The code can be very efficient, It just depends on what you are doing with your code like:
-
Specific Methods/Functions
A function you’re using can be Deprecated and was replaced by a newer and faster Method. -
Specific Syntax
The Specific order of Code may take more time than a shortcut that simplifies your code, like for example:
if Number > 100 then -- This is Horrible! Ewwwwww
Number = 100
elseif Number < 0 then
Number = 0
end
-- Better Method:
Number = math.clamp(Number, 0, 100) -- Way better and more cleaner!
you know those kinds of things
People tend to use ModuleScripts
as a way to Store Data for specific tasks or to Follow the DRY Principle, which means Don’t Repeat Yourself, An Example of this is when you have multiple scipts that have the same function, you would have to update them all if you make a change, with ModuleScripts
you can have one function for all these functions, and you can update them from one area instead of hundreds, This can be used for stuff like OOP (Object Oriented Programming) to make your code work easier around stuff.
The “inconvenience” is likely just you, You probably think your code Organization looks ugly but I can assure you, Its Normal with Developers.
There is no “Correct Answer” here, It just a matter of how you want to Organize your Code.
I have written many code structure tutorials, I’m a software engineer and system architect at the professional level. If you need help add me on discord, I can lead you in the right direction.
My discord tag is as follows: Country Coder#2633
Module scripts are particularly useful when you need to pass variables between different scripts.
They’re also useful for using the same functions in different scripts.
Here’s and example of using the same “Mana” resource for something like spells:
Scripts used:
FireballScript:
MagicMissileScript:
ManaModuleScript:
The FireballScript and MagicMissileScript are both able to access the shared functions and variables used in the ManaModuleScript.
If you’re interested, I made an article on the SOLID principles.
It’s long but it teaches how to use the science of code structure to increase productivity and readability.
This is a very in-depth and useful post. Thank you! I will look at it more later.