Basic lua: Basics

From The Official Visionaire Studio: Adventure Game Engine Wiki
Revision as of 16:00, 22 September 2014 by AFRLme (talk) (Created page with "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 anot...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.

Print

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 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.