2.2 KiB
2.2 KiB
When writing or reviewing GDScript code, strictly follow the official Godot Engine GDScript style guide and file naming conventions:
Code Formatting
- Indentation: Use 4 spaces (no tabs).
- Line Length: Keep lines under 100 characters where possible.
- Naming Conventions:
- Variables/functions:
snake_case(e.g.,player_health,calculate_damage()). - Constants:
UPPER_SNAKE_CASE(e.g.,MAX_SPEED = 100). - Class Names:
PascalCase(e.g.,class_name Player.gd). - Signals:
snake_case(e.g.,signal player_died).
- Variables/functions:
- Spacing:
- Add spaces around operators (
x = y + z, notx=y+z). - No space after
(or before)in function calls (e.g.,func(a, b)).
- Add spaces around operators (
- Braces: Open braces on the same line for
if,for,func, etc.:if x > 0: print("Positive") - Docstrings: Use Godot’s built-in docstring format for functions:
## Calculates the player's speed based on input. ## @param delta: Time since last frame (float). ## @return: The adjusted speed (float). func calculate_speed(delta: float) -> float: return base_speed * delta - Type Hints: Always include type hints for parameters and return values (Godot 4+).
- Avoid Redundancy: Omit redundant
self.unless required for clarity.
File Naming Conventions
- Script Files:
- Use
snake_casefor.gdfiles (e.g.,player.gd,enemy_ai.gd). - Match the filename to the class name (if the script defines a class).
Example: If the class is
Player, the file should beplayer.gd.
- Use
- Scene Files:
- Use
PascalCasefor.tscnfiles (e.g.,Player.tscn,MainMenu.tscn). - Match the filename to the root node of the scene.
- Use
- Resource Files:
- Use
snake_casefor.tresor.resfiles (e.g.,player_stats.tres).
- Use
- Folders:
- Use
snake_casefor directories (e.g.,scripts/,assets/sprites/). - Group related files logically (e.g.,
player/forplayer.gd,player.tscn).
- Use
Reference: Godot GDScript Style Guide.