Posted 08 January 2013 - 11:34 AM
Hi guys. I'm creating a program that needs to react to either an input from a bundled cable, or a number being typed into the terminal. But it needs to do both at the same time.
Here is an excerpt from my current code:
The code works great, except if someone has typed in a string and then walked away without hitting enter. Then the "redstone" event won't activate because the computer is still doing read().
I tried to get it to detect the pressing of the escape key - the player quits looking at the terminal -and terminating the read() function, but I couldn't find a way of doing this while the read() command is still active.
What's the best way to solve this issue? Can the computer detect a second event if the first event's code hasn't finished executing?
Here is an excerpt from my current code:
local y = nil
local z = 0
while z==0 do
term.clear()
term.setCursorPos(1, 1)
term.write("Type a number: ")
local event, param = os.pullEvent()
if event == "key" then
if param ~= 1 and param ~= 28 then
y = tonumber(read())
end
if y~=nil then z = 1
end
elseif event == "redstone" and rs.getBundledInput("back") and rs.getBundledInput("back")~=0 then
y = rs.getBundledInput("back") term.setCursorPos(1, 2) z = 1
y = 1+(math.log(y)/math.log(2))
end
end
print(y)
The code works great, except if someone has typed in a string and then walked away without hitting enter. Then the "redstone" event won't activate because the computer is still doing read().
I tried to get it to detect the pressing of the escape key - the player quits looking at the terminal -and terminating the read() function, but I couldn't find a way of doing this while the read() command is still active.
What's the best way to solve this issue? Can the computer detect a second event if the first event's code hasn't finished executing?