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

Question regarding read()

Started by Justinjah91, 05 October 2018 - 12:44 PM
Justinjah91 #1
Posted 05 October 2018 - 02:44 PM
Suppose you have a program that requires a user input at the beginning via the read() command. Now, suppose I want to make a wrapper script that will automatically repeat this program. Is there a way to have my wrapper program pass inputs through that read() command?

I should clarify that the best way to approach this is probably just to set up the program so it can take either arguments OR a user input through read(), but I'm just curious if this is possible.

Thanks!
EveryOS #2
Posted 05 October 2018 - 03:02 PM
Do you mean like this:

local args = {...}
local oRead = read
local function read(...)
  if args[1] then
    return unpack(args)
  else
    return oRead(...)
  end
end
Justinjah91 #3
Posted 05 October 2018 - 03:17 PM
Do you mean like this:

local args = {...}
local oRead = read
local function read(...)
  if args[1] then
	return unpack(args)
  else
	return oRead(...)
  end
end

More like this:


write('Start mining? ')
local input = read()

if input == "y" then
  Do stuff
else
  Dont do stuff
end

I want to know if there is a way to set up a wrapper script for this program to make it repeat forever with an input of "y". Again, I know this is trivial using arguments. I'm just wondering if a program can enter a read() input for the user (just out of curiosity, I would never actually do that)
EveryOS #4
Posted 05 October 2018 - 03:44 PM
So like

local read = function()
  return "y"
end

while true do
  write('Start mining? ')
  local input = read()

  if input == "y" then
    Do stuff
  else
    Dont do stuff
  end
end
Luca_S #5
Posted 05 October 2018 - 10:15 PM
Another option would be to queue a char and key event and then call the next program while being careful not to yield. But that only works if the program you are trying to call does not yield(call os.pullEvent, os.pullEventRaw or coroutine.yield) before calling read():


while true do
  os.queueEvent("char", "y")
  os.queueEvent("key", keys.y)
  os.queueEvent("key", keys.enter)
  shell.run("YourProgram")
end
Justinjah91 #6
Posted 06 October 2018 - 04:21 AM
Another option would be to queue a char and key event and then call the next program while being careful not to yield. But that only works if the program you are trying to call does not yield(call os.pullEvent, os.pullEventRaw or coroutine.yield) before calling read():


while true do
  os.queueEvent("char", "y")
  os.queueEvent("key", keys.y)
  os.queueEvent("key", keys.enter)
  shell.run("YourProgram")
end

Ah yes, this is what I was curious about. So you can sort of emulate user inputs with os.queueEvent()… Good to know!