DEV Community

Cover image for 5 WAYS TO ORGANIZE YOUR C# CODES IN GODOT
Emmanuel Pangan
Emmanuel Pangan

Posted on

5 WAYS TO ORGANIZE YOUR C# CODES IN GODOT


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.

ui folder

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.

scene folder

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.

root 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.

levelstarter and gamemanager

FYI, here’s my current folder structure of my game. And, folder structure is also used in the web industry, not just in gamedev.

my folder structure

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 { }
}
Enter fullscreen mode Exit fullscreen mode

And if you’re using the newer version of C#, you can do it like this.

namespace YourNamespaceHere;

public class YourClassHere { }
Enter fullscreen mode Exit fullscreen mode

This is why in your first programming classes, you call the System namespace in your:

System.Console.WriteLine("Hello World");
Enter fullscreen mode Exit fullscreen mode

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 { }
Enter fullscreen mode Exit fullscreen mode

After

public class SceneManager
{
    public class LevelData { }
}
Enter fullscreen mode Exit fullscreen mode

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();
    }    
}
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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}");
}
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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)