COMMENT


Comments are used to add explanations or notes to your code that do not affect the game's functionality. In other words, code comments are used to provide additional information about the code that will not directly affect how the code runs.

In Lua, comments can be added to the code by using two hyphens (--). Anything written after the two hyphens on a line will be ignored when running your game. For example:

-- This is a comment in Lua
print("Hello, World!") -- This is also a comment

--[[ use this to create a long 
multi-line comment when you 
want to explain a lot ]]

In the above example, the first line is a comment and will be mostly ignored by PICO-8. Comments do take up cart space as characters, but they do not cost any tokens. The second line will be executed and will output "Hello, World!" to the console. You add a comment at the end of a line of code to make a label or note for that line.

Use multi-line comments ( using --[[ and ]] ) to give a long and detailed comment, useful when a single line comment trails off the right side of the screen and you want to make it easier to read without scrolling.


Alternative

If you are coming from another programming language and are already familiar with using double slashes ( // ) for comments, then you can continue to use them in PICO-8. (This is specific to PICO-8 and not Lua).

// this is also a single line comment

Note that this does not work for multi-line comments, nor for naming tabs in the code editor (more on this below).


Usefulness

Comments can also be used to disable code temporarily during testing or debugging. To do this, you can simply add two hyphens at the beginning of the line of code. This is often referred to as "commenting out" a line of code. For example:

print("hello world")
--print("This line of code is temporarily disabled")

In the above example, it will print "hello world" but the second print will not be executed because it is commented out.

It's important to note that code comments are meant for humans to read and understand, not for computers to execute. Therefore, it's good practice to add comments to your code to make it easier to understand and maintain, both for others reading your code, and even for yourself if you return to your code later.

As a programmer, commenting your code well is just as important of a habit to have as formatting and naming your variables and functions accurately and descriptively.


Naming Tabs

If you are using the PICO-8 Code Editor, you can name the tabs by placing a single line comment at the top of the tab. These names will be displayed under the tabs as you hover over the tab numbers. 

This is great for organizing your code and labeling for ease of navigation between tabs.


Labeling Carts

To properly label your game cartridge, create 2 single line comments at the very top of your code (in tab #0). These will be displayed below the label section of a .png game cart image. This is usually used to first name the cart, and second name the developer.

See Save Cartridge Image.


581

8 May 2023

Font