Skip to content

Coding guidelines

Carenalga edited this page Apr 17, 2024 · 7 revisions

Complete example

The coding guidelines used in the project are based on the GDQuest GDScript guidelines and Godot"s GDScript style guide.

# Example class of coding guidelines.
#
# This fake GDScript class shows how the code in most of the Popochiu scripts
# is ordered and written.
# Group things according to the Code order section.
@tool
# Use PascalCase for class and node names
class_name PopochiuCodeGuidelines
extends Node

# Use the past tense to name signals
signal game_loaded(Dictionary)
signal ui_requested

enum Types {
	ROOM,
	CHARACTER,
	INVENTORY_ITEM,
	DIALOG,
}
enum FlipsWhen { NONE, MOVING_RIGHT, MOVING_LEFT }

# Use SNAKE_CASE (in uppercase) when defining constants
const SELECTED_FONT_COLOR := Color("706deb")
# Keep individual lines of code under 100 characters. If you can, try to keep
# lines under 80 characters
const PLAYER_CHARACTER_ICON :=\
preload("res://addons/Popochiu/icons/player_character.png")
# Use PascalCase when loading a class into a constant (or a variable)
const PopochiuClickable :=\
preload("res://addons/Popochiu/Engine/Objects/Clickable/PopochiuClickable.gd")

export var is_clickable := true
export var object_name := "" setget set_object_name

# Whenever you can use static typing when defining variables and method
# parameters
var is_cutscene_skipped := false
var hovered: PopochiuClickable = null setget set_hovered, get_hovered

# Private variables and methods start with underscore: _
#   > It will also apply to virtual methods from Popochiu 2.0 onwards
var _config := ConfigFile.new()
var _is_camera_shaking := false
var _shake_timer := 0.0
var _hovered_queue := []

@onready var btn_create: Button = %BtnCreate
@onready var creation_popup: Popup = %Loading


#region Godot ######################################################################################
# Surround functions with two blank lines
func _ready() -> void:
	_config = PopochiuResources.get_data_cfg()
	
	btn_create.pressed.connect(_on_create_pressed)

	_do_the_private_thing()
#     blank line 1   \ ( u.u)>
#     blank line 2   <(u.u ) /
func _process(delta: float) -> void:
	if _is_camera_shaking:
		_shake_timer -= delta


func _input(event: InputEvent) -> void:
	if event.is_action_released("popochiu-skip"):
		is_cutscene_skipped = true


#endregion

#region Virtual ####################################################################################
func _on_click() -> void:
	pass


func _on_right_click() -> void:
	pass


#endregion

#region Public #####################################################################################
func wait(time := 1.0, is_in_queue := true) -> void:
	if is_in_queue: yield()

	if is_cutscene_skipped:
		yield(get_tree(), "idle_frame")
		
		return
	
	yield(get_tree().create_timer(time), "timeout")


# Declare the return type of functions whenever you can
func do_something() -> bool:
	prints("I have to do something")
	return true


#endregion

#region SetGet #####################################################################################
func set_hovered(value: PopochiuClickable) -> void:
	hovered = value
	
	if not hovered:
		G.show_info()


func get_hovered() -> PopochiuClickable:
	return null if _hovered_queue.is_empty() else _hovered_queue[-1]



#endregion

#region Private ####################################################################################
func _on_create_pressed() -> void:
	creation_popup.popup_centered_minsize(Vector2(640, 360))


func _do_the_private_thing() -> void:
	prints("Shhhhhhhh", "...", "This is stupid!")


#endregion

Code order

01. tool
02. class_name
03. extends

04. signals
05. enums
06. constants
07. exported variables
08. public variables
09. private variables
10. onready variables


#region Godot ######################################################################################
11. Godot built-in methods


#endregion

#region Virtual ####################################################################################
12. Virtual methods


#endregion

#region Public #####################################################################################
13. Public methods


#endregion

#region SetGet #####################################################################################
14. Setters and getters


#endregion

#region Private ####################################################################################
15. Private methods


#endregion

#region SubClasses #################################################################################
16. Subclasses


#endregion

File naming

For naming scene, resource and script files, use snake_case:

popochiu_clickable.gd
animation_player_inspector_dock.gd
animation_player_inspector_dock.tscn
room_house_state.tres

This will change in Popochiu 2.0. There we"ll use snake_case instead :D .

Use snake_case when naming other type of files:

icon_room.png
icon_walkable_area.png
monkey_island_1991.ttf

Node naming

Nodes inside scenes should be named with PascalCase:

Props
WalkableArea
WalkToPoint
Clone this wiki locally