Hello and welcome to the GDScript fundamental tutorial series.
In this episode, I go over comments in Godot GDScript.
A comment is a programmer-readable explanation or annotation in the source code of a computer program in computer programming.
They are added to make the source code easier for humans to understand and are generally ignored by compilers and interpreters during runtime.
You can use the #
symbol followed by your comment text.
You can use the triple quotation marks for opening and closing your comments """
.
Keep in mind that the compiler will interpret this as a multi-line string value.
# This is a single line comment
"""
This is a multiline comment.
This is a multiline comment.
This is a multiline comment.
"""
There are 4 different types of comments you may find yourself using:
Methodology comments can be used to explain the code you are using rather than the clarification of its intent.
For example, in your code for a sorting algorithm, you may use a methodology comment to explain why you used the insertion sort algorithm instead of the quick sort.
"""
Quicksort turned out to be slower than insertion sort for our list. For this reason, I went with insertion sort.
"""
These comments are at the top of a script file.
They will include things like the company name, author, file name, etc.
You will typically see this sort of comment on open source code.
"""
This file is part of:
Godot Game Engine
https://godotengine.org
***********************
Copyright (c) 2007 -2021 Juan Linietsky, Ariel Manzur
"""
Debugging comments are used when you want to apply brute force debugging techniques on your code.
An example would be commenting out print statements (which print things to console).
if(x < 10):
# print(x)
x = x + 1
Generally used o make others understand the intent of a line of code.
This type of comment should be used only when needed.
Sometimes if used, it is a sign of lousy variable naming convention.
The following is an example of a Code Description comment.
# Player health
var x = 100
In this case, it is a lousy use of Code Description. You can do a better job of describing your code through its variable name.
var player_health = 100
You can do even better by expressing what values you’d like your variable to have
var player_health: int = 100
There is a saying:
Code tells you how, and comments tell you why
Try to use naming conventions to improve your code readability.
When you have reached that limit, use comments to explain the rationale of your code.
Comments | Godot GDScript Tutorial | Ep 04 video & article by Godot Tutorials is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License .