Lua - Kommandozeilen-Eingabe vom Benutzer erhalten?
-
06-07-2019 - |
Frage
In meinem lua Programm, ich mag Benutzer zur Bestätigung stoppen und fragen, bevor sie mit einer Operation fortfahren. Ich bin mir nicht sicher, wie zu stoppen und auf Benutzereingaben warten, wie es getan werden kann?
Lösung
Werfen Sie einen Blick auf die io
Bibliothek, die standardmäßig Standard-Eingang als Standard-Eingabedatei hat:
Andere Tipps
local answer
repeat
io.write("continue with this operation (y/n)? ")
io.flush()
answer=io.read()
until answer=="y" or answer=="n"
Ich habe mit Code wie folgt gearbeitet. Ich werde dies in einer Art und Weise geben, es wird funktionieren:
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
ich benutze:
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
versuchen folgenden Code zu verwenden,
m=io.read()
if m=="yes" then
(insert functions here)
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
Aus dem bisschen lua, die ich getan habe (nicht viel), werde ich sagen, dass Groß- und Kleinbuchstaben mit redundant ist, wenn Sie string.sub verwenden.
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
Das sollte funktionieren.