Forum

> > CS2D > Scripts > Lua tutorial by FN_Linkin Park ~ Part 2.
Forums overviewCS2D overview Scripts overviewLog in to reply

English Lua tutorial by FN_Linkin Park ~ Part 2.

24 replies
Page
To the start Previous 1 2 Next To the start

old Lua tutorial by FN_Linkin Park ~ Part 2.

J4x
User Off Offline

Quote
Hi this is the second part of my tutorial.
------------------------------------------------------
Now we will learn more complex things, like using cs2d commands in lua,variables, loops and Tables.
------------------------------------------------------
> Loops
There are several loops in lua, some of the them are:
1
while,for

the first one we will learn to use is the while loop
this loop is very easy to use, here is an example:
1
2
3
4
5
x = 10
y = 1
while  x > y do
msg("Hi")
end
As u can see this loop works with a condition, the condition here is that while x > y something will happen, in this case the message "Hi" will appear.

Now we will learn to use the for loop. this one is harder to use, in cs2d we commonly use it when our hooks didn't have the id parameter.
example:
1
2
3
4
addhook("second","lol")
funtion lol()
msg2(id,"1")
end
this will send us a error, so to fix this error we use the for loop:
1
2
3
4
5
6
addhook("startround","lol")
funtion lol()
for i=1,32 do 
msg2(i,"Prepare to fight")
end
end
In this example i = 1 is the starting number, 32 is when the loop ends. In cs2d we use 32 as the highest number because thats the highest id, and 1 is the lowest.
------------------------------------------------------
>Variables
If u study math u must know what is a variable, a variable is just a letter or name that represents a value.
example:
1
2
3
a = 1
b = 2
a + b = 3
u can also represent other types of values:
1
2
3
4
5
6
7
8
9
player_name = "player(i,'name')"
addhook("startround","Die")
function die()
for i=1,32 do
if player_name == "Linkin Park" then
msg2(i,"hi admin")
end
end
end
as u can see the variable is "player_name" that represents the value "player(id,"name)".
------------------------------------------------------
> Cs2d commands in lua
To use a cs2d command ( a console command) in lua u just have to write parse and ur command.
1
parse("THE COMMAND")
Example:
1
parse("mp_wpndmg ak-47 50")
------------------------------------------------------
∗ If u have any doubt write it on the comments.
∗ Thanks for reading :D!
edited 3×, last 08.04.11 02:58:59 am

old Re: Lua tutorial by FN_Linkin Park ~ Part 2.

Yasday
User Off Offline

