First we will learn what is a hook. A hook is not a real Lua part is just a code that a game or application can understand. In cs2d we have different hooks, here are some examples :
second: This hook is trigered every second.
Spawn: This hook is triggered everytime a player spawns
Start round: This hook is trigered every time that round starts.
Everytime we will use a hook we have to add the following Things: the word addhook and 2 ". So if i like to use the second hook i have to write this:
1
addhook("second","lol")
1
addhook("second","FN")
----------------------------
Now i will explain u the functions. A function is always written under the hook, and the name of the hook is used as the function, in this case "lol" will be our function. So now we will write our hook + our function :
1
2
2
addhook("second","lol") function lol()
the () are parts of the function, inside the () we add the function parameters, this parameters are default ones, u will find them inside the info.txt archive inside the lua folder. Now lets make our first script In our first script we will use the start round hook,so here we go! Lets say that u want to make a script that everytime that a round starts it sends u a message. We will start our code with the hook and the function:
1
2
2
addhook("startround","lol") <---Start round is the Hook and lol the hook's name. function lol()<---- Some hooks didn't have parameters, in this case the startround hook didn't has a paremeter.
1
- msg("text")
So our script is
1
2
3
4
2
3
4
addhook("startround","lol") funtion lol() msg("Prepare to fight") end
----------------------------
now lets do something more dificult. I want to make a script that makes that when i am terrorist it sends me a message.
Now the things will be diferent, we will have to use our [bTeam hook[/b] and the if statement. Why the if satement? because the script will work only if u are terorrist. So lets start:
1
2
3
4
5
6
2
3
4
5
6
addhook("team","wtf") function wtf(id,team) <--- We can add more than 1 paremeter in the () if player (id,"team") == 1 then the msg2(id,"You are terrorist!") end end
---------------------
Now lets go with the errors.
Normally if a script dont works, a message in red will appear in the console [to open the console pers the ~ key or just write /console].
NOte: Cs2d has to be open to open the console...
Common erros are this ones:
<eof> expected near <end>
end expected to close at line 49 near <eof>
But wtf is <eof>? eof means end of file.
How do we solve this problems?
the first one is saying us that there is a unnecessary end in our file(a extra end), what do we do? we remove a end. example
1
2
3
4
5
2
3
4
5
addhook("startround","lol") funtion lol() msg("Prepare to fight") end end <----- this is end is unnecessary, so we have to remove it
The second one is the contrary of the first one.
it says us that our script lacks a end, so we have to add another end to our script. example:
1
2
3
4
5
6
2
3
4
5
6
addhook("team","wtf") function wtf(id,team) if player (id,"team") == 1 then the msg2(id,"You are terrorist!") end <--- here we should add a end to solve the problem.
Note : if u dont understand something write in the coments ;D.
Common question:
1.I dont found the info.txt! dont worry here it is, just save it in ur desktop.
http://www.mediafire.com/?m6efd522u8hpovo
2.What u teach me here is everything about lua?
Of course not, lua is a complex language, and it would be to hard to long, to teach it all to you, but u cand find a few tutorials in google.
3. Only cs2d uses lua?
no, a lot of engines, aplications and games use lua, note that each game and application uses lua in a different way.
edited 17×, last 04.04.11 12:30:38 am