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

prompt api not working

Started by KingofGamesYami, 03 April 2014 - 01:44 AM
KingofGamesYami #1
Posted 03 April 2014 - 03:44 AM
So, I wrote a little code for a prompt in my api, but it seems to be glitching after I tried to add another feature.
Before
function prompt(text)
term.write(text)
return(read)
end

After

function prompt(text, valid)
term.write(text..": ")
input = read()
if valid == nil then
  return(input)
else
  if input == valid then
   return(input)
  else
   return prompt(text, valid)
  end
end
end
I now want to know if I can add multiple options, not limited to an amount though. eg prompt("Color: ", "red" or "blue" or "green")
As you can see, I was trying to add an option to validate the answer (so you could have, say, "yes" or "no" as the only answers.
The error I get when I try to call this api is
bios:395: Expected string
The code I was calling it with:
os.loadAPI(testapi)
var = testapi.prompt("Test", "Awesome")
print(var)
Obviously I was just seeing if it worked, as this code has no practical use by itself.
Edited on 03 April 2014 - 02:19 AM
1lann #2
Posted 03 April 2014 - 04:00 AM
You're currently returning "read" as a function in your prompt function, you need to call it with "read()" to get the value of the read input. So it should be "return read()"
theoriginalbit #3
Posted 03 April 2014 - 04:01 AM
os.loadAPI requires a string for the path you wish to load. so add quotes around testapi in the loadAPI call.

also it should be noted that return is not a function, you do not need to add brackets there.

You're currently returning "read" as a function in your prompt function, you need to call it with "read()" to get the value of the read input. So it should be "return read()"
that was in the 'before' not the 'after'
Edited on 03 April 2014 - 02:02 AM
KingofGamesYami #4
Posted 03 April 2014 - 04:10 AM
You're currently returning "read" as a function in your prompt function, you need to call it with "read()" to get the value of the read input. So it should be "return read()"
Uh, sorry about that… I typed this up kinda fast. Must have missed it.
os.loadAPI requires a string for the path you wish to load. so add quotes around testapi in the loadAPI call.

also it should be noted that return is not a function, you do not need to add brackets there.

You're currently returning "read" as a function in your prompt function, you need to call it with "read()" to get the value of the read input. So it should be "return read()"
that was in the 'before' not the 'after'
Thanks. I feel very stupid now :blink:/> I need to start coding when I am more than half awake apparently. *facepalm*
edit: I have used os.loadAPI before so I really should have spotted that earlier.

EDIT #2: I now have only one problem, I cannot set multiple acceptable answers for some reason (eg. prompt("test", "hello" or "goodby"))
Edited on 03 April 2014 - 02:17 AM
CometWolf #5
Posted 03 April 2014 - 06:46 PM
EDIT #2: I now have only one problem, I cannot set multiple acceptable answers for some reason (eg. prompt("test", "hello" or "goodby"))
Obviously, you've only specified one valid variable. You'll either have to setup multiple variables or use "…"

local function derp(...)
  local tArgs = {...}
  local text = table.remove(tArgs,1) -- text is the first argument, so we remove that and store it
  local tValid = {}
  for i=1,#tArgs --store all the valids in the table in a way that makes it easy to check
	tValid[tArgs[i]] = true
  end
  if tValid[text] then
	-- text matched
  else
	--text did not match
  end
end
Edited on 03 April 2014 - 04:47 PM
theoriginalbit #6
Posted 04 April 2014 - 12:53 AM
an explanation as to why your previous way didn't work. its due to how the or operators work.
both variables do not get passed to the function, instead they're evaluated there. so what happens is Lua checks if "hello" is nil, which obviously it isn't (its a string, not nil) so it doesn't bother checking the rest of the conditional (as its not required) and passes "hello" to the function. if somehow "hello" were to be nil however (which is impossible, but lets pretend for this example) it would evaluate it and move on, meaning that instead of passing "hello" to the function it will pass "goodbye"

okay so a slight improvement (and bugfix) on what CometWolf posted

local function prompt(text, ...) --# we can just get the first argument and the rest as a vararg
  local tArgs = {...}
  local tValid = {}
  for i = 1, #tArgs do
    tValid[tArgs[i]] = true
  end
  local input = read()
  if tValid[input] then
    --# input is valid
  else
    --# input is invalid
  end
end
notice how vararg functions can declare variables in front of the '…'; do note however they cannot declare variables after.