Forum

> > CS2D > Scripts > table error
Forums overviewCS2D overview Scripts overviewLog in to reply

English table error

3 replies
To the start Previous 1 Next To the start

old table error

iii
User Off Offline

Quote
I made a script and have a porblem with it.
I think i did somethink wrong at the generation of the table.
Google didn't give solutions...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
bombexploss={}

function asdf()
	function drawexplo()
		local bombexplos={left={},right={},up={},down={},center=nil}
		--example:
		bombexplos.center=image(asd)		
		bombexplos.right[#bombexplos.right+1]=image(asd)
		bombexplos.left[#bombexplos.left+1]=image(asd)
		bombexplos.up[#bombexplos.up+1]=image(asd)
		bombexplos.down[#bombexplos.down+1]=image(asd)
		return bombexplos
	end
	function clearexplo()
		for i=1,#bombexploss[1].bombexplos.left do
			freeimage(bombexploss[1].bombexplos.left[i])
		end
		for i=1,#bombexploss[1].bombexplos.right do
			freeimage(bombexploss[1].bombexplos.right[i])
		end
		for i=1,#bombexploss[1].bombexplos.up do
			freeimage(bombexploss[1].bombexplos.up[i])
		end
		for i=1,#bombexploss[1].bombexplos.down do
			freeimage(bombexploss[1].bombexplos.down[i])
		end
		freeimage(bombexploss[1].bombexplos.center)
		table.remove(bombexploss,1)
	end
			
	bombexploss[#bombexploss+1]=drawexplo()
	timer(500,"clearexplo")
end

asdf()

the error is :
1
LUA ERROR: sys/lua/myscript.lua:15: attempt to index field 'bombexplos' (a nil value)

can you help me?

old Re: table error

EngiN33R
Moderator Off Offline

Quote
Well, bombexploss.bombexplos was never defined. Try doing it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
bombexploss={}

function asdf()
     function drawexplo()
          bombexploss.bombexplos={left={},right={},up={},down={},center=nil}
          --example:
          bombexploss.bombexplos.center=image(asd)          
          bombexploss.bombexplos.right[#bombexplos.right+1]=image(asd)
          bombexploss.bombexplos.left[#bombexplos.left+1]=image(asd)
          bombexploss.bombexplos.up[#bombexplos.up+1]=image(asd)
          bombexploss.bombexplos.down[#bombexplos.down+1]=image(asd)
          return bombexploss.bombexplos
     end
     function clearexplo()
          for i=1,#bombexploss[1].bombexplos.left do
               freeimage(bombexploss[1].bombexplos.left[i])
          end
          for i=1,#bombexploss[1].bombexplos.right do
               freeimage(bombexploss[1].bombexplos.right[i])
          end
          for i=1,#bombexploss[1].bombexplos.up do
               freeimage(bombexploss[1].bombexplos.up[i])
          end
          for i=1,#bombexploss[1].bombexplos.down do
               freeimage(bombexploss[1].bombexplos.down[i])
          end
          freeimage(bombexploss[1].bombexplos.center)
          table.remove(bombexploss,1)
     end
               
     bombexploss[#bombexploss+1]=drawexplo()
     timer(500,"clearexplo")
end

asdf()

old Re: table error

Flacko
User Off Offline

Quote
You should declare drawexplo and clearexplo outside of the asdf() function. Otherwise you're just re-assigning them every time you call asdf(), which just makes your code ugly and inefficient.

old Re: table error

iii
User Off Offline

Quote
thx for the tip, flacko.

@user EngiN33R:
should i add the "bombexploss." to the bracket like in line 8?
1
bombexploss.bombexplos.right[#bombexploss.bombexplos.right+1]=image(asd)
and if i made "bombexploss.bombexplos", there is only 1 table of bombexplos in bombexploss and i can't save 2 bombexplos. or am i wrong?
i want a table bombexploss, which can save many bombexplos.
i thought if i create one in a function with local bombexplos and give this in bombexploss it would work. like in
1
2
3
4
5
6
7
function initArray(m)
local array = {}
	for i = 1, m do
		array[i]=0
	end
return array
end

i think i need a structure like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
overarray={}

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

function makemanyarrays()
	for s=1,7 do
	overarray[#overarray+1]=initArray(3)
	end
end

makemanyarrays()
but i don't know how to do it with my more complicated script

edit : i found the solution with my last code.
in the function clearexplo i must delate all bombexplos.
now it is working.
sry EngiN33R for wasting your time for my help, because i don't thought enough...
edited 1×, last 16.03.13 07:36:15 pm
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview