Forum

> > CS2D > General > LUA Tutorial [DaKnOb]
Forums overviewCS2D overviewGeneral overviewLog in to reply

English LUA Tutorial [DaKnOb]

8 replies
To the start Previous 1 Next To the start

old LUA Tutorial [DaKnOb]

sixpack
User Off Offline

Quote
Hello there Unreal Players!
I am DaKnOb and I started learning LUA before some weeks.
Now I know some basic and I can share them here with you.

This tutorial will cover the basics first and it will move on slow but steady!

NOTE:
In order to make scripting easier, I strongly recommend you to download Notepad Plus Plus by clicking below!
IMG:https://img11.imagehosting.gr/out.php/i1441421_download.png

END OF NOTE

First of all, I will teach you what an addhook is.
Addhooks are not real LUA parts, they are just custom code that only a specific game/app can understand.

The most used addhooks for CS2D are:
√ say wich is being triggered when someone says something
√ minute wich is being triggered every 1 minute
√ join wich is being triggered when someone joins the game
√ parse wich being triggered when someone types something in console
√ serveraction wich is being triggered when someone hits the F2/F3/F4 button.

So the basic syntax of an addhook command is this:
addhook("say", "chat")
Explain:
More >


Now the system will trigger "chat" everytime we say something.
But what is actually "chat"?

Chat is a part of code that's being executed ONLY when its triggered!

Lets tell the system what to do when "chat" is being triggered.

Type
function chat (id, txt)

end
Explain:
More >


So right now we have a hook called chat wich is being triggered when someone says something and right now it does nothing. Lets tell this lazy function what to do.

Inside the function (...) and the "end" type the following code:

if(txt=="!hello") then
     msg("Hello "..id)
msg("How are you?")
end

Explain:
More >


After you type this, you will have the following code:
1
2
3
4
5
6
7
8
9
10
addhook ("say", "chat")

function chat (id, txt)

         if(txt=="!hello") then
                msg("Hello "..id)
                msg("How are you?")
         end

end

YOU ARE DONE!
This is your first script!
Now to import it to game, go to File -> Save and then save it to CounterStrike-2D\sys\lua\ folder.
Click here to get there if you have CS2D on C drive
Now open server.lua with Notepad Plus Plus or with any other Text editor. In the end of the file, type
1
dofile ("sys/lua/scriptname.lua")
Replace scriptname with your script's name.

Now start CS2D with default settings and Create a server.

You can see if the script contains any errors by opening the console. If it has any red lines that say:
Error myscript.lua:01:<an error here>
Explain:
More >


If you get no red lines, it does not mean it will work 100% so don't rush to upload it without testing! An error may appear after you execute a command like
when you type !hello in this case.

As I said before, LUA won't "touch" this piece of code until the IF exists, so when you join, the IF is false so the console won't check for error there because it can't access it.
It will be accessible ONLY when you type it. If you type it and nothing happens, then its sure an error! Check console for more info.

Okay guys! Thank you for the positive response.
Now I am going to tell you what an addhook in in the function's parenthesis.

√ say hook can use only Player's ID and Player's Text

√ join hook can use only Player's ID

√ serveraction hook can use only Player's ID

√ parse hook can use only the command someone typed in console

√ minute hook can't use anything.

So the basic syntax of the function is for each one:

say
function chat (id, text)
join
function joined (id)
serveraction
function saction (id)
parse
function command (cmd)
minute
function min()
-Its important to add the marenthesis even when its empty!

But don't worry! You actually can't use just theese!
I mean when you say Hello! to a player, you can edit the code and make it say "Hello DaKnOb" instead of "Hello 1".

This can be achieved with some other variables. Check this example out.

..player(id, "name") will have as result the name of a player with ID the one you selected/system found out.

..player(id, "tilex") will have as result the number of the current Tile in X axis of a playerwith ID the one you/system choose.

--Important! Some commands require player's X and Y and not TileX and TileY. You can find the TileX/Y in the map editor but its impossible to see the X and Y in the map editor. EVERY X is bigger than a TileX. Actually its 1 TileX = 32 X!
Click below to see some example commands that use X and some that use TileX.
More >


So you have to remember! If you can't find a player information from a hook, you can find it with this simple variable.

VARIABLES
Now lets talk a little bit about variables. Lets say that you are making a roleplay script and you want the player to has some more money than normal game allows you (16.000 $).

