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

how to read() a printed returned value

Started by deathvondoom, 21 July 2013 - 05:08 AM
deathvondoom #1
Posted 21 July 2013 - 07:08 AM
hello. i have been trying to figure out how to read a printed returned value from a menu table.
every time i run the program it prints the number of the option you had selected then stops
once stopped if you retype the number it printed and hit enter then it does what its ment to do.
how do i make it so yuo dont have to retype the already printed number?

here is the code

function CUI(m)
n=1
l=#m
while true do
term.clear()
term.setCursorPos(1,2)
for i=1, l, 1 do
if i==n then print(i, " ["..m[i].."]") else print(i, " ", m[i]) end
end
print("Select a number[arrow up/arrow down]")
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<=l then n=n+1 end
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
local options={
"The Dunes",
"The trench",
"East Woods"
}
local n = CUI(options)
print(n)


track = read()  - i believe this is the problem but dont know how to fix it as it needs to read the printed (n)

if track == "1" then
redstone.setOutput ("right", true)
sleep(1)
redstone.setOutput ("right",  false)
print ("The dunes have been selected")
sleep(3)
print ("you may now launch when ready")
sleep(5)
os.reboot()
elseif track == "2" then
redstone.setOutput ("back", true)
sleep(1)
redstone.setOutput ("back", false)
print ("The Trench has been selected")
sleep(3)
print ("you may now launch when ready")
sleep(5)
os.reboot()
elseif track == "3" then
redstone.setOutput ("left", true)
sleep(1)
redstone.setOutput  ("left", false)
print ("East Woods have been selected")
sleep(3)
print ("you may now launch when ready")
sleep(5)
os.reboot()
end
Lyqyd #2
Posted 21 July 2013 - 04:12 PM
Split into new topic.

Get rid of the print and the read lines, and change the comparisons to compare n to 1, 2, or 3 (instead of "1", "2", or "3" like you have currently).
deathvondoom #3
Posted 21 July 2013 - 04:24 PM
Split into new topic.

Get rid of the print and the read lines, and change the comparisons to compare n to 1, 2, or 3 (instead of "1", "2", or "3" like you have currently).

Thank you very much….i hate when something simple ends up being the wrench.
RoD #4
Posted 24 July 2013 - 05:04 PM
i see you have a repeated line of code XDDD i personaly like to keep my code more compacted as possible(sometimes i forgot to do it :P/>) so i was thinking instead of:

sleep(3)
print ("you may now launch when ready")
sleep(5)
os.reboot()
And repeating this trough all the code, you could just make a simple function like:

function rebootselection()
sleep(3)
print ("you may now launch when ready")
sleep(5)
os.reboot()
end
And use it on the code to keep it more simple and organized:

rebootselection()
:D/>