Difference between revisions of "Basic lua: Basics"

From The Official Visionaire Studio: Adventure Game Engine Wiki
m
 
(21 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
{| class="in"
 +
|-
 +
| class="i_arrow clickablecell" |  [[basic_lua:_Introduction|<]]
 +
| class="i_norm clickablecell" | '''[[basic_lua:_Index|Index]]'''
 +
| class="i_arrow clickablecell" | [[basic_lua:_Operators|>]]
 +
|}
 +
<hr>
 
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.
 
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 ==
 
== 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>
 +
== 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.
 +
<syntaxhighlight lang="lua">
 +
a = 1
 
</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 <span class="inlinecode">..</span>
<syntaxhighlight>
+
<syntaxhighlight lang="lua">
 
a = "hello"
 
a = "hello"
 
b = "world"
 
b = "world"
Line 14: Line 26:
 
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
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
== Return ==
 +
Return allows us to return data from a script or function, which can then be used inside of another script or function.
 +
<syntaxhighlight lang="lua">
 +
function hello()
 +
return "hello world!"
 +
end
 +
 +
print( hello() ) -- prints "hello world!"
 +
</syntaxhighlight>
 +
== 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.
 +
 +
Global is often used for functions, tables & variables that need to be accessed by multiple scripts, or from various different locations.
 +
<syntaxhighlight lang="lua">
 +
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
 +
</syntaxhighlight>
 +
{| class="ts"
 +
|-
 +
| 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.''
 +
|}
 
== Comments ==
 
== Comments ==
 
=== 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 23: Line 62:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
=== 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 36: Line 75:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
=== 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.
<syntaxhighlight>
+
<syntaxhighlight lang="lua">
 
--[=[
 
--[=[
 
this is a nested comment
 
this is a nested comment
Line 50: Line 89:
 
]]
 
]]
 
</syntaxhighlight>
 
</syntaxhighlight>
== Global vs Local ==
+
<hr>
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''.
+
{| class="in"
 
+
|-
Global is often used for functions, tables & variables that need to be accessed by multiple scripts, or from various different locations.
+
| class="i_arrow clickablecell" |  [[basic_lua:_Introduction|<]]
{{toc}}
+
| class="i_norm clickablecell" | '''[[basic_lua:_Index|Index]]'''
 +
| class="i_arrow clickablecell" | [[basic_lua:_Operators|>]]
 +
|}{{toc}}

Latest revision as of 14:00, 5 September 2022

< 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

Return

Return allows us to return data from a script or function, which can then be used inside of another script or function.

function hello()
 return "hello world!"
end

print( hello() ) -- prints "hello world!"

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.

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

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 >