• Vote in May Mapping Competition to select a winner!
  • There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Guide to scripting with Lua

Infernum

Asshole
Support Team
Joined
Feb 14, 2015
Messages
5,456
Best answers
540
Reaction score
3,316
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:

zxmatzx

Intermediate OT User
Joined
Dec 1, 2010
Messages
265
Best answers
23
Reaction score
107
Location
Brazil
Very good content! Easy reading and very well explained! Waiting for next chapters.
 

Evil Puncker

its just a flu
TFS Developer
Premium User
Joined
May 30, 2009
Messages
6,583
Best answers
77
Reaction score
2,283
very nice, I'm enjoying it, keep it up!
 

Shadowsong

Game Developer & Graphic Designer
Joined
Feb 23, 2010
Messages
3,381
Best answers
18
Reaction score
2,693
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

Asshole
Support Team
Joined
Feb 14, 2015
Messages
5,456
Best answers
540
Reaction score
3,316
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:

roriscrave

Intermediate OT User
Joined
Dec 7, 2011
Messages
919
Best answers
23
Reaction score
114
Very Nice Bro!!!
I read All and im waiting for a News.
 
OP
Infernum

Infernum

Asshole
Support Team
Joined
Feb 14, 2015
Messages
5,456
Best answers
540
Reaction score
3,316
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:

Kulavy

New Member
Joined
Apr 3, 2017
Messages
5
Best answers
0
Reaction score
0
Good Job ! Very helpful and easy to understand o_O
 
OP
Infernum

Infernum

Asshole
Support Team
Joined
Feb 14, 2015
Messages
5,456
Best answers
540
Reaction score
3,316
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

Intermediate OT User
Joined
Dec 7, 2011
Messages
919
Best answers
23
Reaction score
114
good job man, u are helping me a lot!
when you get married i dance!
 

Keal Atis

Well-Known Member
Joined
Feb 1, 2017
Messages
184
Best answers
1
Reaction score
85
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

Asshole
Support Team
Joined
Feb 14, 2015
Messages
5,456
Best answers
540
Reaction score
3,316
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

Excellent OT User
TFS Developer
Support Team
Joined
Sep 7, 2015
Messages
2,104
Best answers
93
Reaction score
932
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
Premium User
Joined
Apr 3, 2011
Messages
382
Best answers
9
Reaction score
112
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

Asshole
Support Team
Joined
Feb 14, 2015
Messages
5,456
Best answers
540
Reaction score
3,316
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.
 

Zak.Tibiam

New Member
Joined
Mar 25, 2012
Messages
147
Best answers
0
Reaction score
3
@Delusion

Could you make a tutorial explaining how to apply c ++ in Tibia? A more straightforward tutorial would help thanks a lot!
 
Top