Forum

> > CS2D > Scripts > What is different (2 Lua Scripts)?
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch What is different (2 Lua Scripts)?

14 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt What is different (2 Lua Scripts)?

Eternal
User Off Offline

Zitieren
Hello Everyone on us

I have a question about lua , that question is

what different about these code ??

this

Spoiler >


And this

Spoiler >


They aren't same ??

Can someone explain me??

Sorry for bad English

Admin/Mod Kommentar

"i want to know" = retarded title which tells us nothing about your thread. Try harder next time or your thread will be removed. Title fixed. /DC

alt Re: What is different (2 Lua Scripts)?

Alistaire
User Off Offline

Zitieren
1
2
3
4
5
6
7
A(a, b)
	local c = {}
	for d = 1, a do
		c[d] = b
	end
	return d
end

1
2
3
4
5
6
7
A(a)
	local c = {}
	for d = 1, a do
		c[d] = 0
	end
	return d
end

Difference;

At nr 1 you can choose the value you want the tables to have, at nr 2 you can't.

alt Re: What is different (2 Lua Scripts)?

Apache uwu
User Off Offline

Zitieren
1
2
3
4
5
6
7
8
9
10
11
function initArray(m)
	return Array(m,0)
end

function Array(size,value)
	local array = {}
	for i = 1, size do
		array[i]=value
	end
	return array
end

initArray is just a wrapper for Array.

alt Re: What is different (2 Lua Scripts)?

Alistaire
User Off Offline

Zitieren
user Apache uwu hat geschrieben
1
2
3
4
5
6
7
8
9
10
11
function initArray(m)
	return Array(m,0)
end

function Array(size,value)
	local array = {}
	for i = 1, size do
		array[i]=value
	end
	return array
end

initArray is just a wrapper for Array.


That's the dumbest script I've ever seen.

alt Re: What is different (2 Lua Scripts)?

Apache uwu
User Off Offline

Zitieren
explains it pretty well huh?

1
2
3
4
5
6
7
8
function initArr(size,value)
	value=value or 0
	local array = {}
	for i = 1, size do
		array[i]=value
	end
	return array
end

Merged the functions together, so arrays can be set to a value or defaulted to 0.

alt Re: What is different (2 Lua Scripts)?

Spook MQ Hacker
BANNED Off Offline

Zitieren
function initArray(m)
local array = {}
for i = 1, m do
array[i]=0
end
return array
end

function Array(size,value)
local array = {}
for i = 1, size do
array[i]=value
end
return array
end

My total different code.!

alt Re: What is different (2 Lua Scripts)?

EngiN33R
Moderator Off Offline

Zitieren
In short (as user Alistaire already said, mind you):

• The first function, initArray(m), allows you to create a table, an "array", of size m with all of its entries (from 1 to m) set to zero.
• The second function, Array(size,value), lets you create an "array" with the size size with all of its entries (from 1 to size) set to value.

Please do remember that there's no such thing as arrays in Lua, Lua has tables.

alt Re: What is different (2 Lua Scripts)?

Lee
Moderator Off Offline

Zitieren
Be careful with the second function as not understanding its limits will usually end up biting you in the ass. What happens when you run the following?

1
2
3
4
5
6
7
8
9
-- I need an array of points, I guess I can just use a table of length two to represent points and have them pre-initialized to be all {0,0}
arr = Array(10, {0,0})
-- Somewhere during the calculation, I needed to add 1 to each of the x components
for i=1,10 do
	arr[i][1] = arr[i][1]+1
	-- I should expect each point to contain (1,0) right?
end
-- For sanity check, is my first point still 1,0?
print(unpack(arr[1]))

The short answer to the above question is What the Fuck? Briefly speaking, don't pass in tables to the Array function.

alt Re: What is different (2 Lua Scripts)?

EngiN33R
Moderator Off Offline

Zitieren
Yes, Lee has a point - if you want to create nested tables, don't do it with the array function. Use a derivation of it or something - like this one:
1
2
3
4
5
6
7
8
9
function doublearray(m,n,k) --m is amount of first-level fields, n is of second-level ones
    local a, x, y = {}
    for x = 0, m do a[x] = {}
        for y = 0, n do
            a[x][y] = k
        end
    end
    return a
end
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht