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

'attempt to call nil' first time after boot

Started by nolongerexistant, 23 June 2013 - 08:53 AM
nolongerexistant #1
Posted 23 June 2013 - 10:53 AM
So I made a few buttons that work fine, mostly.
When I reboot the computer and try to click one of them for the first time it errors with 'attempt to call nil', and after that it works fine.


local buttons = {
	[1] = {sX = 1, eX = 13, sY = 04, eY = 06, txt = "Btn 1",   cmd = btn1},
	[2] = {sX = 1, eX = 13, sY = 08, eY = 10, txt = "Btn2",	cmd = btn2}
}

function handleMenu()
	while true do
		event = {os.pullEvent()}

		if event[1] == "mouse_click" then
			for k, v in ipairs(buttons) do
				if event[3] >= buttons[k].sX and event[3] <= buttons[k].eX and event[4] >= buttons[k].sY and event[4] <= buttons[k].eY then
					buttons[k].cmd()
				end
			end
	   end
	end
end

function btn1()
	-- Write function
  writeStr("Btn1", 1, 1, colors.white)
end

function btn2)
	-- Write function
  writeStr("Btn2", 1, 1, colors.white)
end
Engineer #2
Posted 23 June 2013 - 11:34 AM
First make the function, then call it;

function btn1()
		-- Write function
  writeStr("Btn1", 1, 1, colors.white)
end

function btn2()
		-- Write function
  writeStr("Btn2", 1, 1, colors.white)
end

local buttons = {
		[1] = {sX = 1, eX = 13, sY = 04, eY = 06, txt = "Btn 1",   cmd = btn1},
		[2] = {sX = 1, eX = 13, sY = 08, eY = 10, txt = "Btn2", cmd = btn2}
}

function handleMenu()
		while true do
				event = {os.pullEvent()}

				if event[1] == "mouse_click" then
						for k, v in ipairs(buttons) do
								if event[3] >= buttons[k].sX and event[3] <= buttons[k].eX and event[4] >= buttons[k].sY and event[4] <= buttons[k].eY then
										buttons[k].cmd()
								end
						end
		   end
		end
end
nolongerexistant #3
Posted 23 June 2013 - 12:57 PM
That did it, thanks Engineer!