This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Enter text with a while loop running.
Started by tjhod, 19 February 2015 - 12:03 AMPosted 19 February 2015 - 01:03 AM
I am trying to make a program and I can not figure out how to let the user enter text with a while loop running at the same time. It keeps blocking the user from typing anything and I have to end the program with ctrl+t. if I put the read() function into the loop it will only run the loop once and then stop. Anyone know how to fix this?
Posted 19 February 2015 - 01:50 AM
if the while loop requires data from the read function, then put something like "input = read()" before the loop. I could give a more precise answer if i could see some of the code?
Posted 19 February 2015 - 02:28 AM
You should be able to do something like this:
If you can post your current code, that would help us help you.
EDIT: Ninjad by Romeo. We said pretty much the same thing
while true do --#runs forever
input = read()
--#do stuff
end
If you can post your current code, that would help us help you.
EDIT: Ninjad by Romeo. We said pretty much the same thing
Edited on 19 February 2015 - 01:29 AM
Posted 19 February 2015 - 03:30 AM
try:
input = read() – asks for user input
i = 1 – defines the i variable
while i < 25 do – condition
print(input) – things to do
i = i+1 – things to do
end – closes loop
what this code will do is ask for input, then commence the loop; printing the input.
I hope i helped! :P/> :)/>
input = read() – asks for user input
i = 1 – defines the i variable
while i < 25 do – condition
print(input) – things to do
i = i+1 – things to do
end – closes loop
what this code will do is ask for input, then commence the loop; printing the input.
I hope i helped! :P/> :)/>
Posted 19 February 2015 - 03:35 AM
Thanks for the help guys I will try these when I can and let you know if it works!
Posted 19 February 2015 - 03:39 AM
Revealing the code would involved would indeed make it easier to comment on it. I suspect what you're trying to ask about is quite different to what these guys are thinking, and it's the easiest way to be clear.
Posted 19 February 2015 - 04:08 AM
I am guessing he meant to write text at the same time than being able to handle other events.
In that case use the "char" event.
In that case use the "char" event.
local text = ""
while true do
local event = {os.pullEvent()}
if event[1] == "char" then // If a character key is pressed
text = text .. event[2]
elseif event[1] == "key" then // If a non-character key is pressed
if event[2] == keys.backspace then // If backspace is pressed
text = text:(1, #text-1) // Remove the last character
elseif event[2] == keys.enter then // If enter is pressed
doThingWithText(text)
text = ""
end
elseif event[1] == "anything_else" then // Handle other events here
anythingelse()
end
end
Edited on 19 February 2015 - 03:10 AM
Posted 19 February 2015 - 06:09 AM
Revealing the code would involved would indeed make it easier to comment on it. I suspect what you're trying to ask about is quite different to what these guys are thinking, and it's the easiest way to be clear.
Translation:
Post your existing code :)/>