• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua [Tutorial] Basic Lua

Ispiro

New Member
Joined
Oct 1, 2007
Messages
129
Reaction score
2
Basics of Programming
In this thread, I'll try to tell you about variables, 'if then' checks, function and their parameters.
Although I might not be using the correct 'terms', this thread is just for explanatory purposes of how I think things work in Lua. Information in this thread can be 'questionable'.

Well, first I'd like to start off by explaining what's a variable. A variable is a place where you store a value, whether it's a string or number.
You can assign a value to the variable by using the '=' sign, for example:
Code:
variable = 1
Note: the term variable is what you want to refer to the variable by(you'll probably understand what I mean later on in the thread)
You can read variables and check it's contents.
Code:
print(variable)
Using a Stand-Alone Lua interpreter, that should print out 1. But what now? What's the use of assigning a value to a variable?
Well, first and most basic statement used in almost all languages is the if then statement. By reading a variable for it's contents and doing an action according to what it's holding.
Code:
if variable == 1 then
print('text')
end
Note: To evaluate a variable, you must use double '='s. You would only use 1 equal sign to assign a value to a variable.
This would print out 'text', or call for another function instead(we'll get to this part later on, for now, we'll just be using prints).
You can also act upon if the variable wasn't equal to 1, by using 'elseif'.
Code:
variable = 2
if variable == 1 then
print('text')
elseif variable == 2 then
print('even more text')
end
This would print 'even more text'. However, what if variable was equal to neither 1 or 2? You can use the 'else' statement here.
Code:
variable = 3
if variable == 1 then
print('text') 
elseif variable == 2 then
print('even more text')
else
print('wtf not 1 or 2')
end
Note: 'else' statement should be at the bottom of all other checks.
This would print 'wtf not 1 or 2', because the variable was not equal to 1 or 2.
Alright, now to sum it all up, here's a commented example script:
Code:
variable = 2 -- to assign a value to a variable, use just one equal sign and the value after it
if variable == 1 then -- evaluations to check a variable's value would require 2 equal signs
print('text')
elseif variable == 2 then -- seeing that variable is equal to 2(as assigned in first line), the script will execute the following block of code.
print('even more text')
else -- this print statement will not be executed, because variable was found to be one of the above options
print('wtf not 1 or 2')
end
Note: Comments must be preceed by '--'.

Functions and Parameters
A function is used to perform a task like print(text), prints text in the console window. Also, commonly, functions are used to simplify scripts(you'll understand what I mean later on in this thread).

I'll use an example I saw in a C++ tutorial... party invites.
Ofcourse, a program won't create the cards and walkdown the street to the postbox, but just for explaining :p
To invite someone to your party, you'll have to use these statements:
Code:
Write [i]name[/i] on card.
Put card in envelope.
Write [i]name[i]'s address on envelope.
put envelope in postbox
If you're inviting a friend or two, you won't have a problem.
But, what if you're invite 5 friends, or 10? Now you'll be crapping lines of repeated codes.
Code:
Write Pablol on card.
Put card in envelope.
Write Pablol's address on envelope.
put envelope in postbox

Write Colandus on card.
Put card in envelope.
Write Colandus's address on envelope.
put envelope in postbox

Write Zaga on card.
Put card in envelope.
Write Zaga's address on envelope.
put envelope in postbox

Write Helveg on card.
Put card in envelope.
Write Helveg's address on envelope.
put envelope in postbox

Write Kakan on card.
Put card in envelope.
Write Kakan's address on envelope.
put envelope in postbox
Ofcourse, you can create a function instead, making the script 'simpler' and easier to read.
Code:
function inviteFriend(friend)
Write friend's name on card.
Put card in envelope.
Write friend's address on envelope.
put envelope in postbox
end

inviteFriend(Kakan)
inviteFriend(Colandus)
inviteFriend(Helveg)
inviteFriend(Zaga)
inviteFriend(Pablol)
Which one is easier to read, and faster to edit? Unless your insane and cross-eyed, I'm pretty sure I know what your answer is.
Now that you know what functions are used for, we get to the part on how functions are created.
First, we'll need to define the function itself.
Code:
function example()
	print('sometext')
end
example is the function's name, the name is used to refer to the function itself.
you can call the function by using it's name, which in this case is example.
Also, remember that you must add paranthesis, to tell the computer that you're calling for a function and not defining a variable.
I'll explain what goes between the paranthesis later on.
Code:
example()
This would print out 'sometext' in console window everytime example() is used.
What if we would like to print out different text? In this case, we'll have to pass parameters to the function with the text we would like to print.
First, we'll need to define a new function that reads the parameter.
Code:
function newExample(textToPrint)
	print(textToPrint)
end
Now, we can call the function and tell it to print whatever text we want.
Code:
newExample('sometext')
newExample('someothertext')
This would print out 2 different phrases, using the same function.
Now, for the last part of my thread...
You can also call a function from inside another.
Code:
function lastExample(text)
	example()
	newExample(text)
end
lastExample('some random text')
First, the script would call for example(), printing out 'sometext'. Afterwards, the script would call for newExample, printing the parameter given to lastExample, which in this case, is 'some random text'.

Alright, hope I helped someone with this. Although it's pretty basic, I could've used something like this when I first started, maybe someone else would need it too.

Comments please.
 
Last edited:
This really IS basic lua scripting, you have to know this to make your own scripts. BTW nice tutorial Ispiro. Just that you might want to use real functions and maybe show a script that's composed from the things you've shown us. Other than that, great.
 
This really IS basic lua scripting, you have to know this to make your own scripts. BTW nice tutorial Ispiro. Just that you might want to use real functions and maybe show a script that's composed from the things you've shown us. Other than that, great.
I did use 'real functions'. The inviteFriend stuff was just for explaining what functions are used for.
 
Back
Top