In this tutorial I will generally explain how to work with action scripts.
----1 Useful information
When editing the .lua / .xml files the best editor to use is simply Notepad. Also it's very easy to use Notepad ++, scripting with this program is very easy, and comfortable. You can download notepad++ HERE
---1.1 Storing data
When scripting in ot action files you will have to understand what some thing mean. One of the important things are items in which you can store data. Here I will give an example:
Now the storage item "number" contains "1". So each time we will call up the storage value "number", we will get the answer "1".
When you'd like to perform math calculations you can do it like this:
Now there will be added 1 to the storage value number. You can replace + with -, / and some more math signs.
If you want number to be an random number it would be like this:
1,10 is the min/max number that the random init should get.(math.random is a function)
It is also possible to store text inside these storage items. An example:
Now the storage item "name" will contain the text "Hi, my name is Fare".
I highly recommend to use local in scripts. What they are supposed to? When announcing a value, (anything = 5), you creating a storage value which store "5", after script was executed, you don't need to keep this "5", becouse script was already executed. Thats why I recommend to use local values. The word ‘local’ means that the variable is only used only when executing script. As soon as the script is done, the memory is cleared again.
---1.2 "If" statements:
An if statement tests its condition and executes its then-part or its else-part accordingly. The else-part is optional.
Another words, "if" statement simply says that a piece of code between the "if" and the "end" will only be carried out when the lines of code between the "if" and the "then" is correct.
You always need to close an "if" statement with and "end".
Example:
This code will carry out the "piece of code" only if the storage value "apple" is 1. Look at the "==" part. When using an if statement and you want to compare two factors you must use the "=" signe twice instead of once!
If you want the "if" statement to compare multiple factors in one statement it will look like this:
Now the "piece of code" will only be carried out when the storage value "apple" is 1 and "pear" is 2. Of course you can put as many factors in one if statement, as long as you separate them with an "and".
If you'd like to carry out "piece of code" when the storage value "apple" is 1 OR when the storage value "pear" is 2 simply replace the "and" by an "or".
Let's say you want to execute "piece of code1" when the storage value "apple" is 1, and "piece of code2" when the storage value "apple" is not 1. It will have to look like this:
You simply put an "else" between the "if ... then" and the "end". Everything which is between the "if .. then" and the "else" will be carried out if the storage value apple IS 1, everything which is between the "else" and "end" will be carried out when and store value apple is NOT 1.
Now, what if you want "piece of code1" to be carried out when the storage value "apple" is 1, and "piece of code2" when the storage value "apple" is 2. When apple is neither 1 nor 2 “piece of code3” will be carried out. Example:
If the storage value "apple" is 1 "piece of code1" will be carried out, but if the storage value "apple" is 2 ONLY "piece of code2" will be carried out!
When the apple is neither 1 nor 2 "piece of code 3" will be carried out.
You can put as many "elseif...then"'s between the "if...then" and the "else" as you want.
If you want nothing to happen when the apple is neither 1 or 2 simple remove the(else "piece of code3")
When you are using "==" between apple and 1(if apple == 1 then) you are comparing. So you say, if the storage value of "apple" is 1 then ....
When you use the "==" sign, the part in front of the "==" (apple) most of the time represents a storage item.
But it would be nice if we could do other things then comparing factors! Therefore we can place a few different things between the storage value and then thing you want to compare (or something else) it to.
Examples:
-
(will only be carried out if the storage value "apple" is bigger then 1)
-
(will only be carried out if the storage value "apple" is smaller then 1)
-
(will only be carried out if the storage value "apple" is not 1)
-
(will only be carried out if the storage value "apple" is 1, or bigger then 1)
-
(will only be carried out if the storage value "apple" is 1, or smaller then 1)
---1.3 "For" statements
For statements are completly different then if statements. Here is an example:
Now, I will explain a bit.
"doPlayerSendTextMessage(cid,18,apple)" is an function (more about this later) which will send a message to the player with a text. Now the text which will be send is the contains of the storage item "apple". In this code the for statement will create a loop. It will repeat "doPlayerSendTextMessage(cid,18,apple)" until the storage value apple is 10.
Here nr1 is the starting number, nr2 is the finishing number. The for loop will automatically increase the value of the storage item "apple" each time it repeats the loop.
---1.4 Repeat statements
Repeat statements will repeat a piece of code until something reaches a value. Example:
the “piece of code” wil be repeated untill the storage value NumberOfApples is bigger then the storage value NumberOfPears. Ofcourse you can change NumberOfPears into a number.
Credits goes to Mindstorm!
I just found this tutorial and I think it can be usefull, so I updated, completed, and posted it here.
- Useful information (read when you are new with this)
- Lua functions
----1 Useful information
When editing the .lua / .xml files the best editor to use is simply Notepad. Also it's very easy to use Notepad ++, scripting with this program is very easy, and comfortable. You can download notepad++ HERE
---1.1 Storing data
When scripting in ot action files you will have to understand what some thing mean. One of the important things are items in which you can store data. Here I will give an example:
Code:
number = 1
When you'd like to perform math calculations you can do it like this:
Code:
number = number + 1
If you want number to be an random number it would be like this:
Code:
number=math.random(1,10)
1,10 is the min/max number that the random init should get.(math.random is a function)
It is also possible to store text inside these storage items. An example:
Code:
name = "Hi, my name is Fare"
I highly recommend to use local in scripts. What they are supposed to? When announcing a value, (anything = 5), you creating a storage value which store "5", after script was executed, you don't need to keep this "5", becouse script was already executed. Thats why I recommend to use local values. The word ‘local’ means that the variable is only used only when executing script. As soon as the script is done, the memory is cleared again.
---1.2 "If" statements:
An if statement tests its condition and executes its then-part or its else-part accordingly. The else-part is optional.
Another words, "if" statement simply says that a piece of code between the "if" and the "end" will only be carried out when the lines of code between the "if" and the "then" is correct.
You always need to close an "if" statement with and "end".
Example:
Code:
if apple == 1 then
"piece of code"
end
This code will carry out the "piece of code" only if the storage value "apple" is 1. Look at the "==" part. When using an if statement and you want to compare two factors you must use the "=" signe twice instead of once!
If you want the "if" statement to compare multiple factors in one statement it will look like this:
Code:
if apple == 1 and pear == 2 then
"piece of code"
end
Now the "piece of code" will only be carried out when the storage value "apple" is 1 and "pear" is 2. Of course you can put as many factors in one if statement, as long as you separate them with an "and".
If you'd like to carry out "piece of code" when the storage value "apple" is 1 OR when the storage value "pear" is 2 simply replace the "and" by an "or".
Let's say you want to execute "piece of code1" when the storage value "apple" is 1, and "piece of code2" when the storage value "apple" is not 1. It will have to look like this:
Code:
if apple == 1 then
"piece of code"
else
"piece of code2"
end
You simply put an "else" between the "if ... then" and the "end". Everything which is between the "if .. then" and the "else" will be carried out if the storage value apple IS 1, everything which is between the "else" and "end" will be carried out when and store value apple is NOT 1.
Now, what if you want "piece of code1" to be carried out when the storage value "apple" is 1, and "piece of code2" when the storage value "apple" is 2. When apple is neither 1 nor 2 “piece of code3” will be carried out. Example:
Code:
if apple == 1 then
"piece of code1"
elseif apple == 2 then
"piece of code2"
else
"piece of code3"
end
If the storage value "apple" is 1 "piece of code1" will be carried out, but if the storage value "apple" is 2 ONLY "piece of code2" will be carried out!
When the apple is neither 1 nor 2 "piece of code 3" will be carried out.
You can put as many "elseif...then"'s between the "if...then" and the "else" as you want.
If you want nothing to happen when the apple is neither 1 or 2 simple remove the(else "piece of code3")
When you are using "==" between apple and 1(if apple == 1 then) you are comparing. So you say, if the storage value of "apple" is 1 then ....
When you use the "==" sign, the part in front of the "==" (apple) most of the time represents a storage item.
But it would be nice if we could do other things then comparing factors! Therefore we can place a few different things between the storage value and then thing you want to compare (or something else) it to.
Examples:
-
Code:
if apple > 1 then
-
Code:
if apple < 1 then
-
Code:
if apple ~= 1 then
-
Code:
if apple >= then
-
Code:
if apple <= then
---1.3 "For" statements
For statements are completly different then if statements. Here is an example:
Code:
for apple 1,10 do
doPlayerSendTextMessage(cid,18,apple)
end
Now, I will explain a bit.
"doPlayerSendTextMessage(cid,18,apple)" is an function (more about this later) which will send a message to the player with a text. Now the text which will be send is the contains of the storage item "apple". In this code the for statement will create a loop. It will repeat "doPlayerSendTextMessage(cid,18,apple)" until the storage value apple is 10.
Code:
for apple [B]nr1[/B],[B]nr2[/B] do
---1.4 Repeat statements
Repeat statements will repeat a piece of code until something reaches a value. Example:
Code:
Repeat
“piece of code”
Until NumberOfApples > NumberOfPearsNow
the “piece of code” wil be repeated untill the storage value NumberOfApples is bigger then the storage value NumberOfPears. Ofcourse you can change NumberOfPears into a number.
Credits goes to Mindstorm!
I just found this tutorial and I think it can be usefull, so I updated, completed, and posted it here.
Last edited: