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

About timer that interacts with event

Started by aceyo369, 15 August 2014 - 05:27 PM
aceyo369 #1
Posted 15 August 2014 - 07:27 PM
I wanted to make a time runs while i press up and down to choose in the menu, but somehow, i got <textutils:30: attempt to compare nil with number>: I know the compare nil with number is a common error, but the textutils is the part that makes me confused…

This is the code:
local w,h = term.getSize()

local select = 1
local currentTime = textutils.formatTime(runningTime,false)
local running = true

local function clear()
term.clear()
term.setCursorPos(1,1)
end

local function printCentered(word, h)
term.setCursorPos(w/2 - #word/2, h)
term.write(word)
end

local function runApp()

end

local function editApp()

end

local function seeApp()

end

local function exit()
running = false
end

local menu = {
[1] = {text = "Run a program", handler = runApp},
[2] = {text = "Make or edit a program", handler = editApp},
[3] = {text = "See existing program", handler = seeApp},
[4] = {text = "Exit", handler = exit}
}

local function printMenu()
clear()
printCentered("Time:"..currentTime)
for i = 1, #menu do
if i == select then
printCentered("["..menu.text.."]",h/2 + i)
else
printCentered(menu.text, h/2 + i)
end
end
end

local function keyHandler()
event, key = os.pullEvent()
if key == 200 and select > 1 then
select = select - 1
elseif key == 208 and select < #menu then
select = select + 1
elseif key == 28 then
menu[select].handler()
elseif event == timer then
currentTime = textutils.formatTime(runningTime,false)
end
end

while running do
local timing = os.startTimer(1)
printMenu()
keyHandler()
end
shell.run("startup")
Lyqyd #2
Posted 15 August 2014 - 07:38 PM
Line four passes the undefined variable runningTime to textutils.formatTime. Since that variable is nil there, that is likely the source of the error.
aceyo369 #3
Posted 16 August 2014 - 05:45 AM
I wanted the computer to multitask having to be able to use your key while the time runs… I used the os.startTimer() but it doesn't seems to be working as the time only shows the one in the printMenu() but does not process the one in the keyHandler()…

This is the code:

local w,h = term.getSize()

local select = 1
local running = true

local function clear()
term.clear()
term.setCursorPos(1,1)
end

local function printCentered(word, h)
term.setCursorPos(w/2 - #word/2, h)
term.write(word)
end

local function runApp()

end

local function editApp()

end

local function seeApp()

end

local function exit()
running = false
end

local menu = {
[1] = {text = "Run a program", handler = runApp},
[2] = {text = "Make or edit a program", handler = editApp},
[3] = {text = "See existing program", handler = seeApp},
[4] = {text = "Exit", handler = exit}
}

local currentTime = 0

local function printMenu()
clear()
printCentered("Time:"..currentTime,1)
for i = 1, #menu do
if i == select then
printCentered("["..menu.text.."]",h/2 + i)
else
printCentered(menu.text, h/2 + i)
end
end
end

local function keyHandler()
sT = os.startTimer(0)
event, key = os.pullEvent()
if key == 200 and select > 1 then
select = select - 1
elseif key == 208 and select < #menu then
select = select + 1
elseif key == 28 then
menu[select].handler()
elseif event == timer and key == sT then
currentTime = textutils.formatTi(os.time(),false)
term.setCursorPos(1,1)
term.clearLine()
printCentered("Time: "..currentTime,1)
end
end

while running do
printMenu()
keyHandler()
end
shell.run("startup")
natedogith1 #4
Posted 16 August 2014 - 05:56 AM
event == timer should be event == "timer" you should probably also make sure that when you're checking what key was pressed that it's actually a "key" event
aceyo369 #5
Posted 16 August 2014 - 06:16 AM
Now that i check the key event, even the key cannot be used now…
Lyqyd #6
Posted 16 August 2014 - 06:37 AM
Please paste the new code along with any new error messages whenever you change it. We can't guess what changes you've made, you have to show us.
aceyo369 #7
Posted 16 August 2014 - 06:45 AM
There is no error to the program, but what i want is where the Time constantly updating itself while i am able to used my keys, The function that goes wrong is:

local function keyHandler()
sT = os.startTimer(0)
event, key = os.pullEvent()
if key == 200 and select > 1 then
select = select - 1
elseif key == 208 and select < #menu then
select = select + 1
elseif key == 28 then
menu[select].handler()
elseif event == timer and key == sT then
currentTime = textutils.formatTi(os.time(),false)
term.setCursorPos(1,1)
term.clearLine()
printCentered("Time: "..currentTime,1)
end
end
flaghacker #8
Posted 16 August 2014 - 07:55 AM
EDIT: Double post

A bug with the back button on a browser + 60 seconds post timeout error.
Edited on 16 August 2014 - 05:56 AM
aceyo369 #9
Posted 16 August 2014 - 08:31 AM
Ok, im sticking to this thread, just want to know how can i allow the computer to multitask?
theoriginalbit #10
Posted 16 August 2014 - 09:27 AM
Merged threads
Bomb Bloke #11
Posted 16 August 2014 - 12:03 PM
I'd use something along these lines:

local function keyHandler()
	local sT = os.startTimer(0)
	local lastSelected = 0
	
	while true do
		if lastSelected ~= select then
			printMenu()
			lastSelected = select
		end
		
		local event, par = os.pullEvent()

		if event == "key" then
			if par == 200 and select > 1 then
				select = select - 1
			elseif par == 208 and select < #menu then
				select = select + 1
			elseif par == 28 then
				menu[select].handler()
			end
		elseif event == "timer" and par == sT then
			term.setCursorPos(1,1)
			term.clearLine()
			printCentered("Time: "..textutils.formatTime(os.time(),false),1)
			
			sT = os.startTimer(1)
		end
	end
end
Edited on 16 August 2014 - 02:48 PM
Dragon53535 #12
Posted 16 August 2014 - 03:52 PM
I'd use something along these lines:


		elseif event == "timer" and par == sT then
			currentTime = textutils.formatTime(os.time(),false)
			term.setCursorPos(1,1)
			term.clearLine()
			printCentered("Time: "..textutils.formatTime(os.time(),false),1)
			
			sT = os.startTimer(1)
		end
	end
end
Quick question, what's currentTime used for?
Bomb Bloke #13
Posted 16 August 2014 - 04:48 PM
Nothing, it was there (in a redundant fashion) in aceyo369's code, and I apparently failed to delete that line when I edited it.

*snip*
aceyo369 #14
Posted 16 August 2014 - 06:46 PM
Sorry, a wrong post here… Editing new one

Thank you alot bomb bloke, you help me solve the multitasking with timer… just that it won't run the handler, but i solved that too…
Edited on 16 August 2014 - 04:45 PM