Learn it fast, learn it simple! cs2d lua tut.
18 replies



22.06.12 03:15:05 am
Hello newbies at scripting, today I bring this tutorial for you!
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.
output
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.
----------------------------------------------------
Lets get to strings:
Strings are basically blocks of text
We can put two or more pieces of text together in an operation known as concatenation. To do this, we place .. (two dots) between two pieces of string.
----------------------------------------------------
Now lets go with maths! Yeah lua is able to solve math operations, you can use diferent numerical operators in lua.
Example:
output
----------------------------------------------------
Now lets go with variables! Variables are useful to make thing simple, we can store words, numbers, or even other varibles.
output
You can see in the examples above that the variable d stores the result between the variables a and b which results as 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..
Output
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:
----------------------------------------------------
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.
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
----------------------------------------------------
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.
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.
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
The parse function execute ordinary cs2d commands through lua, it does not need a hook or a function, unless you need to declare something in inside it.
----------------------------------------------------
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.
----------------------------------------------------
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.
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.
Code:
1
print("Hello world!")
output
Code:
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.
Code:
1
print("Hello world!") -- My first script
----------------------------------------------------
Lets get to strings:
Strings are basically blocks of text
Code:
1
"hello friends"
We can put two or more pieces of text together in an operation known as concatenation. To do this, we place .. (two dots) between two pieces of string.
Code:
1
"Hello" .." ".. "World"
----------------------------------------------------
Now lets go with maths! Yeah lua is able to solve math operations, you can use diferent numerical operators in lua.
Code:
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)
- (substract
< (less than)
> (greater than)
>= (greater than or equal to)
<= (less than or equal to)
^ (exponent)
/ (divide)
% (remainder)
* (multiply)
Example:
Code:
1
1 + 2
output
Code:
1
3
----------------------------------------------------
Now lets go with variables! Variables are useful to make thing simple, we can store words, numbers, or even other varibles.
Code:
1
2
3
4
2
3
4
a = 2
b = 3
d = a+b
print(d)
b = 3
d = a+b
print(d)
output
Code:
1
5
You can see in the examples above that the variable d stores the result between the variables a and b which results as 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..
Code:
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
If points == 1 then
print("The amount of points you have is 1!")
end
end
Output
Code:
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.
Code:
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
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
Code:
instead of 1
dog = {1,2,3,etc..}
Code:
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.
Code:
1
2
2
addhook("startround","blah")
function 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.
Code:
1
2
3
4
2
3
4
addhook("startround","blah")
function blah()
msg("Hello world") -- Remember strings always use ""
end
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
Code:
1
parse("mp_wpndmg deagle 100")
The parse function execute ordinary cs2d commands through lua, it does not need a hook or a function, unless you need to declare something in inside it.
----------------------------------------------------
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.
Code:
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..
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
you use "==" without specifying what it does or why u used it
you use tables without specifying how to access them
you have code but you dont explain what it does very well, like the startround hook function. as far as beginners can tell, it says "hello world", but arent sure how or why it happens. they tend to get the tunnel vision on the msg() part because thats what they recognize the most
you dont explain/break down the parse function very well, nor how you concatenate variables to strings
you dont explain functions well
you sound like you can have magical parameters that just appear on demand and do what you have in mind
you dont show the potential of coding through any meaningful examples
you dont even reference the command lists in sys/lua; only the basic commands u listed here
you use tables without specifying how to access them
you have code but you dont explain what it does very well, like the startround hook function. as far as beginners can tell, it says "hello world", but arent sure how or why it happens. they tend to get the tunnel vision on the msg() part because thats what they recognize the most
you dont explain/break down the parse function very well, nor how you concatenate variables to strings
you dont explain functions well
you sound like you can have magical parameters that just appear on demand and do what you have in mind
you dont show the potential of coding through any meaningful examples
you dont even reference the command lists in sys/lua; only the basic commands u listed here
will code for food
@
omg: The tutorial took me about an hour, Let me add those things now.

Quote:
I explained cs2d gives default parameters to functions. you sound like you can have magical parameters that just appear on demand and do what you have in mind
but you dont say where to find the parameters different functions have, just that parameters exist which isnt very useful
will code for food
I already listed hooks at the cs2d web page, there you can see the parameters, also this a simple lua tutorial, maybe I'm going to explain all that in the second part.
Well this ain't gonna make you understand Lua fast. Useless tutorial to me, too much random talk and your love for print(), which only prints something in the console.
Also, you say addhook() and function ..() need to be placed close to eachother. That's not true.
Even worse is how you explain tables;
Also, you say addhook() and function ..() need to be placed close to eachother. That's not true.
Even worse is how you explain tables;
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Dogone = 1
Dogtwo = 2
Dog = {1, 2}
-- Would make a table with Dog[1] and Dog[2], but not Dog[1] = 1 nor Dog[2] = 2
--~~~~~~
Dog = {1, 2, 3, 'etc'}
-- Would make a table Dog with values Dog[1], Dog[2], Dog[3] and Dog['etc']. Everything is a nil.
Dogone = 1 Dogtwo = 2 Dogthree = 3 Dogetc = 'etc'
-- Would make actual values.
--~~~~~~
--"The parse function execute ordinary cs2d commands through lua, it does not need a hook or another function, but it can be used together with them."
--What the fuck, are you mental? You need to declare id first.
addhook('kill', 'killspeed')
function killspeed(id)
parse('speedmod '..id..' 20')
end
Dogtwo = 2
Dog = {1, 2}
-- Would make a table with Dog[1] and Dog[2], but not Dog[1] = 1 nor Dog[2] = 2
--~~~~~~
Dog = {1, 2, 3, 'etc'}
-- Would make a table Dog with values Dog[1], Dog[2], Dog[3] and Dog['etc']. Everything is a nil.
Dogone = 1 Dogtwo = 2 Dogthree = 3 Dogetc = 'etc'
-- Would make actual values.
--~~~~~~
--"The parse function execute ordinary cs2d commands through lua, it does not need a hook or another function, but it can be used together with them."
--What the fuck, are you mental? You need to declare id first.
addhook('kill', 'killspeed')
function killspeed(id)
parse('speedmod '..id..' 20')
end
@
Alistaire: That piece of code... Is it from Pwnisher's tutorial or is it yours? Because that is completely wrong.
That will make a table with Dog[1] == 1, Dog[2] == 2
That will make a table with Dog[1] == 1, Dog[2] == 2, Dog[3] == 3 and Dog[4] == 'etc'.
And he's right - parse actually does not need anything with it; the string like that which you provided in your example needs an id parameter.
@
J4x: Nice efforts, but you should learn it yourself first. Your old ones were wrong, this one also has errors.
On a side note,
I hope you know how that works.

Code:
1
Dog = {1, 2}
That will make a table with Dog[1] == 1, Dog[2] == 2
Code:
1
Dog = {1, 2, 3, 'etc'}
That will make a table with Dog[1] == 1, Dog[2] == 2, Dog[3] == 3 and Dog[4] == 'etc'.
And he's right - parse actually does not need anything with it; the string like that which you provided in your example needs an id parameter.
Code:
1
2
2
parse("mp_roundtime 0") --This does not need anything
parse("speedmod "..id.." 100") --This does
parse("speedmod "..id.." 100") --This does
@

On a side note,
Code:
1
dog_amount = {dogone = 1, dogtwo = 2} -- Variables inside a table
I hope you know how that works.
I code, therefore I exist. | Visit my blog for Lua tips and other interesting info
It helped me a bit. I unterstand now why lua is written like this. Now I need to learn how to use all these hooks, parameters etc correctly.
Tutorial is nice, but this basics are very simple.
EngiN33R fixed errors in it very well.

Trust me, I'm an engineer |
DC approved
Super extra mod for CS2D (64), yeah!



Actually I can only make it up metaphorically, kinda something like this:
If you have received a kill,
Function kill then add 1 point,
So, 0+1 point = 1 point,
Then message "You have received 1 point for killing ...!",
End of function.
It may be wrong but this is the only way I can make it out of.
If you have received a kill,
Function kill then add 1 point,
So, 0+1 point = 1 point,
Then message "You have received 1 point for killing ...!",
End of function.
It may be wrong but this is the only way I can make it out of.
Another Day Another Shitload Of Shit
@
EpicCrisis: Read some tutorials and the scripts in the sample folder.

yeah, not everyone knows how to read correctly, let alone spell correctly. Just make a video of how.



