Pregunta

En mi programa lua, quiero detenerme y pedirle confirmación al usuario antes de continuar con una operación. No estoy seguro de cómo detener y esperar la entrada del usuario, ¿cómo se puede hacer?

¿Fue útil?

Solución

Eche un vistazo a la biblioteca io , que por defecto tiene entrada estándar como el archivo de entrada predeterminado:

http://www.lua.org/pil/21.1.html

Otros consejos

local answer
repeat
   io.write("continue with this operation (y/n)? ")
   io.flush()
   answer=io.read()
until answer=="y" or answer=="n"

He trabajado con código como este. Escribiré esto de una manera que funcione:

io.write("continue with this operation (y/n)?")
answer=io.read()
if answer=="y" then
   --(put what you want it to do if you say y here)
elseif answer=="n" then
   --(put what you want to happen if you say n)
end

Yo uso:

     print("Continue (y/n)?")
re = io.read()
if re == "y" or "Y" then
    (Insert stuff here)
elseif re == "n" or "N" then
    print("Ok...")
end

intenta usar el siguiente código

m = io.read () if m == " yes " el (insertar funciones aquí) end

print("Continue (y/n)?")
re = io.read()
if re == "y" or "Y" then
    (Insert stuff here)
elseif re == "n" or "N" then
    print("Ok...")
end

Por lo poco que lua que he hecho (no mucho), voy a decir que usar letras mayúsculas y minúsculas es redundante si usas string.sub.

print("Continue? (y/n)")
local re = io.read()

--[[Can you get string.sub from a local var? 
If so, this works. I'm unfamiliar with io(game 
lua uses GUI elements and keypresses in place of the CLI.]]

if re.sub == "y" then
    --do stuff
if re.sub == "n" then
    --do other stuff
end

Eso debería funcionar.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top