Difference between revisions of "Basic lua: Iteration and Loops"

From The Official Visionaire Studio: Adventure Game Engine Wiki
(Created page with "{| class="in" |- | class="i_arrow clickablecell" | < | class="i_norm clickablecell" | '''Index''' | class="i_arrow clickablecell" |...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
{| class="in"
 
{| class="in"
 
|-
 
|-
| class="i_arrow clickablecell" |  [[basic_lua:_Tables|<]]
+
| class="i_arrow clickablecell" |  [[basic_lua:_Functions|<]]
 
| class="i_norm clickablecell" | '''[[basic_lua:_Index|Index]]'''
 
| class="i_norm clickablecell" | '''[[basic_lua:_Index|Index]]'''
| class="i_arrow clickablecell" | [[basic_lua:_Functions|>]]
+
| class="i_arrow clickablecell" | [[basic_lua:_Strings_and_Patterns|>]]
 +
|}
 +
<hr>
 +
Being able to iterate through data for the purpose of reading it or manipulating it is very important when it comes to scripting as it is a real time saver. There are multiple different ways to create iteration loops & loops in general with Lua script.
 +
<hr>
 +
{| class="ts"
 +
|-
 +
| ''Quick note: iteration loops will automatically pause all lines after them until the for loop has finished, which is why in the first example below I can get away with adding the print message after the function & as you can see, it printed the entire message.''
 
|}
 
|}
 
<hr>
 
<hr>
Functions can be really useful as they let us process/calculate huge amounts of data & return it, also if you include input arguments then they can potentially be reused multiple times over, which can reduce overall workload & increase workflow.
 
  
== Creating Functions ==
+
== Iteration ==
Below I am going to show you how to create your own custom functions.
+
Below I am going to show you how to use the 3 main types of '''for''' iteration loops.
  
=== Classic Function ===
+
=== for i equals 1 ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! print hello world! !!
+
! iterate through an index table !!
 
|-
 
|-
 
| style="max-width:680px;" | <syntaxhighlight lang="lua">
 
| style="max-width:680px;" | <syntaxhighlight lang="lua">
function hello()
+
local t = {"Hi,", "my", "name", "is", "Barry."}
print("hello world!")
+
local str = ""
 +
 
 +
for i = 1, #t do -- for variable equals 1 to table total entries do
 +
  str = str .. t[i] -- insert string from table based on current index value into the str variable
 +
  if i < #t then str = str .. " " end -- if i is less than table total then insert white space after each word
 
end
 
end
  
hello()
+
print(str) -- print the content of the str variable to the log  print(str)
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(functions)_1.png|frameless|center|180px]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(loops)_1.png|frameless|center|180px]]
 
|}
 
|}
  
Line 71: Line 81:
 
{| class="in"
 
{| class="in"
 
|-
 
|-
| class="i_arrow clickablecell" |  [[basic_lua:_Tables|<]]
+
| class="i_arrow clickablecell" |  [[basic_lua:_Functions|<]]
 
| class="i_norm clickablecell" | '''[[basic_lua:_Index|Index]]'''
 
| class="i_norm clickablecell" | '''[[basic_lua:_Index|Index]]'''
| class="i_arrow clickablecell" | [[basic_lua:_Functions|>]]
+
| class="i_arrow clickablecell" | [[basic_lua:_Strings_and_Patterns|>]]
 
|}{{toc}}
 
|}{{toc}}

Latest revision as of 16:14, 10 September 2022

< Index >

Being able to iterate through data for the purpose of reading it or manipulating it is very important when it comes to scripting as it is a real time saver. There are multiple different ways to create iteration loops & loops in general with Lua script.


Quick note: iteration loops will automatically pause all lines after them until the for loop has finished, which is why in the first example below I can get away with adding the print message after the function & as you can see, it printed the entire message.

Iteration

Below I am going to show you how to use the 3 main types of for iteration loops.

for i equals 1

iterate through an index table
local t = {"Hi,", "my", "name", "is", "Barry."}
local str = ""

for i = 1, #t do -- for variable equals 1 to table total entries do
  str = str .. t[i] -- insert string from table based on current index value into the str variable
  if i < #t then str = str .. " " end -- if i is less than table total then insert white space after each word
end

print(str) -- print the content of the str variable to the log  print(str)

Variable as Function

print hello world!
hello = function()
 print("hello world!")
end

hello()
Basic lua (functions) 2.png

Function with Input Arguments

print 6
function plus(a, b)
 return a + b
end

print( plus(2, 4) )
Basic lua (functions) 3.png

Input Arguments and Fallback

print 0.5, 5
function divideAndConquer(a, b, c)
  c = c or 1 -- if c equals nil then c equals 1
  return (a / b) * c
end

print( divideAndConquer(2, 4) ) -- 2 ÷ 4 x 1 = 0.5
print( divideAndConquer(2, 4, 10) ) -- 2 ÷ 4 x 10 = 5
Basic lua (functions) 4.png

< Index >