Toribash
Original Post
Toribash Lua tutorial
Okay, first of all, if you need programming basics for Lua, either learn on your own, ask me, or go to http://lua.org and read the tutorials. This tutorial will describe things as detailed as possible, but it won't go in detail on each function.

Okay, to start off, get 2.6.

Then, go to your scripts folder:



Here, create a text document, and rename it to [name-of-your-script].lua. If asked about the extension, hit Yes, if not, go to Folder Tools, Advanced and uncheck "Hide extension for known file-types", then rename it again.



After that, paste this code into the .lua, save it, then re-open it, and read everything. The code is commented, so you can understand easily what it's about. Before reading it, though, go to http://lua.org, and go to Documentation > User Manual, and read the part about syntax (variables, etc)

-- tutorialscript.lua
-- A script that will be used in Jok's tutorial

-- The two dashes are what signal Lua not to use this line. Called "commenting".

--[[ This type of commenting stretches
across multiple
lines
like this ]]--


---------

local function start() -- this is the main function. int main() { } in C/C++, begin in Pascal, etc. 
--This is what gets run as soon as the script starts

helloworld() -- this is called a function call. It basically means it'll go to the other FUNCTION helloworld, then execute it

end -- This ends every function and statement (if then else, for - do, while - do, etc)

---------

local function helloworld() -- And this is the function that gets called. Looks almost the same as start()

local a = 1337 -- giving a variable (a) a value (1337) is called assigning, 
		  -- and is done by simply writing var = value, replacing the two with their actual values
local b = "Who is" -- almost the same as the first variable, this var, b, is a string. Basically a row of characters.
local c = "Jok is!" -- same thing here. If you print them, to make a new line, use the \n operand, writing, for example "Jok is!\n"

local d = b .. " " .. a -- This is called concatenating a string. It merges the two values, and gives out a string of the two.
				 -- In this case "Who is 1337"
echo(d) -- The echo() command writes a message to the chat area, same as if you were doing /echo in-game.

centerprint("",c,"",100) -- The centerprint() command writes three lines of text, in the middle of the screen.
				-- the first argument ("") is the first line, second (the string c) is the middle, third is the last
				-- The fourth argument (100) is the time the text will be displayed on the screen

end


----------


--Now for the tough bit. A hook is a 'function' that instead of having to call it when you use it, will be attached to the end of the game's default functions
--For example, attaching a hook to a function, the hook being "new_game", the function will be ran at every new game, with no other necessities

local function Hello()
local a = get_player_info(0) -- This function will return a table of two things, name and score. These are accessed with a.score, a.name, etc 
local b = get_player_info(1) -- Read the readme for the full list of commands

draw_text("Welcome ".. a.name ..". Good luck to ".. b.name .." as well.", 0, 300) -- this function draw text (the strings) to a specific spot on the game screen
													  -- x = 0, and y = 300. Measured in pixels

end

add_hook("new_game","unique_group_name",Hello) -- This is the actual hook. From now on, every new game will run the Hello() function at the start of the match
							   -- The "unique_group_name" can be assigned to different hooks so you can remove them all at once using remove_hooks([name])

local function pain() -- this is a test function, which will print these messages on injury of either player
local a = get_player_info(0) -- Read the info 
local b = get_player_info(1) -- For both players

if a.injury > 1000 then -- This is the basic Lua IF syntax, remember it well: if [condition] then [code-to-run] {else [code-to-run-if-condition-is-false]} end Everything in-between {} is optional
	centerprint("Ouch","","Red is hurt.",100) -- Note that it will not reset, and messages get displayed only once
else
	if b.injury > 100 then
		centerprint("Ouch,"","Blue is hurt.",100)
	end
end
end

add_hook("enter_freeze","lua_group_here",pain)

--This is it. Credits to hampa, for the awesome game, the devs for the continued production, and last but not least, the members of Toribash IRC for the motivation to write this.
--Have fun, guys.
That's it. Comments/ideas go here.

Have fun guys.
Last edited by Jok; Sep 20, 2007 at 07:47 PM. Reason: fixed [code] blocks
Re: Toribash Lua tutorial
Is there an editor that you would suggest, or should we simply use Notepad?

Also, this should be stickied, in my opinion. : D Great tutorial!
[22:26] million: someday, we're gonna destroy space-time
[22:26] million: I'd like to be partly responsible
Re: Toribash Lua tutorial
Thanks. And, you could use Notepad++, though I use Notepad, plain. Don't need syntax highlighting, for one.
Re: Toribash Lua tutorial
And where would i get this Beta =]?

-- Dw, all mighty Isha showed me the light
<@veb> HEY WHO CHANGED YOUR TITLE
<@veb> IT SAYS SENIOR MEMBER
Don't change my title you asshole
Re: Toribash Lua tutorial
Yeah, get Notepad++. Just an awesome everyday editor. Has syntax colouring for many languages and has some neat features.
don't unban, he has hard coded userid bypasses everywhere.
which is why the avatar repeats so often.

awesome tutorial, I'll have to try it out sometime.
Former Item Forger