people playing: 86, servers online: 30, games played: 142,128,904 Top Player: Karstnator

  Toribash Community » Help » Tutorials
Register FAQ General Rules Community
Reply
 
Thread Tools Display Modes
Old Apr 26, 2018   #1
kchris
Junior Member
 
Green Belt
Join Date: Apr 2018
Posts: 9



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; Apr 26, 2018 at 11:12 PM..
kchris is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:11 AM.




Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
ragdoll fighting game physics fighting game ragdoll fighting physics funmotion joints martial arts karate pc mac free game turn based game