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

Attempt to call nil on paintutils.drawPixel()

Started by crazyadam0, 24 February 2014 - 04:29 PM
crazyadam0 #1
Posted 24 February 2014 - 05:29 PM
I seem to basically only get attempt to call nil errors and they never seem to make sense until you guys point out my apparent stupidity. I am beginning to make a very simple shapes drawing API and all of the sudden I am noticing an error when calling the rectFill function. Note that this is being used as an API

My error is draw:28: attempt to call nil

All the code that you need to see is:

local col = term.setTextColor;
local bgc = term.setBackgroundColor;
local cpos = term.setCursorPos;
line = paintutils.drawLine;
pixel = paintutils.drawPixel;

--A few functions that wont help you

function rectFill(x1,y1,x2,y2,fillColor)
	for x = x1, x2 do
		for y = y1, y2 do
			pixel(x, y, fillColor) --LINE 28 IN MY CODE
		end
	end
end
Edited on 24 February 2014 - 05:13 PM
Bomb Bloke #2
Posted 24 February 2014 - 06:01 PM
To guess, you've either set "paintutils.drawPixel" to nil before trying to assign the function to "pixel", or you've later assigned nil directly to "pixel".

The problem isn't in this code.
crazyadam0 #3
Posted 24 February 2014 - 06:08 PM
Now I am getting really odd errors. After rearranging the code and restarting Minecraft (Yes I did save in my editor) the error still appears in line 28 even though the rectFill function is being called, not the rect function where the error is now.

Full code:

local col = term.setTextColor;
local bgc = term.setBackgroundColor;
local cpos = term.setCursorPos;
line = paintutils.drawLine;
pixel = paintutils.drawPixel;

function fillScreen(fillColor) --Fills the screen with color
	bgc(fillColor);
	term.clear();
end

function textPixel(x, y, bgColor, textColor, text) --Draws a pixel with a character inside
	col(textColor);
	bgc(bgColor);
	cpos(x, y);
	write(string.sub(text, 1, 1));
end

function rectFill(x1,y1,x2,y2,fillColor) --Draw filled in rectangle
	for x = x1, x2 do
		for y = y1, y2 do
			pixel(x, y, fillColor)
		end
	end
end

function rect(x1, y1, x2, y2, fillColor) --Draw outline of a rectangle
	line(x1, y1, x2, y1, fillColor); --LINE 28 WHERE THE ERROR IS NOW
	line(x1, y2, x2, y2, fillColor); --Bottom
	line(x1, y1, x1, y2, fillColor); --Left
	line(x2, y1, x2, y2, fillColor); --Right
end

Code that calls stuff in the draw API:

local function login()
    draw.fillScreen(colors.cyan);
    draw.rectFill(10,6,42,13,colors.blue);
    cpos(24,6);
    col(colors.white);
    print("LOGIN");
end
Edited on 24 February 2014 - 05:13 PM
Bomb Bloke #4
Posted 24 February 2014 - 06:19 PM
Hrm. You didn't happen to name the script that calls the API "draw" as well, did you?

This:

    cpos(24,6);
    col(colors.white);

… should probably be:

    draw.cpos(24,6);
    draw.col(colors.white);
crazyadam0 #5
Posted 24 February 2014 - 06:28 PM
No that script is named winmc and the cpos and col function shortcuts are defined in that as well. Heres a screenshot showing all of the things you might need to know.
http://imgur.com/oXIpSuS
Edited on 24 February 2014 - 05:29 PM
Bomb Bloke #6
Posted 24 February 2014 - 06:50 PM
Maybe add some random "prints" to the API to ensure your changes are actually being heeded. For now, reboot the CC computer in between tests; don't rely on os.unloadAPI().
crazyadam0 #7
Posted 24 February 2014 - 06:59 PM
Well, nothing in rect() or rectFill() is being executed and no matter what I change, save, reboot, and test the computer says attempt to call nil on line 28. Even if the line is blank!
Bomb Bloke #8
Posted 24 February 2014 - 07:06 PM
Then that strongly suggests that the file you're editing is not the API your main script is loading.
crazyadam0 #9
Posted 24 February 2014 - 07:18 PM
*FACEPALM* I feel so dumb right now, my API is in the /.winMC/.OS/APIs folder and the one i'm editing is just in the root folder. Thanks a ton you always seem to find the answer to my problems. At first this seemed impossible and then i looked at the top of my notepad ++ screen and realized…