Difference between revisions of "Basic lua: Basics"
m |
|||
Line 10: | Line 10: | ||
== Print == | == Print == | ||
The print function is used for printing messages to the log file or lua console & is useful for debugging your scripts & functions. | The print function is used for printing messages to the log file or lua console & is useful for debugging your scripts & functions. | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
print("hello world") -- prints "hello world" into the log/console (ah yes how cliché of me, but it had to be done) | print("hello world") -- prints "hello world" into the log/console (ah yes how cliché of me, but it had to be done) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Concatenation == | == Concatenation == | ||
Concatenation is essentially the joining of two or more strings & is represented by two full stops like so '''..''' | Concatenation is essentially the joining of two or more strings & is represented by two full stops like so '''..''' | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
a = "hello" | a = "hello" | ||
b = "world" | b = "world" | ||
Line 24: | Line 24: | ||
=== Single line comments === | === Single line comments === | ||
can ''only'' be placed on their ''own line'' or at the ''end'' of a line. | can ''only'' be placed on their ''own line'' or at the ''end'' of a line. | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
-- this is a single line comment | -- this is a single line comment | ||
i = 10 -- comment placed after working code | i = 10 -- comment placed after working code | ||
Line 31: | Line 31: | ||
=== Multi-line comments === | === Multi-line comments === | ||
can be placed anywhere in the script & allow you to write comments over multiple lines. | can be placed anywhere in the script & allow you to write comments over multiple lines. | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
--[[ | --[[ | ||
this is a multiple line comment | this is a multiple line comment | ||
Line 44: | Line 44: | ||
=== Nested multi-line comments === | === Nested multi-line comments === | ||
allow you to comment out entire blocks of code, even if they already contain multi-line comments, thus preventing existing multi-line comments from ending the nested comment. ''Note: It seems that, nested comments are not included in the geshi lua syntax, as the comments should be completely grayed out, but I can assure that they work''. | allow you to comment out entire blocks of code, even if they already contain multi-line comments, thus preventing existing multi-line comments from ending the nested comment. ''Note: It seems that, nested comments are not included in the geshi lua syntax, as the comments should be completely grayed out, but I can assure that they work''. | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
--[=[ | --[=[ | ||
this is a nested comment | this is a nested comment | ||
Line 61: | Line 61: | ||
Global is often used for functions, tables & variables that need to be accessed by multiple scripts, or from various different locations. | Global is often used for functions, tables & variables that need to be accessed by multiple scripts, or from various different locations. | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
local a = 1 -- only accessible in this script | local a = 1 -- only accessible in this script | ||
Revision as of 16:03, 2 November 2018
< | Index | > |
Here I am covering a few of the basic fundamentals of Lua script that I can't really create individual pages for. You will probably see most of these - in one context, or another - throughout the various pages of this guide.
The print function is used for printing messages to the log file or lua console & is useful for debugging your scripts & functions.
print("hello world") -- prints "hello world" into the log/console (ah yes how cliché of me, but it had to be done)
Concatenation
Concatenation is essentially the joining of two or more strings & is represented by two full stops like so ..
a = "hello"
b = "world"
print(a .. " " .. b) -- prints "hello world" - we had to add " " to create a space between the 2 words
Comments
Single line comments
can only be placed on their own line or at the end of a line.
-- this is a single line comment
i = 10 -- comment placed after working code
-- comment place before code; i = 10 -- notice how the code is also commented out?
Multi-line comments
can be placed anywhere in the script & allow you to write comments over multiple lines.
--[[
this is a multiple line comment
I can go on & on & on
& on & on
& on.
]]
local i = 2 * --[[ this is a comment ]] 2
print(i) -- would print 4 to the log (2x2)
Nested multi-line comments
allow you to comment out entire blocks of code, even if they already contain multi-line comments, thus preventing existing multi-line comments from ending the nested comment. Note: It seems that, nested comments are not included in the geshi lua syntax, as the comments should be completely grayed out, but I can assure that they work.
--[=[
this is a nested comment
see how --[[ we can include ]]
a multi-line comment inside of it?
]=]
--[[
this is what would happen if we were to use
a regular multi-line --[[ comment ]]
i = 10
]]
Global vs Local
Global & local refers to anything from variables, to tables, to functions. The difference between global & local is that local only works inside of the script or function that it was created in, whereas global can be used throughout multiple scripts. Note: You need to be careful when using global conventions, that you do not use the same instance in other scripts, as this would be considered cross-polluting & may break your script because you would be canceling out what it was previously doing.
Global is often used for functions, tables & variables that need to be accessed by multiple scripts, or from various different locations.
local a = 1 -- only accessible in this script
a = 1 -- can be accessed from anywhere
function return_n(n)
local a = n -- only valid inside of this function but can be returned outside of function
return a
end
< | Index | > |