• 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!

Lua Questions about (semi-adv?) Lua Scripting.

Guitar Freak

_LüA_n☺b_
Joined
Dec 27, 2008
Messages
831
Reaction score
13
Location
Caracas, Venezuela
Hello there, it's been a while since I last posted a request, and this time I will request something different: Theory :thumbup:

*First fo all, sorry if this is the wrong subforum, mods feel free to move it respectively*

I want some pr0s to explain me some specific things about Lua Scripting that Im having trouble with. And Id like to improve by having some knowledge about them.

There are several questions so if you know only one of them dont worry, post it, Ill +Rep any helpful answers, even if they are questions already answered and you have a correction or a helpful extension.

So here they are, enjoy the never-ending question-encyclopedia of doom :thumbup::

--------
-----------
-------------​


1.- About "for" in general:

1.- Questions about "for" in general.

2.- About "for i = x,x".​

2.- Questions about "for i = x,x".

3.- About "in pairs".

3.- Questions about "in pairs".

4.- About "return".​

4.- Questions about "return".

5.- About "symbols" in general.​

5.- About "symbols" in general.

6.- About "locals and tables".​

6.- About "locals and tables".

7.- About "talkactions".​

7.- About "talkactions".

8.- About "General & Random stuff".​

8.- About "General & Random stuff".

Final Note: Be aware that, in the links, the questions that are numbered in red color (example: 1.1) and/or have a (Solved) at the end of them, is (therefore) because they are solved, but as said on the beginning of this post, you can still post corrections or extensions to them if you want.

-------------
-----------
---------​

Ill post more if I remember more.

I placed the questions in links so you can just look for the big titles in blue to see if you find a category where you can start to answer to keep the post shorter and easier to read (thanks Fare for the idea).

Notice Im not hoping to get them all answered right away, or ever, because they are many (and more to come probably) but it took me a while to write these questions down as detailed as possible for a better comprehension (like 2 hours, didnt think I had so many when I started ^_^), hopefully they will be easy for you to read and answer, and I and others will be able to learn eventually.

And again, if you know just 1 answer, post it! Its generally better than nothing ^_^

Ill +Rep helpful comments. However, Im aware that 6 rep power may not be enough for the possible time you could take to answer a couple of questions, so if its not enough, please do it just for the help of it :)

(Take in consideration that yes, Ive researched for them in Google and learned from some sites aswell but I decided Id rather get some explanations here with TFS-related examples to understand better about it. I also used examples from scripts of this forum for my questions)

Also Im sure other people might be wondering some of the questions here mentioned and it could help them too. That means Ill edit the question-posts with the respective answers (and answerers) as soon as I get them so others can learn too.

Thanks a lot in advance.
-Guitar Freak.
 
Last edited:
---------
-----------
-------------​

7.- About "talkactions".​

7.1- What does "string.explode(param, " ")" mean/do?

Example of this:
(Taken from Mazen's Advanced Buying Command script)

Lua:
function onSay(cid, words, param)
    par = string.explode(param, " ")
    for i, item in ipairs(items) do
                par = getFullParam(par)
        if item[1] == 1 then
            titem = par[1]
            if isNumber(par[1]) == true then
                titem = par[2]
                if tonumber(par[1]) > 100 or tonumber(par[1]) == 0 then
                    doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
                    return true
                end
            end

(Solved)

Solution by kokokoko:

About the string.explode(), here's an example:

Lua:
function onSay(cid, words, param, channel)

--splitted string will contain an array that has 2 strings. If the param was 'hello world', splitted_string[1] will contain "hello" and splitted_string[2] will contain "world"
splitted_string = string.explode(param, " ")

--This will give the player a message that says 'hello world'(in case the param was 'hello world')
rebuilt_param = splitted_string[1] .. " " .. splitted_string[2]
doPlayerSendTextMessage(cid, 22, rebuilt_param)

Sorry for the crappy example that first splits the param and then puts it back like it was again. I hope you get how it can be used. :p

Notice that the "param" I mention, are the words after the talkaction. For example, if the talkaction is /makefood, and the player does /makefood "Ham 3 to make 3 hams, then Ham and 3 are the params.

7.2- Where can I find the functions like "function getFullParam(param)" and others alike? Considering they are not on the LUA_FUNCTIONS document.
(this function is used by Mazen on the script mentioned above)



-------------
-----------
---------​
 
Last edited:
---------
-----------
-------------​

8.- About "General & Random stuff".​

8.1- What exactly is "cid"? I know, n00bish :p but theorically speaking what is it ;) Since Ive seen scripts with functions where they replace the "cid" spot with another function so I get confused. (Solved)

Answer by kokokoko:

'cid' stands for 'creatureID'.

8.2- What is the best way to make randomizations or what are the possible ways? (Solved)

Solution by kokokoko:

To select a random value, use this:
Lua:
math.random(0, 10) --min_value, max_value

In case the random thing you want is not a number, put the possible values in an array. Then, use this:
Lua:
name_of_array = {"a", "b", "c"}

print(name_of_array[math.random(1, #name_of_array)]) --The #name_of_array returns the size of our array. In this case, 3.

This will give us either 'a', 'b', or 'c' in the console.

8.3- Is it too different from scripting LUA for OT than for anything else? Only functions change or something else aswell? (Solved)

Answer by kokokoko:

At the moment, scripting in .LUA for OTServ is function orientated. If the "something else" is also function orientated, only the functions will change. However, OTServ will soon be changing to object orientated scripting.

8.4- Do you have any sites recommendation for *preferably* OT-related LUA scripting tutorials? Preferably detailed and not only basic but advanced aswell.

Answers so far:
Fare: lua-users wiki: Tutorial Directory


8.5- How long have you been LUA scripting and how did you learn? *Answer this question only if you answered at least 1 of the other questions, and its optional of course, just out of curiosity*



-------------
-----------
---------​
 
Last edited:
7.2- Where can I find the functions like "function getFullParam(param)" and others alike? Considering they are not on the LUA_FUNCTIONS document.

What you mean in this question? o.0 param, it's already full parameter which you wrote after talkaction word.

btw, you can found all lua function in sources, luascript.cpp :) Also there are some more simple functions in lib\function.lua
 
7.2- Where can I find the functions like "function getFullParam(param)" and others alike? Considering they are not on the LUA_FUNCTIONS document.

What you mean in this question? o.0 param, it's already full parameter which you wrote after talkaction word.

btw, you can found all lua function in sources, luascript.cpp :) Also there are some more simple functions in lib\function.lua

Ye man, but I mean, what is exactly that "getFullParam" function? Is it made up or something like that? Because I have looked in LUA_FUNCTIONS, in lib/function.lua, and in luascript.cpp and nothing says "getFullParam" anywhere, so how does that function work or where did he get it from?

Ive also seen scripts with stuff like:
Lua:
function hin(-)
if something then
 do something
end

I mean what the hell is function "hin", Im just guessing they have to do directly with the script but thats what is confusing me. If the "hin" thing is too confusing just use as example the "getFullParam" one, it is not a documented function but it is on that script, how/why? Thats my question :)
 
Back
Top