This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Engineer's profile picture

Table does not get created

Started by Engineer, 13 June 2013 - 08:32 AM
Engineer #1
Posted 13 June 2013 - 10:32 AM
Hello dear programmers!

Today I was working on my GUIbuilder and figured out that my term API overwrite was not working as I was expecting.
I came to the conclusion, that a table doesnt get created. The program is starting to complain at line 69, Im assuming it will also error on lines 70 and 71.

I create the table via a metatable, wich you can see on line 16. I immediatly create the table, and that does work (It is not starting to complain). But when I call line 69, it says: "attempt to call nil"

I had this error for quite a few days now, and I cant seem to figure out the problem. Also, the file.writeLine's are for debugging.

For those who like to count:
Spoiler

-- Load layering system with screen system
local layer = { 
	currLayer = 1;
	layers = 0;
}
local screen = {}
setmetatable( screen, { 
	__call = function()
		layer[#layer + 1] = {}
		for word in tostring( objects ):gmatch("[^%s]+") do
			layer[#layer][word] = {}
		end
		layer.layers = layer.layers + 1
		
		screen[layer.layers] = {}
		for x = 1, w do
			for y = 1, h do
				screen[layer.layers][x] = {}
				screen[layer.layers][x][y] = {}
				screen[layer.layers][x][y]["background"] = 32768
				screen[layer.layers][x][y]["text"] = 1
				screen[layer.layers][x][y]["char"] = " "
			end
		end;
	end
})
screen()


-- Screen tracking, partial overwrite term API
local backgroundColour = colours.black
local textColour = colours.white

if not term.native then
	term.native = {}
	term.native.setBackgroundColour = term.setBackgroundColour
	term.native.setTextColour = term.setTextColour
	term.native.write = term.write
end

local tbackground = term.setBackgroundColour
local ttext = term.setTextColour
local twrite = term.write

term.setBackgroundColour = function( col )
	term.native.setBackgroundColour( col )
	backgroundColour = col
end

term.setTextColour = function( col )
	term.native.setTextColour( col )
	textColour = col
end

term.write = function( s )
	if type( s ) ~= "string" then error("term.write no string", 2) end
	local x, y = term.getCursorPos()
	pcall( fs.delete, "error.txt" )
	local file = fs.open( "error.txt", "a")
	for i = 0, ( s:len() > w - x - 1 and w - x - 1 or s:len() ) - 1 do
		--[[screen[layer.currLayer][x + i][y].char = s:sub( i+1, i+1 )
		screen[layer.currLayer][x + i][y].text = textColour
		screen[layer.currLayer][x + i][y].background = backgroundColour]]
		file.writeLine( i..": LAYER "..layer.currLayer.." X "..x + i.." Y "..y)
		file.writeLine( tostring(screen[layer.currLayer]) )
		file.writeLine( tostring(screen[layer.currLayer][x + i]) )
		file.writeLine( tostring(screen[layer.currLayer][x + i][y]) )
		file.writeLine( tostring(screen[layer.currLayer][x + i][y]["background"]) )
		file.writeLine( tostring(screen[layer.currLayer][x + i][y]["char"]) )
		file.writeLine( tostring(screen[layer.currLayer][x + i][y]["text"]) )
		file.writeLine( "\n" )
	end
	file.close()
	term.native.write( s )
end

Or if you dont feel like counting: http://pastebin.com/hzQfHndT

Thank you very much in advance!
GopherAtl #2
Posted 13 June 2013 - 10:53 AM
at line 18, you replace the [x] table with an empty table inside the y loop. screen[layer.layers][x] should be initialized in the x loop, before entering the y loop; as it is, the final table will have [x] tables containing only the last y value.
theoriginalbit #3
Posted 13 June 2013 - 10:55 AM
BTW this will never be run as term.native always exists, without term.native the term api wouldn't work, term.native is what actually renders to the device.

if not term.native then
  term.native = {}
  term.native.setBackgroundColour = term.setBackgroundColour
  term.native.setTextColour = term.setTextColour
  term.native.write = term.write
end
GopherAtl #4
Posted 13 June 2013 - 11:02 AM
yeah, was about to come back and point that out, tob. In fact, it's not at all clear to me how this code works at all, after looking more closely at it. w and h don't seem to be defined, so the initialization loop should error out at line 16.
theoriginalbit #5
Posted 13 June 2013 - 11:04 AM
oh yeh, it actually does, to get it to run I had to add
local w, h = term.getSize()
at the top of the code, I didn't even think of saying that :P/>
Engineer #6
Posted 13 June 2013 - 01:27 PM
at line 18, you replace the [x] table with an empty table inside the y loop. screen[layer.layers][x] should be initialized in the x loop, before entering the y loop; as it is, the final table will have [x] tables containing only the last y value.
I knew it was a silly mistake! Thank you very much! :D/>

BTW this will never be run as term.native always exists, without term.native the term api wouldn't work, term.native is what actually renders to the device.

if not term.native then
  term.native = {}
  term.native.setBackgroundColour = term.setBackgroundColour
  term.native.setTextColour = term.setTextColour
  term.native.write = term.write
end
Hmm.. Good point. Never knew that it actually renders the screen. Thank you! :)/>

yeah, was about to come back and point that out, tob. In fact, it's not at all clear to me how this code works at all, after looking more closely at it. w and h don't seem to be defined, so the initialization loop should error out at line 16.
I should have put in that part. I figured it was not in the whole code, only in this part where it was erroring. On the top I have so called 'global variables'