Forum

> > CS2D > Scripts > "Table" example
Forums overviewCS2D overview Scripts overviewLog in to reply

English "Table" example

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

old "Table" example

EndDead
User Off Offline

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

old Re: "Table" example

Infinite Rain
Reviewer Off Offline

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

old Re: "Table" example

EndDead
User Off Offline

Quote
@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)

old Re: "Table" example

Yates
Reviewer Off Offline

Quote
@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.

old Re: "Table" example

EP
User Off Offline

Quote
@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
edited 2×, last 26.12.12 11:15:57 pm

old Re: "Table" example

Flacko
User Off Offline

Quote
user Yates has written
@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.
edited 1×, last 26.12.12 11:13:06 pm

old Re: "Table" example

Yates
Reviewer Off Offline

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

old Re: "Table" example

EP
User Off Offline

Quote
@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.

old Re: "Table" example

Yates
Reviewer Off Offline

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

user Yates has written
I guess while is better to use


In this case, that is.
edited 1×, last 26.12.12 11:19:31 pm

old Re: "Table" example

Flacko
User Off Offline

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

old Re: "Table" example

EngiN33R
Moderator Off Offline

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

More >

old Re: "Table" example

Yates
Reviewer Off Offline

Quote
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.
edited 1×, last 26.12.12 11:24:56 pm

old Re: "Table" example

EP
User Off Offline

Quote
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
edited 1×, last 27.12.12 03:51:38 am

old Re: "Table" example

sheeL
User Off Offline

Quote
@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 ,

old Re: "Table" example

omg
User Off Offline

Quote
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

old Re: "Table" example

EndDead
User Off Offline

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

old Re: "Table" example

Infinite Rain
Reviewer Off Offline

Quote
@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.

old Re: "Table" example

EndDead
User Off Offline

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