There is no way to modify the game through LUA and change the limit so you will have to create a new variable wich handles the money.

Creating a variable is the simplest thing you 've ever done! Even simpler than addhook!

Go ahead and type in the start of the script the following code:
1
Money=0
--Make sure the variable declare is NOT between a "function ..." and an "end" else it will be set to 0 every time some triggers this function.

Now what we've done is to set the variable "money" to 0.
Since we want to add/edit/decrease the number (or do math with it) we do NOT use quotations (").

--A variable's value can be used in two ways, with quotation as a text or witout quotation as a number.

--To do math, we need to write without them. But to show a name or a text we need to add.

--Adding quotation in a variable when it has a number, is not a fault unless you want to do math with this number.

Okay. So this is the creation of a variable! Simple as that!

But of course a variable is not still! Else we wouldn't need that! A variable must change. In this case, to change a number we need to do math.

Lets say that somehow, we don't care about the functions and the addhooks right now, the player picked up some money. We need to increase player's money with a code.

In real life we would type variable + 10 = newvariable
and then newvariable + 100 = verynewvariable.

This requires a lot of variables. We MUST use one only!
So the code to do the math is:
1
money=..money+10
Explain:
More >

So we will have the variable changing from 0 to 10.

MENUs
So now that you know about variables and addhooks lets try
making something even harder. It an in-game menu. You probable seen a lot of them in Classes script/Superhero scripts etc.

A menu is more useful than commands because the player does not write a line that HAS to be exactly what you programmed. In a menu the player can see all his options and click whatever he likes.

Now in this tutorial we will not just learn how to display a menu. We will code the menu to open with a serveraction (F2 in this case), display some buttons like:
√ Kick
√ Kill
√ Trap
√ Kamikaze

So, in order to do all theese, we will learn one more addhook (except the 5 we learned in the start) and how to send console commands.

Lets go ahead and make some addhooks.

In order to activate the menu when the player hits a "serveraction" button, we need to hook a serveraction.

Just type this:
1
addhook ("serveraction", "saction")

This will hook a serveraction protocol with name saction.

Now we need to add one more hook to handle the menu.
There is a hook for that!

1
addhook("menu", "mymenu")

This is our new code. An addhook. As you probable understand its a simple code to add a variable with name mymenuwith the protocol menu.

So until now the code for our menu with opens with F2 is:
1
2
addhook("serveraction", "saction")
addhook("menu", "mymenu")

We are done with the hooks!

Lets move to the functions. The place where coding is important.

First of all we need the serveraction(F2).

go ahead and type in:
1
2
3
4
5
function saction(id, sact)
        if(sact==1) then
	        menu(id, "Pick one, Kick, Kill, Trap, Kamikaze")
        end
end
This will take the player ID and the button you pressed(sact) and then move to the code. If the pressed serveraction is 1(F2) then it will show a menu with title Pick one and items Kick, Kill, Trap, Kamikaze.

--You can add up to 9 buttons in each menu.
--If you add a Kick|Kicks you, (...) it will add as main item the Kick and as secondary item the Kicks you.

So now every time you press F2, it will instantly show up a menu wich is going to show 4 buttons.

Now lets make the "menu" function wich is going to handle them.

Type in:
1
function mymenu (id, title, button)

This is going to add the function.
As you can see we have some properties like Player's ID, Menu's Title, Player's Selection.

We can use all of theese to make out handler.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (title=="Pick one") then
      if(button==1) then
           code
      end
      if(button==2) then
           code
      end
      if(button==3) then
            code
      end
       if(button==4) then
            code
      end
end
This will handle our F2 menu.

--Each menu you make must have a different title.
--You handle menus with titles

So if someone presses the first button, system will trigger the first code, second button second code etc.

Now its time to add the code.
I didn't wrote it before, because you will learn NOW how to send console commands.

--Sending console commands is pretty easy unless you need to add a lot of variables.

The main code for a console command is:
1
parse("command");

Although we need a lot of variables to make a successful command.

In order to kill, we need the ID of the player because the command is
kill 'pid'
In order to kick we need the PID too!
In order to trap someone (spawn a wall III in his position) we need his TileX and TileY!
In order to Kamikaze (kill him and make a bomb explosion in his place) we need his ID and his X and Y.

Woohoo! Thats a lot of data!

Lets take one command at a time!

For kill we need the ID. Easy!
1
parse("kill "..id);

--We need the space after the kill word so the command will have a space between the kill word and the number (PID).

Kick is easy as well.
1
parse("kick "..id);

Now you probable noticed the ".." I add in front of every VARIABLE. Theese two dots mean the following text is a variable. You have to add the two dots in front of all variables that has at least one character before them and you have to add the two dots in the end of each variable if they have at least one character after them. So:
1
2
3
4
"kill "..id
id
id.." "..x
id.." "..x.." "..y

Now lets move to building. You can build by spawning objects. So
1
parse("spawnobject 5 "..player(id, "tilex").." "..player(id, "tiley"))
Explain:
More >


This will spawn a Wall III in his position and cause him unable to move.

Now for the kamikaze, we will add two commands at the if.
Its normal! You can add a million commands if you wish.

Go ahead and...
1
2
parse("customkill "..id.." KaMiKazE "..id);
parse("explosion "..player(id, "x").." "..player(id, "y").." 100 100");
CustomKill is a command a few people know!
It kills the player and in the kill log (The place where you see Player M4A1 Player 2) it records that the player's ID admin used first, killed with the text after it, the player's id after the text.

So in this case it will show:
KaMiKazE Player 1
because Player 1 killed Player 1!

The second command, "explosion" will create an explosion in player's X and Y! Not TileX and TileY! X and Y! with Range/Damage 100/100.
--Range of an explosion is a number that handles the range of an explosion. 50 = applies only to the tile of the explosion, 100 = applies to the tile of explosion + in every tile surrounding it.
--Damage is the HP that decreases from player's Health.

So that's all the commands! Replace them in the mymenu function and you will have the script!



SO THIS IS UNTIL NOW! WAIT FOR THE NEXT PART OF THIS TUTORIAL IN A BIT

DaKnOb
edited 5×, last 25.08.10 10:12:21 am

old Re: LUA Tutorial [DaKnOb]

BlacKJacK93
User Off Offline

Quote
thanks u for this Tutorial , i still learning lua and this notepad really help full thank u . i am waiting for Part II

old Reply

sixpack
User Off Offline

Quote
Okay guys! Thank you for the positive response.
Now I am going to tell you what an addhook in in the function's parenthesis.

√ say hook can use only Player's ID and Player's Text

√ join hook can use only Player's ID

√ serveraction hook can use only Player's ID

√ parse hook can use only the command someone typed in console

√ minute hook can't use anything.

So the basic syntax of the function is for each one:

say
function chat (id, text)
join
function joined (id)
serveraction
function saction (id)
parse
function command (cmd)
minute
function min()
-Its important to add the marenthesis even when its empty!

But don't worry! You actually can't use just theese!
I mean when you say Hello! to a player, you can edit the code and make it say "Hello DaKnOb" instead of "Hello 1".

This can be achieved with some other variables. Check this example out.

..player(id, "name") will have as result the name of a player with ID the one you selected/system found out.

..player(id, "tilex") will have as result the number of the current Tile in X axis of a playerwith ID the one you/system choose.

--Important! Some commands require player's X and Y and not TileX and TileY. You can find the TileX/Y in the map editor but its impossible to see the X and Y in the map editor. EVERY X is bigger than a TileX. Actually its 1 TileX = 32 X!
Click below to see some example commands that use X and some that use TileX.
More >


So you have to remember! If you can't find a player information from a hook, you can find it with this simple variable.

old Wow!

Passer
User Off Offline

Quote
Thx For the Toturial But I Still cant make Lua NEVER !

old Re: LUA Tutorial [DaKnOb]

sixpack
User Off Offline

Quote
Passer has written
Thx For the Toturial But I Still cant make Lua NEVER !


Thats what I first said. I stopped trying after a couple of hours. Then after 2 years, I tried to understand it again. And... There it is! It wasn't just that I learned LUA, it was that I was able to write a tutorial for it too!

Of course in theese years I learned AS3 and VB.NET but I was just kept thinking that it was impossible to learn LUA. That was the thing that prevented me from learning. I said lets give it a shot and if I still don't understand I let it forever! I downloaded a lot of scripts and saw that every singal one had the "addhook" in the start. Then I saw that every hook had a function with it and then I started to understand everything. Try once more!
To the start Previous 1 Next To the start
Log in to replyGeneral overviewCS2D overviewForums overview