Guide to scripting with Lua

Infernum

Senator
Joined
Feb 14, 2015
Messages
5,624
Solutions
558
Reaction score
3,787
I've been working on a book which aims to provide a way to learn for anybody looking to learn Lua. The end goal is to have a book that lets anybody go from zero knowledge to being experienced, and to provide even just 1 thing that somebody could learn, even if the reader is experienced.
Hopefully what I'm writing here will be easier to understand than anything given or explained by the official Programming in Lua book, or any explanation on lua-users.
This book isn't so people can learn scripting for TFS. I believe it is infinitely more important to learn the language itself rather than learning just enough to get by and create nooby scripts in TFS. Learning the language itself and applying that knowledge to TFS will be much easier in the long run than not knowing what you're doing and simply just copy/pasting code (this is exactly how I learned and how I got here today!).
This book is not finished, but I have finished all of the basics that anybody looking to learn will need, and there will be much more to come.
If you're here to learn, tell me what you don't understand! It will help me improve and be able to provide a better explanation for future readers. Experienced readers, alert me of any typos or mistakes I may have made, or if you have any recommendations on analogies or examples, let me know!
I believe anybody, even if you're unfamiliar with programming, can learn at least how to do basic things in any language. All you have to do is have an open mind and apply yourself.

You can view the book here:
 
Last edited:

Shadowsong

Game Developer & Graphic Designer
Joined
Feb 23, 2010
Messages
3,431
Solutions
21
Reaction score
2,897
Location
Bosnia & Herzegovina
Extremely simple and easy to understand, like Lua itself is. This will come in handy as a reference for helping anyone who wants to get into OT development and further using Lua.

I'm currently organizing a bit of a boot-camp for new developers on my project, and have been explaining these things to them through wiki entries and discord notes, but having a book like this, explained in layman's terms is much easier.

It's interesting to see that most of us tend to use almost the exact analogies for explaining stuff like tables and variables. 😅
 
OP
Infernum

Infernum

Senator
Joined
Feb 14, 2015
Messages
5,624
Solutions
558
Reaction score
3,787
Update: I've added examples for the return keyword in Control Structures, and added the length (#) operator in the Operators page. Also added a new example for functions under Types to declare a function inside of a table like all of the standard libraries do (table.insert, string.format, etc). Made drafts for Closures, Iterators, and Recursion.

I've also introduced the rest of the layout of the book so you can see what it'll look like in the end, but I'll more than likely be writing the concepts before manually rewriting the Lua manual to include examples of each standard lib function.
 
Last edited:
OP
Infernum

Infernum

Senator
Joined
Feb 14, 2015
Messages
5,624
Solutions
558
Reaction score
3,787
Metatables, Weak Tables, Memoization, and Object-Oriented Programming now have rough drafts.

The Object-Oriented Programming page will be especially useful for those looking to understand how the new TFS scripting style works in 1.0+. Memoization as well for anybody trying to optimize scripts.

Since the Concepts chapter is now 'finished' (roughly), I will be working on the Auxiliary/standard library chapters.
 
Last edited:
OP
Infernum

Infernum

Senator
Joined
Feb 14, 2015
Messages
5,624
Solutions
558
Reaction score
3,787
Been a while since I've written. Added debugging and errors to the Auxiliary chapter, wrote table and basic pages under the Standard Libraries chapter.
All of that was written within an hour, let me know if it's confusing or could use better examples.
Standard Libraries

Auxiliary
 

roriscrave

Advanced OT User
Joined
Dec 7, 2011
Messages
1,093
Solutions
33
Reaction score
181
good job man, u are helping me a lot!
when you get married i dance!
 

Keal Atis

Intermediate OT User
Joined
Feb 1, 2017
Messages
215
Solutions
1
Reaction score
124
I have read what you have published so far and it is good.

Some of my suggestions:

I think that images for "fact(3)" and "fib(5)" would be better.


rawequal - something is wrong with your example. It prints two times 'false'
Shouldn't it be something like that?
Code:
function eqfunc(a, b)
    print('Checking equality ( '.. tostring(a) ..' vs '.. tostring(b) ..' )')
end

local t = {}
setmetatable(t, {__eq = eqfunc})

local t2 = {}
setmetatable(t2, {__eq = eqfunc})

print(t == t2) -- Checking equality ( table: 00f28b00 vs table: 00f28a10 )
print(rawequal(t, t2)) -- nothing, __eq is not invoked

load - not working for me, maybe something like that:
Code:
local f = load([[return function() print(123) end]])
local _, p = pcall(f)
p()

Thanks for your tutorial and keep going, waiting for more! :D
 
OP
Infernum

Infernum

Senator
Joined
Feb 14, 2015
Messages
5,624
Solutions
558
Reaction score
3,787
I have read what you have published so far and it is good.

Some of my suggestions:

I think that images for "fact(3)" and "fib(5)" would be better.


rawequal - something is wrong with your example. It prints two times 'false'
Shouldn't it be something like that?
Code:
function eqfunc(a, b)
    print('Checking equality ( '.. tostring(a) ..' vs '.. tostring(b) ..' )')
end

local t = {}
setmetatable(t, {__eq = eqfunc})

local t2 = {}
setmetatable(t2, {__eq = eqfunc})

print(t == t2) -- Checking equality ( table: 00f28b00 vs table: 00f28a10 )
print(rawequal(t, t2)) -- nothing, __eq is not invoked

load - not working for me, maybe something like that:
Code:
local f = load([[return function() print(123) end]])
local _, p = pcall(f)
p()

Thanks for your tutorial and keep going, waiting for more! :D
I thought so too, but I feel like images would be more of cleaning up the book, I'm just trying to get most of it finished.

That metatable example was wrong, I just went back and fixed it (they should both be using the same metatable), but the output should be the same in this case:
Code:
Checking equality ( table: 00778880 vs table: 00778998 )
false
false

Third one is indeed a mistake, I'm testing locally on 5.3 although I'm writing for 5.1, I've gone back and fixed it (it should be loadstring).
 
Last edited:

Nekiro

Legendary OT User
TFS Developer
Joined
Sep 7, 2015
Messages
2,447
Solutions
110
Reaction score
1,633
Good guide, keep up the great work!
Just a small thing, I think in concatenation section you could include string.format, so people know that its possible to concatenate differently than using dots.
 

Alpha

Relentless
Joined
Apr 3, 2011
Messages
568
Solutions
29
Reaction score
350
Location
South Korea
Good guide, keep up the great work!
Just a small thing, I think in concatenation section you could include string.format, so people know that its possible to concatenate differently than using dots.
Using string formatting should be the standard way of doing it anyway because you can enforce types + it performs better anyway because lua makes a copy of the string each time you use ..
 
OP
Infernum

Infernum

Senator
Joined
Feb 14, 2015
Messages
5,624
Solutions
558
Reaction score
3,787
Using string formatting should be the standard way of doing it anyway because you can enforce types + it performs better anyway because lua makes a copy of the string each time you use ..
It's all preference depending on the situation, you don't need to force string.format on every single string you're joining N strings to another.

Good guide, keep up the great work!
Just a small thing, I think in concatenation section you could include string.format, so people know that its possible to concatenate differently than using dots.
I don't see the point in introducing a standard lib function in Fundamentals that'll be explained in under the Standard Libraries chapter, especially when string.format isn't related to the concat operator.
I suppose it could also go inside of the Optimizations page as well since it's faster than the concat operator.
 
Top