Basic lua: Basics

From The Official Visionaire Studio: Adventure Game Engine Wiki
Revision as of 17:49, 2 November 2018 by AFRLme (talk | contribs)
< 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.

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)

Variables

Variables are like keywords that you can use to store & access data. Variables in Lua script can be local or global in scope, & unlike most other programming languages, they do not require you to specify their type when creating them as variables can be overwritten at any time with new data.

a = 1

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

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.

Quick 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 overwriting the existing data that the variable, or function contained.

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

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
]]
< Index >