Basic lua: Iteration and Loops: Difference between revisions
From The Official Visionaire Studio: Adventure Game Engine Wiki
No edit summary |
No edit summary |
||
| Line 6: | Line 6: | ||
|} | |} | ||
<hr> | <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> | |||
== | == Iteration == | ||
Below I am going to show you how to | Below I am going to show you how to use the 3 main types of '''for''' iteration loops. | ||
=== | === for i equals 1 === | ||
{| class="ts" | {| class="ts" | ||
|- | |- | ||
! | ! iterate through an index table !! | ||
|- | |- | ||
| style="max-width:680px;" | <syntaxhighlight lang="lua"> | | style="max-width:680px;" | <syntaxhighlight lang="lua"> | ||
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 | end | ||
print(str) -- print the content of the str variable to the log print(str) | |||
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_( | </syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(loops)_1.png|frameless|center|180px]] | ||
|} | |} | ||
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()
|
Function with Input Arguments
| print 6 | |
|---|---|
function plus(a, b)
return a + b
end
print( plus(2, 4) )
|
Input Arguments and Fallback
| < | Index | > |


