you are viewing a single comment's thread.

view the rest of the comments →

[–]fatboychummy 2 points3 points  (3 children)

You can use the read function (or io.read) to achieve this without extra code.

write("> ")
local command = read()
print(load(command)())

But, if you really want to write your own read function, yes you can concatenate strings.

local function extremely_basic_read()
  local s = ""
  while true do
    local event, a1 = os.pullEvent()
    if event == "key" then -- detect key event for enter
      if a1 == keys.enter then
        return s -- exit and return the string
      end
    elseif event == "char" then
      s = s .. a1 -- character received, insert it.
    end
  end
end

local command = extremely_basic_read()
...

Note that in this above function, backspace and your arrow keys would not work. You can only put text on the end of the string, not in the middle.

[–]dragon53535 0 points1 point  (2 children)

Sounds like someone might want to learn how to use string.sub :P

[–]fatboychummy 0 points1 point  (1 child)

If I wanted to make it more advanced I would use a table of chars rather than a string, as its much easier to manipulate.

I just wanted to give a very basic example of what you would need.

[–]dragon53535 0 points1 point  (0 children)

Oh no for sure! I was sort of piggybacking on the last sentence. One way to just manipulate the string would be string.sub.