close
close

5 WAYS TO ORGANIZE YOUR C# CODES IN GODOT




Watch the full video on YouTube.

Here are the five things I usually use in my game project, King and Queen, and you can do that, if you want to organize your codes in Godot… or maybe not. Okay, let’s get started!

1️⃣ USE FOLDERS

You should use folders to organize your codes, as this will separate each individual script in your game projects. Okay, let’s take a look at my game project.

ui-folder

Here, the UI folder only contains all the UI scripts I use. No GameManagers or SceneManagers or similar included. Just pure UI scripts that control the flow of my buttons, texts and other UI tasks. So is this Scene folder contains only the SceneManager as this script is only used to manage my scenes and nothing else.

scenemap

Folders allow you to identify which scripts are related and group them relatively. What if you want to break that rule?

Well, of course you can! Nobody stops you. Just like I did in my Root folder, we see that my GameManager And LevelStarter are located in the same folder.

root folder

And when we inspect them, they have nothing in common. GameManager is just to maintain the status of the game. And LevelStarter calls up the events when a level starts.

level starter and game manager

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

my folder structure

2️⃣ USE OF NAME SPACES

The use of namespaces is similar to folders. The only difference? Well, you can type the namespaces into your codes. So, similar to our folder structure, we can group our scripts with this syntax.

namespace YourNamespaceHere
{
        public class YourClassHere { }
}
Go to full screen mode

Exit full screen mode

And if you’re using the newer version of C#, here’s how to do it.

namespace YourNamespaceHere;

public class YourClassHere { }
Go to full screen mode

Exit full screen mode

This is why in your first programming lessons you should use the System namespace in your:

System.Console.WriteLine("Hello World");
Go to full screen mode

Exit full screen mode

Because the Console class is in that namespace.

Finally, the nice thing about this namespace is that you can create a library of classes. And it’s generally a good idea to follow the folder structure for your namespaces as well. Okay, next one!

3️⃣ LESSONS AND FEATURES

We already know what classes and functions are in programming, right? Or I hope you know. Now we can take this further.

LESSONS

If you want to raise a class, we can actually take this class and put it in the classroom SceneManager class.

For

public class LevelData { }

public class SceneManager { }
Go to full screen mode

Exit full screen mode

After

public class SceneManager
{
    public class LevelData { }
}
Go to full screen mode

Exit full screen 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 somewhere in our code, let’s say in the UIDebugger script.

public class UIDebugger
{
    public override void _Ready()
    {
        SceneManager.LevelData levelData = new();
    }    
}
Go to full screen mode

Exit full screen mode

We have to start with our parent class viz SceneManager to call LevelData. And please note your access modifier, this one public And private keywords.

FEATURES

For the functions, we can actually place another function here, within our existing function. Just like I raised my mother Shuffle function SetCloudFrequency(). This is only useful if you want to call that function in that parent function. Because this child function is created in here. If you want to know more about this CloudGenerator script, you can see my previous devlog here.


4️⃣ REGIONS

I actually use this #region a ton in my gaming projects. And this is why. Regions can help you organize which functions and fields are related with a comment. To use them, type it like this:

#region
// The rest of the codes.
Go to full screen mode

Exit full screen mode

It doesn’t even matter if there are spaces in it or not. Then go to the end of the functions and fields of your choice to group and type:

#region
// The rest of the codes.
#endregion
Go to full screen mode

Exit full screen mode

Now we can collapse or collapse again this group of functions and fields that we just grouped. Now, on to the last one.

5️⃣ BRACKETS

We see them all in our classes, functions and if statements, etc., but we can take this even further and use them in code grouping as well. Just type these closed and open curly brackets and you’re done!

{
    int apples = 1;
    int oranges = 2;
    int total = apple + oranges;
    GD.Print($"Total: {total}");
}
Go to full screen mode

Exit full screen mode

You can collapse or collapse these codes, just like regions. This is the most important part. As with our number 3 classes and functions, you cannot call this variable or function outside these brackets because they are created inside these brackets.

{
    int apples = 1;
    int oranges = 2;
    int total = apple + oranges;
}
GD.Print($"Total: {total}"); // This throws an error.
Go to full screen mode

Exit full screen mode

So you can only use it as follows:

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;
}
Go to full screen mode

Exit full screen mode

Now it’s up to you whether you want to organize your codes this way or not. It is 100% COMPLETELY OPTIONAL to do this!

Thanks for reading! 📖

Creating these types of free programming blogs takes a lot of time planning, writing, and editing. You can help me with support by: Buying me a Ko-fi ☕.

Check out my YouTube channel for more coding tutorials. Happy coding everyone! 🧡