Watch the full video on YouTube.
Here are the five things that I usually use to my game project, King and Queen, and you can too, if you want to organize your codes in Godot... or maybe not. Alright, let’s begin!
1️⃣ USE FOLDERS
You should be using folders to organize your codes as it separates each individual script in your game projects. OK, let’s take a look at my game project.
Here, the UI
folder only holds all of the UI scripts that I use. No GameManagers
or SceneManagers
or anything like that included. Just pure UI scripts that controls the flow of my buttons, texts, and other user-interface tasks. Similarly, this Scene
folder only holds the SceneManager
as this script is only use to manage my scenes and nothing else.
Folders helps you to identify what scripts are related and group them relatively. Now, what if you want to break that rule?
Well, of course you can! Nobody’s stopping you. Just like I did in my Root
folder, we see that my GameManager
and LevelStarter
are in the same folder.
And if we inspect them, they don’t have anything in common. GameManager
is just to hold the states of the game. And LevelStarter
calls the events when a level starts.
FYI, here’s my current folder structure of my game. And, folder structure is also used in the web industry, not just in gamedev.
2️⃣ USING OF NAMESPACES
Using of namespaces is similar to the folders. The only difference? Well, you can type the namespaces in your codes. So, similar to our folder structure, we can group our scripts with this syntax.
namespace YourNamespaceHere
{
public class YourClassHere { }
}
And if you’re using the newer version of C#, you can do it like this.
namespace YourNamespaceHere;
public class YourClassHere { }
This is why in your first programming classes, you call the System
namespace in your:
System.Console.WriteLine("Hello World");
Because the Console
class is inside that namespace.
Lastly, the cool thing about this namespace is that you can create a library of classes. And it is generally a good idea to follow the folder structure for your namespaces as well. Alright, next!
3️⃣ CLASSES AND FUNCTIONS
We already know what classes and functions are in programming, right? Or, I hope you know. Now, we can take this further.
CLASSES
If you want to parent a class, we can actually, take this class, and put it inside the SceneManager
class.
Before
public class LevelData { }
public class SceneManager { }
After
public class SceneManager
{
public class LevelData { }
}
Now if we play this game, we can see that it works (watch the video). The only thing is, if we want to call this LevelData
anywhere in our code, let’s say in the UIDebugger
script.
public class UIDebugger
{
public override void _Ready()
{
SceneManager.LevelData levelData = new();
}
}
We must start with our parent class which is SceneManager
to call the LevelData
. And please, keep in mind about your access modifier, these public
and private
keywords.
FUNCTIONS
Now, for the functions, we can actually put an another function here, inside our existing function. Just like how I parented my Shuffle
function in SetCloudFrequency()
. This is useful only when you want to call that function in that parent function. Because this child function is created inside this. If you want to know more about this CloudGenerator
script, you can check my previous devlog here.
4️⃣ REGIONS
I actually use this #region
a ton in my game projects. And this is why. Regions can help you to organize what functions and fields are related to each other with a comment. To use them, you can type it like this,
#region Your Region Name
// The rest of the codes.
doesn’t even matter if it has spaces or not. Then go to the end of the functions and fields of your choice to group and type:
#region Your Region Name
// The rest of the codes.
#endregion
Now, we can collapse or un-collapse this group of functions and fields we just grouped. Now, onto the last one.
5️⃣ BRACKETS
We all see them in our classes, functions, and if-statements, and etc., but we can actually take this further and use them in grouping codes as well. Just type this close and open curly brackets and you’re done!
{
int apples = 1;
int oranges = 2;
int total = apple + oranges;
GD.Print($"Total: {total}");
}
You can collapse or un-collapse these codes as well like regions. Now, this is the important part. Similar to our number 3 classes and functions, you can’t call this variable or function outside of these brackets, because, well, they are created inside these brackets,
{
int apples = 1;
int oranges = 2;
int total = apple + oranges;
}
GD.Print($"Total: {total}"); // This throws an error.
So you can only use it like this:
public override void _Ready()
{
{
int totalFruits = TotalFruits(apples: 1, oranges: 2);
GD.Print($"Total Fruits: {totalFruits}");
}
int myFruits = TotalFruits(apples: 5, oranges: 10);
GD.Print($"My Fruits: {myFruits}");
}
private int TotalFruits(int apples, int oranges)
{
int total = apples + oranges;
return total;
}
Now it’s up to you if you want to organize your codes like this or not. It's 100% COMPLETELY OPTIONAL to do so!
Thanks for reading! 📖
Making free programming blogs like this takes a long time of planning, writing, and editing. You can help me through support by: Buying me a Ko-fi ☕.
Check out my YouTube channel for more coding tutorials. Happy coding everyone! 🧡
Top comments (0)