Toribash
Original Post
How It Works, And How To Write Them
{-Scripting-}
By kChris


Scripting is the most programatically interesting and intriguing part of the Toribash experience, especially for someone with a nack for programming.
Throughout this tutorial, I'm going to attempt to get you on the right tracks for writing your very own scripts.


» Part One «
Understanding LUA


Understanding LUA's syntax is the most important, and complicated part to this entire endeavour.
The syntax isn't complicated, but compared to everything else, it's the most complicated.

Get started at https://www.lua.org/manual/5.3/manual.html.


» Part Two «
A Scripts Context


A script is ran alongside the game, and loaded when a player loads them.
They are run alongside a LUA library written by the creators and developers of Toribash, with a multitude of methods available at our disposal to get information about the current game, players, and everything in between, including firing code when certain things happen.
We will be going over these different functions later.


» Part Three «
Getting Started


To get yourself started, I'm going to introduce you to one of the many functions Toribash will allow you to utilize; echo().
This function allows us to print text to the chat that only the player running the script will see.
To get started, create a new file, name it (name of your script).lua, and open it up in your favorite editor (I use IntelliJ).
Simply write echo("Hello, world!"), save it to your "script" folder, go in game, and load your script with /ls.

You should now see "Hello, world!" in chat!


» Part Four «
The Many Functions Of The Toribash SDK


There are a lot of functions you can utilize.
Inside of the "script" folder in your Toribash, there's a folder named "sdk".
Every file name in there represents a function you can use, and inside of it, contains examples of how to use it.
Feel free to copy and paste their usage into your own script, and see what happens!

If there's enough feedback asking, I'll write a Github page with all of the functions with a more in depth descripton.


» Part Five «
Hooks


Hooks are the grandaddy meat of a script.
If you want to run code when certain things happen, like the game ending, or a command being ran, this is is what you need.

Pretty much, when an event is fired and there's a hook for it, those hooks call the methods that are attached to them, sort of like Spigot's event system if you ever written a Minecraft plugin before.

Here is a list of all of the hooks, and below will be how to use them.

Toribash Hooks



Alright! You now have access to all the hooks... now what?

I'll provide a quick, simple example of how to use a hook.

lua code:


function EndGame()
echo("")
echo("^88Game over!")
echo("")
end

--This is how we add hooks!
add_hook("end_game", "", EndGame)


Our special method here is "add_hook", which takes in 2 strings and a function.
The first string is the hook name, the second is supposed to be a description but I usually leave it blank, and the third is the name of your function.
Since I'm hooking into the end_game hook, EndGame() will be called whenever a match ends.

You could do something more fancy, like accessing get_world_state() and finding out who won, that's up to your imagination.
Most of the hooks names are self explanatory, and, if you search the sdk folder enough, you'll find examples on how to use them.



Thank you for reading!
I hope this helped.




Last edited by kchris_old3393; Apr 27, 2018 at 12:12 AM.