Forum

> > CS2D > Scripts > Learn it fast, learn it simple! cs2d lua tut.
Forums overviewCS2D overview Scripts overviewLog in to reply

English Learn it fast, learn it simple! cs2d lua tut.

18 replies
To the start Previous 1 Next To the start

old Learn it fast, learn it simple! cs2d lua tut.

J4x
User Off Offline

Quote
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.
1
print("Hello world!")
output
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"
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.
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
+ (add)
- (substract
< (less than)
> (greater than)
>= (greater than or equal to)
<= (less than or equal to)
^ (exponent)
/ (divide)
% (remainder)
* (multiply)
Example:
1
1 + 2
output
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
a = 2 
b = 3
d = a+b
print(d)
output
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..
1
2
3
4
5
function pts(points)
If points == 1 then
print("The amount of points you have is 1!")
end
end
Output
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
A 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 .
- This was taken from Crash course tutorial, here the link if you want to read it later http://luatut.com/crash_course.html
----------------------------------------------------
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
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..}
instead of
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
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
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")
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.
1
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

old Re: Learn it fast, learn it simple! cs2d lua tut.

omg
User Off Offline

Quote
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

old Re: Learn it fast, learn it simple! cs2d lua tut.

J4x
User Off Offline

Quote
@user omg: The tutorial took me about an hour, Let me add those things now.

Quote
you sound like you can have magical parameters that just appear on demand and do what you have in mind
I explained cs2d gives default parameters to functions.

old Re: Learn it fast, learn it simple! cs2d lua tut.

Alistaire
User Off Offline

Quote
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;

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

old Re: Learn it fast, learn it simple! cs2d lua tut.

EngiN33R
Moderator On Online

Quote
@user Alistaire: That piece of code... Is it from Pwnisher's tutorial or is it yours? Because that is completely wrong.

1
Dog = {1, 2}

That will make a table with Dog[1] == 1, Dog[2] == 2

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.

1
2
parse("mp_roundtime 0") --This does not need anything
parse("speedmod "..id.." 100") --This does

@user J4x: Nice efforts, but you should learn it yourself first. Your old ones were wrong, this one also has errors.

List of errors >


On a side note,
1
dog_amount = {dogone = 1, dogtwo = 2} -- Variables inside a table

I hope you know how that works.

old Re: Learn it fast, learn it simple! cs2d lua tut.

J4x
User Off Offline

Quote
@user EngiN33R: Thanks, i just wrote this fast and forgot to check those things. Yeah i know how it works

@user Alistaire: the parse function does not need a hook,only if you need to declare id a anythibg else, i just wrote a wrobg example, so no im not "mental".
edited 1×, last 22.06.12 02:04:46 pm

old Re: Learn it fast, learn it simple! cs2d lua tut.

EpicCrisis
User Off Offline

Quote
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.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview