Godot: code architecture¶
How to structure scenes, nodes and scripts so that the game stays easy to change: scene design and communication, scripting languages and style, input and UI layout, multiplayer, and threads. Engine-independent patterns, such as components, fixed time steps and authoritative servers, are in Game development.
Entries are sorted by score from 1 to 10 (criteria): 9โ10 start here or strongly recommended, 6โ8 recommended, 1โ5 further reading or conditional.
Adopted references¶
- 8 GDScript style guide (Godot docs): formatting, naming conventions and code order.
- 7 Static typing in GDScript (Godot docs): typed variables and functions, and the warnings that help you keep code typed.
- 7 High-level multiplayer (Godot docs): peers and connections, RPCs with their transfer modes and channels, authentication, and secure multiplayer design.
- 7 Optimization using Servers (Godot docs): bypassing the scene tree and driving the rendering, physics and audio servers directly through RIDs, for tens of thousands of objects once other optimisations are exhausted.
Key rules¶
Scenes and nodes¶
- 8
GODOT-3Design scenes to work on their own: keep what a scene needs inside it, and let the parent supply any outside dependency. The parent can connect to the scene's signals or set its properties, Callables and node references. (Godot, Scene organization) - 7
GODOT-4Let parents call methods on and set properties of their children, and let children report what happened through signals. Siblings communicate through a common ancestor. Name signals with past-tense verbs, such asitem_collected. (Godot, Scene organization, Using signals) - 7
GODOT-5Use autoloads only for broad-scoped systems that manage their own data, such as a quest or dialogue system. Share helper functions withstatic funcin aclass_namescript, and share data with a custom Resource. (Godot, Autoloads versus regular nodes) - 6
GODOT-6Reference other nodes through@exportproperties or scene-unique names (%Name), not long hard-coded node paths. These references keep working when nodes move in the scene tree. (Godot interfaces, Scene unique nodes) - 5
GODOT-7Make game-specific concepts scenes, and use named script classes for reusable tools shared across projects. (Godot, When to use scenes versus scripts) - 5
GODOT-8UseRefCountedorResourceclasses instead of nodes for data and logic that don't need to be in the scene tree. (Godot, When and how to avoid using nodes for everything)
Scripts¶
- 7
GODOT-21Choose the scripting languages early: GDScript suits most game code, C# suits teams that already know it, as long as you don't ship to the web, and C++ through GDExtension suits performance-critical algorithms. You can mix them in one project. Godot 4 can't export C# projects to the web, its C# support on Android and iOS is experimental, and C# code can pause for garbage collection. (Godot, Scripting languages, C# basics, CPU optimization: languages, About godot-cpp) - 7
GODOT-9Use static typing throughout, and set theuntyped_declarationwarning to Error so that untyped declarations stop a script from compiling. Typed code catches more errors before it runs, and it runs faster. (Godot, Static typing in GDScript, GDScript warning system) - 7
GODOT-11Move objects and run game logic in_physics_process, and turn on physics interpolation so that motion stays smooth at any frame rate. Use_processonly for work that must follow the rendered frame rate. (Godot, Using physics interpolation, Godot notifications, game development: fixed time steps) - 6
GODOT-10Follow the GDScript style guide, including its code order. Consistency within the team matters more than any single convention. (Godot, GDScript style guide)
Input and UI¶
- 7
GODOT-22Read player input through actions defined in the Input Map, not specific keys or buttons, and handle gameplay input events in_unhandled_input()rather than_input(), so that UI controls receive them first. One action can map to several keys and gamepad buttons, and you can build runtime rebinding on top of the Input Map (GAMEDEV-18). (Godot, Using InputEvent, InputMap) - 6
GODOT-23Set the base window size, stretch mode and stretch aspect at the start, following the docs' recommended settings for your kind of game, and lay out UI with anchors and Container nodes so that it adapts to every resolution and aspect ratio. Games that aren't pixel art usually take thecanvas_itemsstretch mode and theexpandaspect, the default for projects created in Godot 4.7 and later; pixel-art games takeviewportwith integer scaling. (Godot, Multiple resolutions: common use case scenarios, Using Containers)
Accessibility¶
- 5
GODOT-40Give each Control that a screen reader should announce a human-readableaccessibility_name, and anaccessibility_descriptionwhere the name isn't enough, keep the Accessibility Support project setting at its default, Auto, and test with a screen reader on each desktop platform you ship. Godot 4.5 added screen reader support through AccessKit on Linux, macOS and Windows, and its release notes call it experimental.DisplayServer.accessibility_should_reduce_animation()andaccessibility_should_increase_contrast()report the matching operating system settings where the platform supports them. This appliesA11Y-9andA11Y-12. (Godot 4.5 release notes, Screen reader support, Godot, Control: accessibility_name, Godot, ProjectSettings: accessibility_support, Godot, DisplayServer: FEATURE_ACCESSIBILITY_SCREEN_READER)
Multiplayer and threads¶
- 7
GODOT-24In multiplayer games, replicate spawned scenes withMultiplayerSpawnerand properties withMultiplayerSynchronizer, keep the server the multiplayer authority over gameplay state, and give each player authority only over a child node that sends their input. Validate the arguments of every RPC that clients may call (any_peer) before applying them, usingmultiplayer.get_remote_sender_id()to know who sent it. This appliesGAMEDEV-5. (Godot, High-level multiplayer: secure multiplayer design, MultiplayerSpawner, MultiplayerSynchronizer, Multiplayer in Godot 4.0: Scene Replication) - 6
GODOT-25Don't change the active scene tree from another thread: build or load nodes outside the tree in the worker thread and add them on the main thread withcall_deferred(), don't load the same resource from several threads at once, and protect data that threads share with aMutex. Check which engine classes a thread may use first; the rendering and physics servers are thread-safe only after you turn on their threading project settings. (Godot, Thread-safe APIs, Using multiple threads)
How to enforce¶
Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.
- 7 GDScript warning system: set
untyped_declarationto Error in Project Settings, and scripts with untyped variables, parameters or return types no longer compile. ChecksGODOT-9. - 5 gdlint and gdformat (gdtoolkit): gdlint checks names against the style guide's conventions and the order of class definitions, and
gdformat --checkfails when a file would be reformatted. Both exit with a non-zero code, so they work in CI or a pre-commit hook. Partly checksGODOT-10.
Tutorials and further reading¶
- 6 Make a Finite State Machine in Godot 4 (GDQuest): when a state machine helps, and how to build one with a single state variable or with one node per state, with the trade-offs of each.