Posted 11 August 2013 - 07:44 AM
working with strings, and variables
this is a tutorial on how to work with different things in lua
strings:
you can use strings in any function that expects a string, like print()variables:
how to use
but this can also be used to print a changing variable like:
now cool thing is about variables the can also get information from functions like:
working with functions, tables and loops
first of functions and args:
functions are things in a program you can call to run a code
now onto tables and loops:
thank you for reading this tutorial and if you have any questions please post below
i might be adding more later
this is a tutorial on how to work with different things in lua
"string" -- is a string
var = "string" -- is a variable containing the string "string"
table = {"string1", "string2"} -- now a table is defined by curly braces {}
now the above isnt very useful you need to now how to work with these,strings:
Spoiler
strings are good for use in simple programs like
print("string") -- string is inside the " "
you can use that to print the word string or what evers inside the " "you can use strings in any function that expects a string, like print()
Spoiler
variables are awesome they an contain your string or table for later use.how to use
var = "my var"
print(var) -- prints 'my var'
print(var) -- prints 'my var'
var = "different" -- this changes what is inside the variable, overrides it
print(var) -- prints 'different'
now this is more efficent then having to keep typing 'my var' or 'different'but this can also be used to print a changing variable like:
if rs.getInput("right") then
var = "on" -- if right side powered by redstone var will be 'on'
else
var = "off" -- if right side not powered by redstone var will be 'off'
end
print(var) -- print the variable
now you can see how this is useful to print a conditional statement,now cool thing is about variables the can also get information from functions like:
program = shell.getRunningProgram() -- shell.getRunningProgram is a defualt program that returns the running program ( stores it in the variable )
print(program) -- will print the running program
now any string including those returned by functions can be stored in a variableSpoiler
now functions, tables and loop can help shorten code ( this will be sorta all worked in together were needed )first of functions and args:
functions are things in a program you can call to run a code
-- define a function like this
function myFunction(args) -- args is a variable to be called upon later in a variable
-- enter code you want to run when function is called
print(args) -- prints 'args'which is defined when functions called
end -- you have to end a function with an end
-- to call function just type in its name and args like this
myFunction("Hello") -- this will print 'hello' which is defined as args when function is called
-- you also have to call it for the code to run
functions can also be used to return a variable like:
-- how to use our other program as a function
function redstone() -- no args are used in this function
if rs.getInput("right") then
var = "on" -- if right side powered by redstone var will be 'on'
retunr var -- returns 'on'
else
var = "off" -- if right side not powered by redstone var will be 'off'
return var -- -- returns 'off'
end -- end 'if'
end -- end function
-- now we can get var because of return
returned = redstone() -- returned = what the function redstone returned
print(returned) -- prints returned
Spoiler
now tables are like variables they store information but, they can store multiple strings like:
table = {[1] = "hello", [2] = "world"} -- use curly braces to define it as a table and ',' to seperate strings.
now thats a table but its a little more complecated if we want to print whats in the table heresd were for loops come in handy
-- we could do
table = {[1] = "hello", [2] = "world"}
print(table[1]) -- which would print 'hello' because in table [1] = "hello", we could also change this to print [2]
print(table[2]) -- this would print all in the table but it is not good enough if:
table = {[1] = "a", [2] = "b", [3] = "c"}
-- this would not print everything in the table, if we want to do that we can use for loops
use for i = 1, 10 do to loop the code 10 times
for i = 1, #table do -- #table is how many variables table contains, it returns a number
print(table[i]) -- i = the number of times it has looped so on the first time table[i] is really table[1]
end -- close your for loop
-- you could also use pairs
for key, value in pairs(table) do -- now there is to variables defined by this function 'key' and 'value'
--[[ now basicly
key = number of looped times
value = table[key]
]]--
print(value) -- print the current variable in table
end -- close your for loop
now they can be useful for display all your tables but, you might think for 3 variables thats alot but it will run for any amout of variables you havei might be adding more later