Skip to content

Coding guidelines

Carenalga edited this page Mar 8, 2023 · 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(data)
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 = find_node('BtnCreate')
onready var creation_popup: Popup = find_node('Loading')


# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ GODOT ░░░░
# Surround functions with two blank lines
func _ready() -> void:
	_config = PopochiuResources.get_data_cfg()
	
	btn_create.connect('pressed', self, '_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


# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ VIRTUAL ░░░░
func on_click() -> void:
	pass


func on_right_click() -> void:
	pass


# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 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


# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ SET & GET ░░░░
func set_hovered(value: PopochiuClickable) -> void:
	hovered = value
	
	if not hovered:
		G.show_info()


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



# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 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!')

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


# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ GODOT ░░░░
11. Godot built-in methods


# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ VIRTUAL ░░░░
12. Virtual methods


# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ PUBLIC ░░░░
13. Public methods


# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ SET & GET ░░░░
14. Setters and getters


# ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ PRIVATE ░░░░
15. Private methods


16. Subclasses

File naming

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

PopochiuClickable.gd
AnimationPlayerInspectorDock.gd
AnimationPlayerInspectorDock.tscn
RoomHouseState.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
Clone this wiki locally