Skip to content

Instantly share code, notes, and snippets.

@blam23
Last active December 19, 2015 23:09
Show Gist options
  • Save blam23/6032712 to your computer and use it in GitHub Desktop.
Save blam23/6032712 to your computer and use it in GitHub Desktop.

Toribash Scripting Tutorial

Programming Basics

So here begins your journey into how to create some simple programs. We will first start out by saying how Toribash runs the script file. If we take the example in the last tutorial:

echo("Hello!")
result = 4+4
echo("4 + 4 is " .. result)

How does the text become a program?

Lets run through the process of how Toribash will deal with this file.

  1. Firstly it starts by opening up and reading the file.
  2. Once the file's contents has been read it stores the contents and closes the file.
  3. Toribash will then 'parse' the contents, what this means is that it takes each block (words, numbers, etc.) and changes them from text into a series of instructions.
  4. It then goes through the instructions step by step.

##So how does this actually work? Ok so if we keep on referring back to the code in the first paragraph, if we saved that code into a file called test.lua we can go through step by step and work out how Toribash converts the text into a script. First we have to tell Toribash to load the script:

/ls test.lua

Toribash will then open up the file 'test.lua' in 'data/scripts' and store the contents of it.

Once it has read the file and stored the contents, it will do a quick check to make sure that the file is a proper Lua Script, and then it run the first statement. A statement is a single instruction, such as "decapitate player one" or "add 2 to 4". Because the computer, and people, don't know the intricacies of the English Language the computer needs a more precise and less ambiguous way to store instructions. This is where a programming language comes in, it converts relatively easy to create and understand instructions into instructions the computer can handle. In Toribash's case the programming language is Lua, it 's very simple and easy to learn.

If we head back to our script, the first statement is

echo("Hello")

What this tells Toribash to do is to run the function echo and it provides it with a string type parameter Hello. If you don't understand what the hell I'm going on about, keep reading, if you do, you can skip ahead to the next tutorial.

Types

Because computers don't have any way to determine what something is, you have to tell it. Humans can look at a fox and go "that's a fox", a computer will have no idea. It needs a way to determine what information you are giving it, that's what types are. In Lua there are only a few different types and I will explain the two most basic here:

Strings

A string is a series of characters (letters), such as Hello my name is Blam or The winning player is: Bust3r. To tell Lua you want to use a string type, you simply surround the characters in quotation marks:

"Hello my name is Blam"
"The winning player is: "
"Bust3r"

Strings can be concatenated (combined) together using two periods .., like so:

"The winning player is: " .. "Bust3r"

Produces

The winning player is: Bust3r

Note: If you want to provide actual quotations within the string, you have to do something called escaping them. This can be done by putting a backslash, \ before the quotation mark: \". Similarly if you want a new line in your string, you can use the escape character \n. Example:

"The password is \"spaghetti\".\nIf you put it in wrong you'll die!"

Produces the string:

The password is "spaghetti".
If you put it in wrong you'll die!

Numbers

Numbers are fairly self explanatory, it is a type to store a number. You could have whole numbers such as 1, 2, or 451234 or numbers with decimal points in such as 2342.342 or 1.0035. To tell Lua we want to use a number, simply write it out, no need to use quotation marks or anything else. If you do put quotation marks around a number it will be treated as a string, meaning you won't be able to do maths on it.

You can use mathematical functions such as addition, subtraction, multiplication and division simply by writing it out like so:

234 + 235

Will add 234 and 235, giving you 469. Similarly:

100 - 23

Will take 23 away from 100, giving you 77. For multiplication you use an asterisk *

100 * 4

Will give you 400. For division you need to use a forward slash:

100 / 4

Will give you 25.

Combining strings and numbers

In order to show say, someone's score you must combine a string and a number together. A number can be converted to a string, because it's just a series of characters. You just combine the number to the string like was previously shown in the string section:

"53 + 23 = " .. 53 + 23

Produces the string

53 + 23 = 76

Note that everything in that string is stored as a character, not a number. So that 76 in the string isn't the number 76, but the two characters 7 and 6 next to each other.

Functions and Parameters

Functions are blocks of instructions, that can be reused and have information passed into them. A parameter is a way of passing in information to the function. The running of the instructions of a function is called calling the function. So if we had a function called decapitate when we called the function it could decapitate the player.

Now going back to the line echo("Hello"). echo is the name of the function, this is how Lua decides what function to run, in this case echo is a function that Toribash has provided in order for us to display strings to the user of the script. They are shown in the chat box as follows:

echo message

In order to tell Lua to actually call a function, we put down, in a statement, the function's name echo, followed by an open parenthesis (, then comes the parameters Hello if there is more than one you can separate them with a comma , and then we have to close the parenthesis ).

If there are no parameters, you just have the open and closing parentheses next to each other: close(). You need to put these parentheses so that Lua knows that you want to actually run the instructions within the function.

Line number one

Ok so now we've discovered what a fucntion is, and what a string is we can figure out what line number one actually does:

echo("Hello")

Will simply put the text Hello into the chat area of Toribash.

Line number two

But what the hell is line two doing?

result = 4+4

Ok so we can determine from our knowledge of numbers that it is adding 4 and 4, creating 8. But what is it doing with the 8? Well it is storing it in a variable called result.

Variables

A variable is a way to store information, in Lua a variable is simply a name that matches up to whatever information you put in it. To put information into a variable you simply need to have a statement as such:

variable = information

So whatever type of information, be it number or string, you put after the equals sign, it will store it inside the variable whose name is determined by the word before the equals sign. Note that variable names cannot start with numbers, or have spaces in them, so the following won't work:

0number = 0
word of the day = "pineapple"

Neither of the above examples will work correctly, you can use underscores _ instead of spaces, and simply put numbers at the end of the variable name or in the middle:

number0 = 0
word_of_the_day = "pineapple"

Or

zeroNumber = 0
wordOfTheDay = "pineapple"

How you do this is up to you, but it's best to keep it consistent so that you remember what your variables are called.

Toribash has various "built in" variables that we will get to in another tutorial, that contain information such as where each tori is, the amount of points a player has, what the mod is, etc.

Conclusion

So now that we know what variables are we should know what line number two is, and then in turn what line number three does.

So line number two: result = 4+4 creates a variable named result and stores 8 in it.

Line number three: echo("4 + 4 is " .. result) will concatenate (remember that means join) together the string 4 + 4 is and the variable result which we know from line 2 is 8, so that will make the string:

4 + 4 is 8

So then it will call the function echo and pass in the string as a parameter. Which means that Toribash should display in the chat 4 + 4 is 8.

The second test

Now then, in order to see if you've understood and digested this admittedly very large, dry, tutorial there is another test! Yipee!

You must edit the script created in the last tutorial's test, in it you must put statements that create a variable named output and store within that variable 8 times 4. The variable should then be displayed in the chat area of Toribash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment