Forum

> > CS2D > Scripts > "Table" example
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch "Table" example

22 Antworten
Seite
Zum Anfang Vorherige 1 2 Nächste Zum Anfang

alt "Table" example

EndDead
User Off Offline

Zitieren
Hello all.
As some of you know, I'm learning how to script. And i'm still a newbie, I'm trying to figure out how to use a table like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
EXAMPLE = {
	{43, 23, 24} -- TileX, TileY, Item ID
	{13, 43, 45} -- TileX, TileY, Item ID
[...]
}
	--Question here--

addhook("use","enddead_use") -- "Use" is for buttons
function enddead_use(id)
--(Get table)
(for [1] in table set X)
(for [2] in table set Y)
(for [3] in table get Item ID)
(if [1] and [2] is true then)
equip(id, [3])
-- Do the same for other tables (And for new tables)
While ([1]) = the first number in a table, and ([2]) same but 2nd number, and same for ([3]) but for the 3rd number in the table.

Thanks in advance~

alt Re: "Table" example

Infinite Rain
Reviewer Off Offline

Zitieren
Well you have to loop through the table, to know if the coordinates right or not.

Use for loop.

Example of loop
1
2
3
for id = 1, 32 do
	msg(id)
end
Will give you
Zitat
1
2
...
30
31
32


id will be updated by 1 each time till it hits 32

so using loop you can scan table.

1
2
3
4
5
6
-- #<table> will return you the ammount of elements in your table
for tid = 1, #EXAMPLE do
	if EXAMPLE[tid][1] == player(id, 'tilex') and EXAMPLE[tid][2] == player(id, 'tiley') then
		parse('equip '.. id ..' '.. EXAMPLE[tid][3])
	end
end


Hope you understand me, and if not...
Then

alt Re: "Table" example

EndDead
User Off Offline

Zitieren
@user Infinite Rain: Yeah, I understood (At least most of what you said )
I'm trying that tomorrow, its pretty late here Thanks anyway.
When I try, I will give my results (And any other questions if I had)

alt Re: "Table" example

Yates
Reviewer Off Offline

Zitieren
@user Infinite Rain: I guess while is better to use as with for you will need to enter an ending number plus you can give it its own adding value. While loop stops if it is nil; and you can always break it like for.

Then again a working code is a good code. So use what Factis posted and you can learn the other things later.

alt Re: "Table" example

EP
User Off Offline

Zitieren
@user Yates: while is an indefinited iterative function, which is not recommended to use in case of looping a table.

If you're going to use while to loop a table then your code will look like this:

1
2
3
4
5
i = 1
while i < #table do
	--Block
	i = i + 1
end

Therefore it's better to use for to loop a table because it simplifies the code.

@user Flacko: Haha
2× editiert, zuletzt 26.12.12 23:15:57

alt Re: "Table" example

Flacko
User Off Offline

Zitieren
user Yates hat geschrieben
@user Infinite Rain: I guess while is better to use as with for you will need to enter an ending number plus you can give it its own adding value. While loop stops if it is nil; and you can always break it like for.

Then again a working code is a good code. So use what Factis posted and you can learn the other things later.


No it's not
1
for var=val, max[, increment]
is the syntax for the for loop, being the increment variable optional. In most cases you won't need a while loop (unless you don't know what will be your max)

@user EP: Damn, you ninja'd me twice.
1× editiert, zuletzt 26.12.12 23:13:06

alt Re: "Table" example

Yates
Reviewer Off Offline

Zitieren
But if he is going to add more tile positions/item ids then wouldn't it be better to use a while loop instead of having to keep changing the max of the for loop? As while will end itself if the return value is nil.

alt Re: "Table" example

EP
User Off Offline

Zitieren
@user Yates: What if a value of the table is supposed to be nil? The loop will break there, if you use for instead it will loop through the table depending on it's lengh, not the value.

alt Re: "Table" example

Yates
Reviewer Off Offline

Zitieren
But he is going to use tile positions and item ids, that is why I said in my first post;

user Yates hat geschrieben
I guess while is better to use


In this case, that is.
1× editiert, zuletzt 26.12.12 23:19:31

alt Re: "Table" example

Flacko
User Off Offline

Zitieren
If he's going to add more indices to his table, he should better use
1
2
3
for i=1, #t do
	stuff
end
That way all that lua has to do is get the table length once (table lengths are stored in a special index so it's just like accessing a key) and increment i every loop

On the other hand
1
2
3
4
5
var i=1
while t[i] do
	stuff
	i = i+1
end
Lua has to index the table every loop, plus it will have to create the var i at a higher scope and increment it, once again, every loop

While loops tend to be slower than for loops in lua.

alt Re: "Table" example

EngiN33R
Moderator Off Offline

Zitieren
To add to user Flacko's post - while loops are indeed slower. I did some benchmarking, and the results are in the More box.

Damn, six posts while I was doing this. You are too quick, guys.

Mehr >

alt Re: "Table" example

Yates
Reviewer Off Offline

Zitieren
Oh, all I knew is how to use them and how they were both different, I did not think about the speed nor did I really know how they execute (In such detail as Flacko posted).

Thanks I guess.
1× editiert, zuletzt 26.12.12 23:24:56

alt Re: "Table" example

EP
User Off Offline

Zitieren
Best way @user EndDead: might use to loop his table is pairs method.

1
2
3
4
5
6
7
8
9
10
11
12
EXAMPLE = {
     {43, 23, 24}, -- TileX, TileY, Item ID
     {13, 43, 45} -- TileX, TileY, Item ID
}
addhook("use","_use")
function use(id,x,y)
	for k,v in pairs(EXAMPLE) do
		if x == v[1] and y == v[2] then
			parse("equip "..id.." "..v[3])
		end
	end
end

Anyways there are much ways to loop tables
1× editiert, zuletzt 27.12.12 03:51:38

alt Re: "Table" example

sheeL
User Off Offline

Zitieren
@user EP: Change

1
2
3
4
EXAMPLE = {
     {43, 23, 24} -- TileX, TileY, Item ID
     {13, 43, 45} -- TileX, TileY, Item ID
}

to

1
2
3
4
EXAMPLE = {
     {43, 23, 24}, -- TileX, TileY, Item ID
     {13, 43, 45} -- TileX, TileY, Item ID
}

You forgot the ,

alt Re: "Table" example

omg
User Off Offline

Zitieren
oh yeah, good catch...accessing the local counter will be slower than accessing the for loop counter because its in a higher scope. didnt even occur to me

in a situation like this, pairs would be the best choice since accessing keys is faster than indices

alt Re: "Table" example

EndDead
User Off Offline

Zitieren
Er, Much information.
Thanks all. Gonna open this thread from time to time to learn what I have missed. And I'm trying it soon and giving my results (And any other questions related).

Thanks again~

alt Re: "Table" example

Infinite Rain
Reviewer Off Offline

Zitieren
@Yates
You always are able to get table's ammount of elements, therefore for loop is better than while loop to use in this case.

alt Re: "Table" example

EndDead
User Off Offline

Zitieren
I still seem not to be able to make a working one
Could anyone post a working example?

(BTW, I'm not planing to use this method on any script. Just as an example to learn from it)

@user EP: LUA ERROR: Attempt to call a nil value .. ? hmm
Zum Anfang Vorherige 1 2 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht