Comments

From GMod Wiki

Revision as of 23:18, 9 May 2011 by theJ89 (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Okay, I'm going to (very quickly) cover comments. Comments let you write notes for yourself in your script without affecting how your script works.

There are two types of comments: The Single-Line comment and the Multi-Line comment. Both are ridiculously easy to use.

Lets say I've got a piece of code here and I want to explain what it does:

 
playerScore={} 
playerScore[1]=0 
playerScore[2]=0 
playerScore[3]=0 
 
playerScore[1]=5 
 

First lets add a single-line comment, above the playerScore={}. To do this you just have to type -- And then type whatever you want! Everything after an -- is ignored by lua, until the next line of course.

 
--This table holds the player's score 
playerScore={} 
playerScore[1]=0 
playerScore[2]=0 
playerScore[3]=0 
 
playerScore[1]=5 
 

Not too shabby. You can also put a single line comment after a piece of code:

 
--This table holds the player's score 
playerScore={} 
playerScore[1]=0 
playerScore[2]=0 
playerScore[3]=0 
 
playerScore[1]=5  --Set player 1's score to 5 
 

If a line of code is giving you trouble, but you don't want to get rid of it, you can use a single-line comment to make lua ignore it.

 
--This table holds the player's score 
playerScore={} 
playerScore[1]=0 
playerScore[2]=0 
playerScore[3]=0 
 
--The line below is disabled 
--playerScore[1]=5  --Set player 1's score to 5 
 

This is called commenting out. It's just disabling code by making lua ignore it.

Now that you've got the hang of single-line comments, I'll show you how to use multiline comments.

Multi-line comments allow you to comment out a large section of code with ease. This is useful for removing troublesome blocks or providing in-depth explanations as to what your code does. At it's most basic, a comment is a normal comment with a multiline string in it:

--[[
This is a comment
]]

However, people often add extra comment symbols to multiline comments, for various reasons.

--[[
This is another comment
]]--

This style is sometimes used as it is more aestetically pleasing, and can make nice hedders, such as:

--[[ PLAYER METATABLE ]]--

However, a slightly more practical style is

--[[
This is yet another comment
--]]

This style is more useful for coding out sections, as you can make it into two single line comments by deleting the first [, temporarily decommenting the code without having to delete the other end of the comment. Such as:

--[
dangerousCode()
--]]

You can comment out a whole section of code with multiline comments. Lets say for some reason I was using the following code to test something and for the time being I want to turn it off.

 
Msg("One\n") 
Msg("Two\n") 
Msg("Three\n") 
Msg("Four\n") 
Msg("Five\n") 
 

So all I need to do is place comment brackets before and after it.

 
--[[
Msg("One\n") 
Msg("Two\n") 
Msg("Three\n") 
Msg("Four\n") 
Msg("Five\n") 
--]]
 

Voila!

There's no script to write for this lesson.

ADDENDUM: Because Garry nicely put in C++ style comments, there are other ways of commenting. Single line comments:

// This is a comment
Msg( "And this is code" )

Multiline comments:

/* This
is
a
multiline
comment */
Msg( "And this is code" )


Yarin Kaul Icon ArrowSquare32 left.png Tables

Yarin Kaul Icon ArrowSquare32.png Back to Lua Tutorial Series

Arithmetic in Lua Yarin Kaul Icon ArrowSquare32 right.png


Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox