Code Guidelines
Forced Static Typing
Unfortunately GDScript Static Typing isn’t mandatory even when the optional Static Typing is turned on it’s still not forced which is not aligned with our M mindset can lead into some weird unwanted behavior.
Our only option is to force it ourself, but sometimes as a human we forgor and that’s ogey just add the type on the Litter Pick-up phase.
Naming Convention
class = PascalCase node = PascalCase scene = PascalCase variable = snake_case function = snake_case file = snake_case
Access Modifier
GDScript doesn’t really have an Access Modifier so this is just a convention to mark stuff therefore this will not effect the code in any way.
To mark a variable as private prefix it with an underscore.
leave public and protected variable as is.
Method doesn’t have an access modifier, a method with underscore prefix indicate that it’s somekind of callback function and not a private metod. If an object can access another object method then it’s either safe to do so or there’s something wrong with the architecture, as a rule of thumb if the method you’re trying to access is part of the local scene tree then it’s fine to access it but if it’s not please proceed with caution.
Getter Setter
Only create getter setter when there’s an extra stuff taking place if the getter setter serve no purpose it’s better to not have them in the first place.
prefix the member variable with an underscore
getter: get_variable_name
setter: set_variable_name
Redundant:
func set_is_in_battle(new_state: bool) -> void:
is_in_battle = new_state
Reasonable:
func set_is_in_battle(new_state: bool) -> void:
if new_state == true:
battle_timer.start()
is_in_battle = new_state
emit_signal("battle_state_changed")
Exporting Variable
We want our designer workflow to be as frictionless as possible and providing them with the ability to tweak stuff inside without touching the code is a great value and this can be done with expoting the variables.
To export variable we need to prefix our variable with the export keyword followed by the export option variable name and then the type.
Example: export(String, MULTILINE) var skill_description: String