I've decided tto make a new one because my old ones were too simple or they were wrong.
----------------------------------------------------
So let's start with something simple, I'm going to explain you some lua functions before I start with cs2d. You'll need a syntax highlighter or any text editor.
1- Notepad ++: http://notepad-plus-plus.org/download/v6.1.3.html
2 - Scite: http://www.scintilla.org/SciTE.html
Or you can simply use windows notepad.
--------------------------------------------------
So our first program will be a simple script that says "Hello world". Lua has diferent functions, one of them is the print function, like its name says it will print any text,words,etc. Usually text needs quotation marks (""). So with what we have learn until now lets make our script.
1
print("Hello world!")
1
Hello world!
There you can see our output is "Hello world!" this is what the print function does, it prints whats inside the parenthesis (), in this case a the hello world sentence. A function is a piece of code that does anything.
Yay! our first script. Really simple and not really useful, so lets move on. As you can see Lua is really simple to use, you can add comments to your script by simply writing two dashes (--). This could be really useful to describe what are you doing in your script or to remeber some important information.
1
print("Hello world!") -- My first script
Lets get to strings:
Strings are basically blocks of text
1
"hello friends"
1
"Hello" .." ".. "World"
Now lets go with maths! Yeah lua is able to solve math operations, you can use diferent numerical operators in lua.
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
+ (add) - (substract < (less than) > (greater than) >= (greater than or equal to) <= (less than or equal to) ^ (exponent) / (divide) % (remainder) * (multiply)
1
1 + 2
1
3
Now lets go with variables! Variables are useful to make thing simple, we can store words, numbers, or even other varibles.
1
2
3
4
2
3
4
a = 2 b = 3 d = a+b print(d)
1
5
----------------------------------------------------
Now lets go whit conditions. Don't worry it's nothing complicated, in lua you can use diferent condition in your scripts, there a more conditions, but because it is the most used one, we will see will be the if condition, as the word says, only if the condition stated is done then the script will run..
1
2
3
4
5
2
3
4
5
function pts(points) If points == 1 then print("The amount of points you have is 1!") end end
1
The amount of points you have is 1!
In the code above you can see I used the == operator, This works this way:
The == operator returns a boolean value if both of it's parameters are true.
As you can see above I used the word "function". Here an explanation:
Quote
- This was taken from Crash course tutorial, here the link if you want to read it later http://luatut.com/crash_course.htmlA function is created by writing function and its name. In our case, our function is called pts. The object that is passed into a function is called a parameter. When you create a function, you have to know what you need to pass into the function, therefore you need to explicitly state what the parameters are. You define the parameters by enclosing them between parenthesis after the name of the function. In this case, our parameter is an object called points .
----------------------------------------------------
And the last one to get to cs2d! Tables, as you saw a few paragraphs above lua uses variables, well tables, as variables do, store data, but in a diferent way, a table is a group of elements that starts with a left bracket ({) and ends with a right bracket (}). elements of this table are separated using commas. Tables can store more than one element at the same time, they can store variables and other tables too.
1
2
3
4
2
3
4
dogone = 1 dogtwo = 2 -- Variables dog = {1,2} -- A table dog_amount = {dogone = 1, dogtwo = 2} -- Variables inside a table
Tables can save time, instead of writing a lot of variables, you can simply use a table. If we want to use 50 different dogs we can use
1
dog = {1,2,3,etc..}
1
dog_one = 1 dog_two = 2 dog_three = 3
Finally we will go to cs2d! Yeah we can now start with cs2d...
If you have opened any script in the cs2d folder you'll realize that there is a "addhook" word . Hooks are not real lua parts they are just a piece of code that a game or aplication uses. Hooks are followed by functions. Hooks are defined by the game, like if we want to do something at the start of the round we have to use the start round hook and add a name to the hook just like in functions, remeber that parameters are given by the game.
1
2
2
addhook("startround","blah") function blah()
Some hooks don't use any paremeters with functions as in the case of the startround hook. After the function we can start our script.
1
2
3
4
2
3
4
addhook("startround","blah") function blah() msg("Hello world") -- Remember strings always use "" end
At the end of each chunk we have to use the end word to tell the game our script ends there.
A list of all hooks and its parameters are here http://www.cs2d.com/help.php?hookcat=all
----------------------------------------------------
Cs2d has also console commands that can be used in cs2d by simply using the parse function. lets say we want to change the weapon damage, it would be like this
1
parse("mp_wpndmg deagle 100")
----------------------------------------------------
As I wrote above function have parameters, parameters are really useful, they can help use to save time with variables, lets say the function lol has the x and y parameters, we dont need to define those variables anymore.
1
2
3
4
5
6
7
2
3
4
5
6
7
addhook("move","lol") function lol(x,y,id) if x == 1 and y == 2 then ... end end -- no need to declare x variable, it already is a parameter, in the case x represents players x coordinates in the map, same goes with y..
Glossary:
String: Strings are basically blocks of text.
Chunks: A block of code
Parameters: The object that is passed into a function
Hooks: a piece of code that a game or aplication uses
Table: a group of elements that starts with a left bracket ({) and ends with a right bracket (}).
Boolean: in most computer programming languages, a boolean may refer to or represents either true or false.
-----------------------------------------------------
I thinks that's all for now, thanks for reading this tut, have fun scripting!
-----------------------------------------------------
If you have any error with your script feel free to write it on the comments.
EDIT: Fixed some erros on the examples.
edited 4×, last 23.06.12 04:49:34 am