How to Structure Code in Godot?
Over the years I saw many tutorials how to build projects, like multiplayer projects, inventory systems, how to move your character 3D and 2D and so on…
After I started my first project in c++ it was easy to manage folders at first. Then because of time reasons + I am a single developer I moved to Godot because it still opensource, so I can later add or even remove or change things that I don’t like or want to improve, but it was a disaster…
Everything was new. I was way faster than plain c++ with SDL2 but after a while I did find my self doing the same structure as in c++ and it didn’t quiet worked the same, and I was using more and more coding than Nodes(Later you see what i mean by that) and even programmed things that Godot already had, so I clearly needed some structure in my code
But what could you done in that situation?
TIPFirst note to yourself:
Always check if the engine is already providing a solution to your problem before you even start solving it!!!
What can you Structure?
In godot there are two main things you can structure the first one is Nodes, Nodes are the way Godot stores Objects with logic, and you can either use one or build one like your Player Node
The second thing which only comes up for medium size or bigger projects is your folder structure after I started my big 3D multiplayer shooter again in godot it got so messy that I couldn’t find anything in my folders
I think they are two separate problems, so I solve them with two separate chapters
IMPORTANTI won’t explain everything in detail because this should be self-explanatory this course is also meant to be for people who already familiar with Godot and Programming but cant really structure his/her code and because I didn’t find anything online I made this for more advanced Godot Programmers but also not Professionals because they might know an even better solution
I will structure my explanation in notes so you can easily come back, and you don’t need to read though 20 lines to find something specific
Solution to the Node Structure Problem
If you want to make trees like this
Game (Node)└── World (Node3D) ├── Environment (WorldEnvironment) ├── Lighting (DirectionalLight3D) ├── GridMap (GridMap) └── Portals (Area3D) # With this you can move between maps|└── Player (CharacterBody3D) ├── Mesh (MeshInstance3D) ├── Collision (CollisionShape3D) ├── Animations (AnimationPlayer) ├── CameraRig (Node3D) │ └── Camera3D ├── StateMachine (Node) │ └── States (Node) │ ├── Idle (Node) │ ├── Move (Node) │ └── Jump (Node) └── Inventory / Stats / etc. (Optional: Node/Node3D)|└── Enemies (Node3D) ├── Enemy_1 (CharacterBody3D) │ ├── Mesh (MeshInstance3D) │ ├── Collision (CollisionShape3D) │ ├── Animations (AnimationPlayer) │ └── StateMachine (Node) │ └── States (Node) │ ├── Attack (Node) │ ├── Search (Node) │ └── Idle (Node) ├── Enemy_2 (...) └── Spawner (Optional) (Node)|└── NPCs (Node) └── Villager_01 (CharacterBody3D or Node3D) ├── Dialogue (Node) └── InteractionArea (Area3D)|└── UI (CanvasLayer) ├── HUD (Control) ├── DialogueBox (Control) ├── InventoryUI (Control) ├── PauseMenu (Control) └── Quests (Control)|└── Audio (Node) ├── Music (AudioStreamPlayer) └── SFX (AudioStreamPlayer)IMPORTANTA really important thing you see is that everything is a Node and that’s a perfect practice which I can explain for hours, but you just need to think that everything you program could actually be a Node that you can put in your Game and use Godot signals to connect everything together which can help you to safely and easily manage your whole game!
NOTEStateMachine: Where you define your Object into states like idle, move, jump
It’s even better if you use multiple state machines for Movement, WeaponHandling or even one for interactions
CameraRig: It’s better to put your Camera3D into a Node3D and apply code to that like shake, zoom, smooth movement and so on you can even put more cameras if you want to support third-person and first-person at the same time and switch between
It also helps when you want an AntiGunClipping to your Game where your weapon is a “picture” so in your view it won’t go through walls!
For the Main Menu this is as simple as
MainMenu (Control)├── Background (TextureRect)├── TitleLabel (Label)└── MenuContainer (VBoxContainer) ├── PlayButton (Button) ├── ContinueButton (Button) ├── SettingsButton (Button) └── QuitButton (Button)|└── SettingsMenu (Control) ├── Tabs (TabContainer) │ ├── AudioTab (Control) │ ├── VideoTab (Control) │ └── ControlsTab (Control) └── BackButton (Button)here is no right or wrong you can customize it how you like, but the structure needs to remain readable and reasonable you can even add CreditsMenu or whatever you need to extend this
Solution to the folder Structure Problem
res://├── MainMenu/│ ├── MainMenu.tscn│ ├── MainMenu.gd│ ├── UI/│ │ └── StartButton.tscn│ └── Assets/│ ├── Background.png│ └── Music.ogg├── Game/│ ├── Game.tscn│ ├── Game.gd│ ├── World/│ │ ├── World.tscn│ │ ├── World.gd│ │ ├── GridMap.tscn│ │ └── Assets/│ │ ├── TerrainTileset.tres│ │ └── EnvironmentSky.tres│ ├── Player/│ │ ├── Player.tscn│ │ ├── Player.gd│ │ ├── States/│ │ │ ├── Idle.gd│ │ │ └── Move.gd│ │ ├── Assets/│ │ │ ├── PlayerMesh.glb│ │ │ ├── PlayerTexture.png│ │ │ ├── Run.anim│ │ │ └── PlayerStats.tres│ │ └── CameraRig.tscn│ ├── Enemy/│ │ ├── Enemy.tscn│ │ ├── Enemy.gd│ │ ├── States/│ │ │ └── Attack.gd│ │ └── Assets/│ │ ├── EnemyMesh.glb│ │ └── Sound_Attack.wav│ ├── NPC/│ │ ├── NPC.tscn│ │ └── Dialogue.tres│ └── UI/│ ├── HUD.tscn│ ├── HUD.gd│ ├── InventoryUI.tscn│ └── Assets/│ └── Icons/│ └── UITheme.tres├── Common/│ ├── UI/│ │ ├── ButtonLarge.tscn│ │ └── DialogBox.tscn│ ├── Scripts/│ │ ├── StateMachine.gd│ │ ├── Health.gd│ │ ├── SaveLoadSystem.gd│ │ └── EventBus.gd│ ├── Resources/│ │ ├── GenericShader.tres│ │ ├── Crosshair.png│ │ └── BaseStats.tres│ └── Extensions/│ ├── VectorUtils.gd│ └── MathHelpers.gd├── Addons/│ └── (Godot plugins)└── Autoloads/ ├── GameManager.gd ├── InputManager.gd └── Config.gdNOTEAutoloads: these are codes that are in the Globals menu in your Project settings(they can be accessed everywhere in you code)
Common: this is a folder where you put not Game specific things, these are mainly template codes that you can just import like Addons(plugins) to your next project later so you write it once and be there when you need them forever
Structure: the main structure is that you put everything related into one folder like your Player Scene(.tscn) and put logic and resources into subfolders for these scenes. If you have more than 1 script for your scene you could make a folder called scripts and put them in there