In this episode you will learn how easy is coding in Lua.
If you have coded before, this is gonna be ultra easy for you.
Software and where to place the scripts:
I use
Notepad++ for script editing, as it has language formatting, and lets you save a file to any format.
Its free, very fast, and works with a lot of languages.
(I will not teach how to use it in this tutorial, as there are a lot out there)
If you want to run a script on toribash, you should place the
.lua file here:
%Toribash Directory%/data/script
Once you place it there, go to toribash main menu (ESC), go to
setup>util>scripts and select your script from the list.
Variables
Variables are data that can be accesed and modified at runtime by the script.
They are the first thing im teaching as they are required by almost any script.
Variables can contain
numbers,
strings (text),
functions (functions are variables after all) and
tables (They can contain multiple type of other variables).
Variables can be local or global, in toribash scripting, locals are recommended to prevent interacting with variables out of your script.
Now i will make it a bit more clear:
Defining variables in Lua is as simply as saying that something is something.
lua code:
That will define the global test as "something"
Defining variables as globals is dangerous, and its recommended that you define them as locals, doing so is very easy:
lua code:
local anotherTest = "something"
Now anotherTest is defined as a local, wich is safe, as it only can overwrite variables inside the script.
Variables types:
Variables can store:
Numbers:
lua code:
Strings:
lua code:
Tables:
lua code:
local myInfo = {name = "Peter", age = 23}
Functions:
local function doSomething(arg1,arg2)
echo(arg1)
echo(arg1..arg2)
end
Functions and Hooks:
These two should go in the same chapter, as they both require each other in toribash lua coding.
Functions:
Functions store a chunk of code, inside of it you can define variables and run other functions.
Every single instruction that has () at the end is a function
Ex: print("stuff to the screen")
Unlike in other languages, Lua knows when a function ends, you dont need to put ; or anything similar.
If you have read the stuff before you will know that a function is basically a variable that contains code.
To define one we should do the same as with variables:
local function doStuff(arg1,arg2,arg3)
--Code goes here
end
So here is the "anatomy" of a function:
Everything between [] is optional.
Everything between {} is writen by you
[local] function {function name}([Arguments you can give to the function])
[Code here]
end
Arguments are custom data you can give to the function to make it more dynamic, they have to be separated by commans, and they are basically, variables. Arguments are local to the function that defines them, they cant be accesed from other functions in the script.
Making functions:
Take a look at this, it simply prints what you give to "arg1" into the toribash chat:
lua code:
local function putStuff(arg1, arg2)
print(arg1)
end
That should be easy to understand, but, anyway, here is the explanation:
lua code:
local function putStuff(arg1)
It defines putStuff as a function wich has 2 arguments: arg1 and arg2
lua code:
It runs the function "print" with arguments: arg1
(print is a global function defined by toribash!)
lua code:
Tells the script that the function ends there.
As you can see we dont use arg2, but there is no problem in leaving an argument with no use.
Global functions are functions that can be accesed by any script, they are defined by just not placing the local before function...
Example of a gobal function:
lua code:
draw_line(number x, number y, number x2, number y2, number width)
--Draws a line
Check: %ToribashFolder%/data/script/sdk
for more functions and their usage.
Hooks:
WIP will update it!