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

ERROR: Attempt to index ? (a nil value) Can't figure it out?!

Started by BreezerFly, 25 May 2012 - 01:45 AM
BreezerFly #1
Posted 25 May 2012 - 03:45 AM
Heya… I have trouble with some code, for some reason it doesn't work for me. It says it's trying to index ? (a nil value)
This is what I'm doing:

file named gfx (an api)

local w,h = term.getSize()
function getSize()
w,h = term.getSize()
return w,h
end

My 'runnable' file

os.loadAPI(apiDir.."pvar")
os.loadAPI(apiDir.."gfx")
function drawTermScreen()
term.clear()
local w,h = gfx.getSize()
gfx.outlineBox("*", 1, 1, w / 2, h) --Left side box
gfx.outlineBox("*", w / 2 + 1, 1, w, h / 2) --Right upper side box
gfx.outlineBox("*", w / 2 + 1, h / 2 + 1, w, h) --Right side lower box
end
drawTermScreen()
os.unloadAPI(apiDir.."pvar")
os.unloadAPI(apiDir.."gfx")

I know the gfx functions work if I'm just plotting in numbers. But I'm getting that error when I do local w,h = gfx.getSize()
PixelToast #2
Posted 25 May 2012 - 03:55 AM
try declaring apiDir first
for some reason the api isnt loading properly

and why do you need
local w,h = term.getSize()
at the beginning?
ComputerCraftFan11 #3
Posted 25 May 2012 - 04:04 AM

local w,h = term.getSize()
function getSize()
w,h = term.getSize()
return w,h
end

Just do this:


function getSize()
return term.getSize()
end
Luanub #4
Posted 25 May 2012 - 04:05 AM
This seems very unneeded, you could just simply do

w,h = term.getSize()


without the need of the API.

The error looks like the API is not loaded. Try doing this to verify they load correctly when the program starts.


if not os.loadAPI(apiDir.."pvar") then
error("API pvar did not load properly")
end
if not os.loadAPI(apiDir.."gfx") then

error("API gfx did not load properly")
end
BreezerFly #5
Posted 25 May 2012 - 01:17 PM
EDIT: Thanks a lot luanub, it's right that it wasn't loaded, was my apiDir that was initiated wrong :)/>/>

The api is loading properly, as I said, all these work just fine if I input numbers into 'em
gfx.outlineBox("*", 1, 1, w / 2, h) --Left side box
gfx.outlineBox("*", w / 2 + 1, 1, w, h / 2) --Right upper side box
gfx.outlineBox("*", w / 2 + 1, h / 2 + 1, w, h) --Right side lower box

And yeah, I do have a need for an API, since it's part of a bigger gfx API that I'm doing. It is loading properly too, the reason I'm declaring w,h = term.getSize() is because the API needs the width and height for some of its functions.

EDIT: Thanks for the quick replies tho!

EDIT2: My bad actually, it could seem like my apis isn't loading properly. (I'm on SMP now, I tested it all on SSP before…)
My apiDir is set to
local apiDir = "apis/"
I have the gfx and the pvar api both in that folder.
But both are in the Breeze subfolder, is that a problem? Do I need to include the whole dir?