Quote
player_name = "player(i,name)"
won't work.
try
1
2
3
4
5
6
7
8
9
10
p_str = "player_ name = player(i, 'name')"
i = 0
player_name = ""
....
for n = 1, 32, 1 do
i = n
loadstring(p_str)()
....
end
...
or just the normal way with if player(i, "name") == "bla" then ... .
If you use if and you "=" a variable, the condition will be true, use == instead ( won't work with tables ( like m = {} if m == 1 then.. ) )
In the startround hook ( function lol() ) an end is missing

old Re: Lua tutorial by FN_Linkin Park ~ Part 2.

EngiN33R
Moderator Off Offline

Quote
I'm sorry, maybe I'm missing something, but
FN_Linkin Park has written
1
if weapon == My_weapons then

You try to compare number with table. There's a wonderful function to check if the table contains an element:
1
2
3
4
5
6
7
8
function contains(table, element)
    for _, value in pairs(table) do
        if value == element then
            return true
        end
    end
    return false
end
So I think the final code should be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function contains(table, element)
    for _, value in pairs(table) do
        if value == element then
            return true
        end
    end
    return false
end

My_weapons = {1,2,3,4,5}

addhook("buy","lol")
function lol(id,weapon)
if contains(My_weapons,weapon) then
msg2(id,"You bought a weapon :D")
end
end

old Re: Lua tutorial by FN_Linkin Park ~ Part 2.

DannyDeth
User Off Offline

Quote
You shouldn't post a Lua tutorial if you yourself do not understand it properly:
1
My_weapons = {"deagle,fiveseven,m4a1,ak-47"}
This is utter uselessness defined. All it will do is make one big, empty table-element called "deagle,fiveseven,m4a1,ak-47". Each string-based table-element needs to have separate double quotes, like this:
1
My_weapons = {"deagle","fiveseven","m4a1","ak-47"}

And then this?
1
2
3
4
5
6
7
My_weapons = {1,2,3,4,5}
addhook("buy","lol")
function lol(id,weapon)
if weapon == My_weapons then
msg2(id,"You buyed a weapon :D")
end
end
This would have to be:
1
2
3
4
5
6
7
8
9
My_weapons = {1=WEPS_ID,2=WEPS_ID,3=WEPS_ID,4=WEPS_ID,5=WEPS_ID}
addhook("buy","lol")
function lol(id,weapon)
	for i=1,#My_weapons,1 do
		if weapon = My_weapons[i]
			msg2(id,"You have *BOUGHT* ( not 'buyed'! ) a weapon!")
		end
	end
end

Then, the explanation of tables...
Quote
Tables are like variables, but a table can represent more than just one value.

No, in Lua, tables are *OBJECTS* in their own sense. They can contain any type of data and have what is known as 'inheritance', so one table can be copied to another with a simple this = that.

If you can't speak Chinese, don't write a book about it.
The same applies to Lua.
If you can't code in Lua properly, don't write a tutorial about it!!

old Re: Lua tutorial by FN_Linkin Park ~ Part 2.

EngiN33R
Moderator Off Offline

Quote
Spoiler >

I haz less code :C No wait, nvm
Mai code more effective :C

And
DannyDeth has written
1
2
3
4
5
6
7
8
9
My_weapons = {1=WEPS_ID,2=WEPS_ID,3=WEPS_ID,4=WEPS_ID,5=WEPS_ID}
addhook("buy","lol")
function lol(id,weapon)
     for i=1,#My_weapons,1 do
          if weapon = My_weapons[i]
               msg2(id,"You have *BOUGHT* ( not 'buyed'! ) a weapon!")
          end
     end
end

1
if weapon = My_weapons[i]
Gaaah my eyes they bleed
1
if weapon == My_weapons[i] then

old Re: Lua tutorial by FN_Linkin Park ~ Part 2.

J4x
User Off Offline

Quote
ok,ok i understand, i won't make more tutorials until i learn more lua.... So thank you guys, for correcting me :P.

NOTE: lol i can't belive i wrote buyed...
edited 3×, last 09.04.11 06:14:56 pm

old Re: Lua tutorial by FN_Linkin Park ~ Part 2.

KenVo
User Off Offline

Quote
FN your tutorials is actually helpful for who are beginners although there is some error but its ok. You should make more tutorial especially about image in lua and something more advanced

old Re: Lua tutorial by FN_Linkin Park ~ Part 2.

EngiN33R
Moderator Off Offline

Quote
You didn't understand us correctly. We didn't say 'u no write tootorials', we said (implied) that you have more practice, a LOT more, and then you'll be able to write quality tutorials with quality code, believe me I think the next tutorial you should write should be about images in Lua, as KenVo said - there are a lot of useful image functions people don't know about.

More >

old Re: Lua tutorial by FN_Linkin Park ~ Part 2.

Yasday
User Off Offline

Quote
That image stuff hasn't got anything with the original Lua scripting language, it's all about CS2Ds engine, just like the hooks and some other functions.

@FN_Linkin Park
You should definitely learn more about tables, take a look at this and this.

old Re: Lua tutorial by FN_Linkin Park ~ Part 2.

Flacko
User Off Offline

Quote
Unreal software forum -> CS2D section -> User-written Lua tutorial

Any normal person would deduct from there that this tutorial will eventually include CS2D specific functions. But you're right, Yasday, FN should state that addhook(), player(), image(), and everything else are functions supplied only by CS2D.
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview