attempt to index field `?' (a nil value)...not true...help!!

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
ZORDAK
Posts: 12
Joined: Sat Jan 21, 2006 9:58 am
Location: Spain
Contact:

attempt to index field `?' (a nil value)...not true...help!!

Post by ZORDAK »

Hi all.

Im making a blocks game,making the code for the elimination cubes i got some extrange problems.

First thing i have a table o 18x9 (tablero), resuming my code, i have 2 variable casilla_X and casilla_Y, thats indicates the cell position onthe table.The function comprobar_puntos() check the horizontal and vertical cell surrounded by the pressed cell, if any of the other cell have the same color i use mira_puntos() to save the position value in a temporal buffer if the value isnt on it.The i will do the same with the values in the buffer when finish i will delete into the original table etc etc....

The code isnt finished, because it check all the board and dont skip the borders btw is not the problem, the real problem is when i check the right zone , with the 4 call, it send the correct value to mira_puntos() but when enter on it it say attempt to index field `?' (a nil value), i checked the values with a trace and values al correct and i got the values directly from the table tablero [16][3] for example and all went ok.

I dont know what to do, is very extrange.



Here is my emilination code

Code: Select all


function eliminacion()
	
  color_borrar=tablero[casilla_X][casilla_Y]
  coorx=casilla_X
  coory=casilla_Y
  
  fuera=false	
  
  indice_mirado=0
  
  repeat	
    
    indice=table.getn(temporal)+1
	
    tenemos=comprobar_puntos()
	
  	if tenemos=="no" and indice==1 then
  		fuera=true   
	else
  	  	
  		indice_mirado=indice_mirado+1   
	    
		    if string.len(temporal[indice_mirado])>2 then
		    	coorx=string.sub(temporal[indice_mirado],1,2)
		    	coory=string.sub(temporal[indice_mirado],-1)
		    else
		    	coorx=string.sub(temporal[indice_mirado],1,1)
		    	coory=string.sub(temporal[indice_mirado],-1)
		    end

		    
        end
    
       
	if indice_mirado>=indice then
		fuera=true
	end
  	
    	
  until fuera==false	
 	
end


function comprobar_puntos()
	
	local cuan=4
	local eso
	--ARRIBA DEL SELECCIONADO
 	 
	coory_a=coory-1
	coorx_a=coorx
	val=mira_puntos()
	
  	if val=="no" then
		cuan=cuan-1
	end
  	
  	--ABAJO DEL SELECCIONADO
	
  	coory_a=coory+1
	coorx_a=coorx
  	val=mira_puntos()
  	if val=="no" then
		cuan=cuan-1
	end

	--IZQ DEL SELECCIONADO
	coorx_a=coorx-1
	coory_a=coory
	val=mira_puntos()
  	if val=="no" then
		cuan=cuan-1
	end
	
	--DERECHA DEL SELECCIONADO
	coorx_a=coorx+1
	coory_a=coory
	val=mira_puntos()
  	if val=="no" then
		cuan=cuan-1
	end
	
  	if cuan==0 then
  		eso= "no"
  	else
  		eso= "si"
  		cuan=0
  	end
  	
  	return eso
end



function mira_puntos()

	local tiene=si
	
	veces=veces+1	
	
	esta=false
	if tablero[coorx_a][coory_a]==color_borrar then
		for i=1,table.getn(temporal) do
			if temporal[i]==coorx_a..coory_a then
				esta=true
			end				
		end
		if esta==false then
			temporal[indice]=coorx_a..coory_a
			esta=false
		end
    else
		tiene="no"
	end
    
    return tiene
end

I hope someone can help.[/code]
modsyn
Posts: 28
Joined: Tue Sep 27, 2005 6:02 am

Post by modsyn »

LUA table indexes start with 1, not 0 like (almost) every other language
ZORDAK
Posts: 12
Joined: Sat Jan 21, 2006 9:58 am
Location: Spain
Contact:

Post by ZORDAK »

???¿¿¿ i dont use the index 0, all the code is using 1 as first index of the array.

some help please?
ZORDAK
Posts: 12
Joined: Sat Jan 21, 2006 9:58 am
Location: Spain
Contact:

Post by ZORDAK »

28 visits ...

Nobody can help please?
soulphalanx
Posts: 35
Joined: Mon Aug 22, 2005 7:48 am

Post by soulphalanx »

i think in LUA, its [y][x], not [x][y]

dont know if that helps

ive had a problem like this before, dunno if shine changed it
ZORDAK
Posts: 12
Joined: Sat Jan 21, 2006 9:58 am
Location: Spain
Contact:

Post by ZORDAK »

Code: Select all


function crea_tablero()
		
	for x=1,18 do
	  tablero [x] = {}
	  for y=1,9 do
			tablero[x][y] = math.random(numero_colores)
	  end
	end
end

Im using x,y i can read all the values and send it to screen....dont unsderstand what happened.
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

Simple thing to double check variables is

Code: Select all

if not value then
 -- call error here
end
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Post by Shine »

The problem was solved, ZORDAK mixed strings and numbers for indexing into the table. But if I understand the problem correctly, ZORDAK wanted to fill a vertically and horizontally connected part of a board of the same color. Here is a nice recursive solution:

Code: Select all

function printBoard(board)
	local height = table.getn(board)
	local width = table.getn(board[1])
	for y = 1, height do
		local line = ""
		for x = 1, width do
			if board[y][x] == 1 then
				line = line .. "#"
			else
				line = line .. "."
			end
		end
		print(line)
	end
end

board = {
	{ 0, 0, 0, 0, 0, 0, 0, 0},
	{ 0, 0, 1, 1, 0, 1, 0, 0},
	{ 0, 0, 1, 1, 1, 1, 1, 0},
	{ 1, 1, 1, 0, 0, 1, 0, 0},
	{ 1, 0, 1, 0, 1, 0, 0, 0},
	{ 0, 0, 1, 1, 0, 0, 1, 0},
	{ 0, 1, 1, 0, 0, 1, 1, 0},
	{ 0, 0, 0, 0, 1, 1, 1, 0}}

function inBound(lower, value, upper)
	return value >= lower and value <= upper
end

function fillBoard&#40;board, x, y, fillColor&#41;
	local height = table.getn&#40;board&#41;
	local width = table.getn&#40;board&#91;1&#93;&#41;
	if inBound&#40;1, x, width&#41; and inBound&#40;1, y, height&#41; and board&#91;y&#93;&#91;x&#93; ~= fillColor then
		local cellColor = board&#91;y&#93;&#91;x&#93;
		board&#91;y&#93;&#91;x&#93; = fillColor
		return  1 +
			fillBoard&#40;board, x + 1, y, fillColor&#41; +
			fillBoard&#40;board, x - 1, y, fillColor&#41; +
			fillBoard&#40;board, x, y + 1, fillColor&#41; +
			fillBoard&#40;board, x, y - 1, fillColor&#41;
	end
	return 0
end

print&#40;"original board&#58;"&#41;			
printBoard&#40;board&#41;
local x = 4
local y = 3
print&#40;"filling at &#40;" .. x .. ", " .. y .. "&#41;"&#41;
count = fillBoard&#40;board, x, y, 0&#41;
print&#40;"filled cells&#58; " .. count&#41;
print&#40;"filled board&#58;"&#41;
printBoard&#40;board&#41;

Code: Select all

original board&#58;
........
..##.#..
..#####.
###..#..
#.#.#...
..##..#.
.##..##.
....###.
filling at &#40;4, 3&#41;
filled cells&#58; 18
filled board&#58;
........
........
........
........
....#...
......#.
.....##.
....###.
ZORDAK
Posts: 12
Joined: Sat Jan 21, 2006 9:58 am
Location: Spain
Contact:

Post by ZORDAK »

Yes, tablero [0] isnt the same that tablero["0"].

Thanks shine.
Post Reply