Forum

> > CS2D > Scripts > 3darray
Forums overviewCS2D overview Scripts overviewLog in to reply

English 3darray

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

old 3darray

Ultimate programmer
BANNED Off Offline

Quote
You can not belive,but I need lua help.
I need to make 3d array( a[32][20][20]) but can not.

Please,help

old Works

Ultimate programmer
BANNED Off Offline

Quote
I can not belive my eyes.It's works.Wait my next script(it will be cool:))

no It does not work as I want:
1
2
3
4
5
6
7
8
mmpmap = {}
mmpmap[32] = {}
mmpmap[32][20] = {}
mmpmap[32][20][20]=1
pl=4
x=4
y=6
mmpmap[pl][x][y]=5		--error
edited 1×, last 03.08.11 10:13:44 pm

old Re: 3darray

Flacko
User Off Offline

Quote
This function will initialize an array of any amount of dimensions with all it's values set to v.
1
2
3
4
5
6
7
8
9
10
function table.array(v, ...)
	local t = {}
	if arg[2] then
		v = table.array(v,table.unpack(arg,2))
	end
	for i=1,arg[1] do
		t[i]=v
	end
	return t
end
Therefore, table.array(1,32,20,20)
Will create a three dimensional array with all it's values set to 1.

old Re: 3darray

Ultimate programmer
BANNED Off Offline

Quote
it can not understand table.unpack!
1
2
3
4
5
6
7
8
9
10
11
12
13
table={}
function table.array(v, ...)
     local t = {}
     if arg[2] then
          v = table.array(v,table.unpack(arg,2))
     end
     for i=1,arg[1] do
          t[i]=v
     end
     return t
end

mmpmap = table.array(0,32,20,20)

old Re: 3darray

Flacko
User Off Offline

Quote
Oh, nevermind, my code doesn't work as it should because I forgot Lua holds tables by reference.

Btw, table.unpack is just the unpack() function.

Your easiest bet is now to make a triple loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
function threedarray(v,d1,d2,d3)
	local t = {}
	for i=1, d1 do
		t[i] = {}
		for j=1,d2 do
			t[i][j] = {}
			for k=1,d3 do
				t[i][j][k] = v
			end
		end
	end
	return t
end

Or atleast something like that should work.
edited 1×, last 04.08.11 12:11:05 pm

old Re: 3darray

Klin
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
function threedarray(v,d1,d2,d3)
	local t = {}
	for i=1, d1 do
		t[i] = {}
		for j=1,d2 do
			t[i][j] = {}
			for k=1,d3 do
				t[i][j][k] = v
			end
		end
	end
	return t

I think, this should work.

Klin

old Re: 3darray

Apache uwu
User Off Offline

Quote
You can check if the table is set so you don't run into errors...

1
2
3
4
5
6
7
8
9
10
a={}
a[23]={}
a[23][42]={}
a[23][42][77]="lol"

if type(a[23])=="table") and type(a[23][42])=="table") and type(a[23][42][77])=="table") and a[23][42][77]~=nil then
	print("it exists!")
else
	print("nope try again!")
end

old Re: 3darray

Lee
Moderator Off Offline

Quote
type(a[23][42][77])=="table" should not be part of the if condition.

3D arrays are usually very memory intensive. For example, a 3D array of dimensions 23x42x77 requires the allocation 74382 empty cells, each of which takes up 8 bytes on the stack and a proportional amount of memory somewhere else in memory.

If you only need a few points in a large 3D space, try using this scheme:

{point1, point2, point3, ...} where each point is a table containing the x, y, and z coordinate of that point.

old Re: 3darray

Apache uwu
User Off Offline

Quote
Well you're not going to really have all of them set now are you?

Are you saying having a table like...


a[23-42-77]=true
a[33-42-65]=true

etc etc

old Re: 3darray

Ultimate programmer
BANNED Off Offline

Quote
I need 3d array because DC have made stupid way to drow image-createimage->deleteimage.But if he will make drawimage(img,x,y) as in engines.i will not need it

old Re: 3darray

DannyDeth
User Off Offline

Quote
A 3D array can be intialised very easily:
1
my_3d_array = {{{}}}
But, as Lee said, the memory it takes it rather large, and you should really be careful in it's usage.

old Re: 3darray

DannyDeth
User Off Offline

Quote
A pixmap.. so, what are you going to do with it EXACTLY? Like build a map overview or smth?
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview