In this tutorial I am going to be going to present another simple script, and then go through step by step how it works, breaking it down and explaining it.
This time I will be explaining about Conditional Statements, Loops, Array Types and Comments.
-- Run this script in multiplayer!
people = get_bouts()
for i = 1,#people do
person = people[i]
if(person == "hampa") then
echo("HOLY POOP IT'S HAMPA!!")
elseif(person == "blam") then
echo("Blam? Phhs I can beat Blam!")
else
echo("Oh it's just " .. person .. ", they suck!")
end
end
Comments are text in the script that will be ignored by the computer. They can be added to scripts to help explain what something does, so you can remember what you've done or show other people. They can also be used to say how the script should be used, as is the case in the example above on line one.
You can tell Lua you want a comment by putting in two dashes --
. Anything after these two dashes all the way up to the next line will be ignored:
-- Run this script in multiplayer!
Will tell anyone that looks in the script that the script only works in a multiplayer game, but because the text has --
before it, Lua will ignore it.
Comments can be added to the end of a line to explain what the line does, or how.
people = get_bouts() -- get_bouts gets all of the players waiting to fight.
You can also have comments that span multiple lines, these are useful for putting large amounts of information, for example explaining what a function you made does. These comments are started by using --[[
and then you must end them with ]]
. Example:
--[[ This is a multiline comment
So this is still part of the comment.
This too!
]]
Remember you need to close the comment with ]]
!
Now we know that the first line of the script doesn't actually do anything because it is a comment, we can move onto the next line:
people = get_bouts()
What this does is call the built in function get_bouts
which will return an array of all players currently fighting or waiting in line to fight.
An array is a data type like a string or a number but it is a way of storing groups of information. It's like a list that you can add and remove items from, in this case it's a list of player names. You can create your own array by using curly brackets {1, 2, 3}
will create an array with three number items, 1
, 2
and 3
. You can then access a specific item by using it's index. The index is, in this case, the position at which the number is stored. To get an item using an index we go array[index]
. Here is an example:
frames = {50, 100, 150}
echo("Frame Length: " .. frames[1])
Now the frames[1]
is the important part, here we are using the index 1
to get the 1
st item in the array. Which would be 50. So the above will display in the chat:
Frame Length: 50
If you change frames[1]
to frames[2]
it will instead display:
Frame Length: 100
So going back to the get_bouts
array. If we wanted to get the people who are currently fighting we could use:
people = get_bouts()
echo("Tori: " .. people[1])
echo("Uke: " .. people[2])
Because the first two players in the list are always the ones fighting, as you should know from playing Toribash :D
To understand the loops and conditional statement sections, we must first understand blocks. In Lua blocks can be defined in a variety of ways and for different reasons.
They are a way of grouping together instructions. Now I've previously said that's what functions are, and the fact is that functions and blocks are very similar. In fact if we want to make our own function we must use blocks:
function echoBouts()
people = get_bouts()
echo("Tori: " .. people[1])
echo("Uke: " .. people[2])
end
Now we have our own function, echoBouts
which can be reused over and over again whenever we want!
The function goes from the word function
all the way to the word end
. This is a function block. There are different types of blocks, but most will end with the word end
.
Note that you can put blocks inside of other blocks, and the end
will end the block it's currently in. So if you had for example:
function dostuff(more) -- Start Block 1
stuff()
if(more) then -- Start Block 2
more_stuff()
end -- End Block 2
end -- End Block 1
There are two blocks containing:
-- Block 1
stuff()
if(more) then
more_stuff()
end
and:
-- Block 2
more_stuff()
So now we have our array of names, how can we do something to all of them? Well what we need is a loop. A loop run a series of statements over and over again until a condition is met.
for i = 1, #people do
What this line does is set up a for loop. A for loop will run from X to Y and perform the code within it. In the example above it goes from the number 1
to #people
.
#people
is the length of the array people
, so if we had five players, #people
would be 5
.
So let's show a more simpler loop so you can see what happens:
for i = 1, #people do
echo(people[i])
end
So what the loop will do is create a variable i
, set it to 1
and then execute the code below it until it reaches the end of the block and then it will set i
to 2
and do the same, until i
is equal to #people
. So if there were three people in the match it would do:
echo(person[1])
echo(person[2])
echo(person[3])
So if you wanted to display the numbers from one to one hundred:
for i = 1,100 do
echo(i)
end
Which would become:
echo(1)
echo(2)
echo(3)
...
All the way up to echo(100)
.
There are other ways to loop through variables, they are outlined here.
So getting back to the loop set up in the script at the start, we should now be able to tell that it will run some instructions for each player, but what exactly does it do? Here is the inside of the for loop block:
person = people[i]
if(person == "hampa") then
echo("HOLY POOP IT'S HAMPA!!")
elseif(person == "blam") then
echo("Blam? Phhs I can beat Blam!")
else
echo("Oh it's just " .. person .. ", they suck!")
end
The first line should hopefully make sense, it's creating the variable 'person' and putting in this variable the i
th element of the array. So if we mapped it out:
person = people[1]
...
person = people[2]
...
So we know now that we have a loop that goes through every player's name and creates a variable person
to easily look get the current player in the loop.
The line that comes after this, is called an if-then statement.
if(person == "hampa") then
An if statement will run the instructions in it's block IF a condition is met. In this case it checks if data in the variable person
is equal to "hampa"
. It does this using the ==
operator, which will be true
if person does equal "hampa" and false
if person doesn't.
Now after this we have the code that is to be executed if person
== "hampa"
a simple echo statement. After that though we have an elseif
. An else
or an elseif
statement will run if the condition is NOT met. So in this case if person
isn't equal to "hampa"
.
The else if will only fire if the condition after it is met, like a normal if statement. So for:
elseif(person == "Blam") then
is just like a normal if statement that will only be called when the person isn't hampa. So for the echo under this to run, we must meet two conditions:
person ~= "hampa"
person == "blam"
The next part of the if statement is a plain else
. This will fire when the above two AREN'T met, aka if the person is neither "hampa" or "blam".
We then have to end the code block with an end
. This signifies the end of the else
set of instructions. As the elseif
and else
blocks automatically end the initial if
block we don't need to put an end
for them. It's automatically added by Lua.
There are other conditional operators listed here.
You are given an array of five different mods:
mods = {"aikido", "judo", "wushu", "lenshu", "sumo"}
You have to create a function that will set the current mod to one of the mods specified in the array using an index. The function should look like:
function set_mod(i)
And should take a number as parameter i
, where i
is the position in the array the mod is. Aka set_mod(2)
should set the mod to judo
.
The lua function run_cmd
will run a command in the chat, such as
run_cmd("join aikido3")
run_cmd("reset")
You will need this for test #3!
Remember you can concatenate strings using two dots: ..
script = "test"
run_cmd("ls " .. script .. ".lua")
-- Runs 'test.lua'