Difference between revisions of "Basic lua: Basics"
m |
|||
Line 19: | Line 19: | ||
b = "world" | b = "world" | ||
− | print(a .. " " .. b) -- prints "hello world" - we had to add " " to create a space between the 2 words | + | print(a .. " " .. b) -- prints "hello world" - we had to add " " to create a space between the 2 words... |
+ | |||
+ | -- though we could have directly inserted the space into the variables containing the words | ||
+ | a = "hello " | ||
+ | b = "world" | ||
+ | |||
+ | print(a .. b) -- prints "hello world" | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Comments == | == Comments == |
Revision as of 16:09, 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...
-- though we could have directly inserted the space into the variables containing the words
a = "hello "
b = "world"
print(a .. b) -- prints "hello world"
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.
--[=[
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 | > |