Posted 11 November 2012 - 06:18 AM
I'm trying to make a rednet messaging program that can receive messages at all times; can someone show me how to write a function that operates in the background?
-- Just a little helper function.
local function clear()
term.setCursorPos(1,1)
term.clear()
end
local function inputFunc()
while true do
term.clear()
term.setCursorPos(1, 19)
write( "Type "exit" or press END to exit." )
term.setCursorPos(10, 7)
write( "Input: " )
local input = read()
if string.lower( input ) == "exit" then
clear()
return -- and thus yielding this coroutine.
end
end
end
local function eventListener()
while true do
local sEvent, sParam = os.pullEvent()
if sEvent == "key" then
if sParam == keys.space then
local oldX, oldY = term.getCursorPos()
term.setCursorPos(1, 1)
write( "Marking spaces..." )
term.setCursorPos(oldX, oldY + 1)
write("^")
elseif sParam == keys["end"] then
clear()
return -- and thus yielding this coroutine.
end
end
if sEvent == "char" then
-- do something
end
if sEvent == "redstone" then
-- do something
end
end
end
-- Start coroutines.
parallel.waitForAny( inputFunc, eventListener )
Yes you can run functions within functions. And yes, you can also let the function recursively call itself without yielding it.Is the "–Start coroutines" bit just calling the two functions? Can I run a function inside the function or call the function again without yielding the parent function?
Just checking.