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

Arguments with input?

Started by Scoptile, 29 July 2015 - 01:34 AM
Scoptile #1
Posted 29 July 2015 - 03:34 AM
I'm making a text based game where you input commands to control your player. i want certain commands to require arguments, but i cant figure out how to do that. i dont want to have hundreds of commands that all will use a different item, i want one command that will do something different based on the argument you give it (ex. "use (item)"). Thanks in advance :)/>
KingofGamesYami #2
Posted 29 July 2015 - 03:57 AM
So… something like this? It'll return two things: a command (eg "use") and a table of arguments (eg {"item"}).

local function prompt()
  local args = {}
  for arg in read():gmatch( "%S+" ) do
    args[ #args + 1 ] = arg
  end
  return table.remove( args, 1 ), args
end

Patterns are your friend… You should learn them!
Scoptile #3
Posted 29 July 2015 - 04:35 AM
It confused me a little at first, but i figured out how it works. thanks for the help :)/>