如何让字符串中的每个字符像在游戏中一样依次出现在控制台中? (Lua)
How can I make each character in a string sequentially appear in the console like in a game? (Lua)
我正在尝试在 Lua 中编写一个函数,该函数基本上应该在每次调用该函数时将一段文本作为输入参数后按顺序将每个字符写到控制台。因此,例如,如果我调用该函数并将“Hello World”作为参数,我希望该函数一次一个地按顺序将每个字符打印到控制台(每个字符之间有给定的时间,因此创建一个一种字母的“流动”)。从技术上讲,我一直在研究的功能可以满足我的要求,但是在循环完全迭代字符串中的每个字符之前,它根本不显示 any 文本.因此,如果我想在打印每个字符之间间隔一秒钟向控制台显示“hello”,那么在出现任何文本之前大约需要 5-6 秒(当它出现时,它将是完整的字符串)。我已经尝试以任何我能想到的方式重新组织函数,但我想不出任何其他方式来顺序输出每个字符而无需循环。这只是一个 Lua 问题吗?我见过同样的一般性问题,但对于 C#,看起来它们都以相同的方式工作 (How to make text be "typed out" in console application?)。另外,郑重声明,我正在一个名为“repl.it”的在线 IDE 中编写代码,我并没有放弃这可能是个问题的想法。提前致谢。
--[[ Delay function --]]
function wait(seconds)
local start = os.time()
repeat until os.time() > start + seconds
end
--[[ Function for "saying" things --]]
function say(string)
speakchar = {}
speakindex = 1;
for c in (string):gmatch"." do
table.insert(speakchar, c);
io.write(speakchar[speakindex])
wait(1);
speakindex = speakindex + 1;
end
print("");
end
您需要刷新输出缓冲区。这是简化的代码:
function say(string)
for c in (string):gmatch"." do
io.write(c):flush()
wait(1);
end
io.write("\n")
end
我正在尝试在 Lua 中编写一个函数,该函数基本上应该在每次调用该函数时将一段文本作为输入参数后按顺序将每个字符写到控制台。因此,例如,如果我调用该函数并将“Hello World”作为参数,我希望该函数一次一个地按顺序将每个字符打印到控制台(每个字符之间有给定的时间,因此创建一个一种字母的“流动”)。从技术上讲,我一直在研究的功能可以满足我的要求,但是在循环完全迭代字符串中的每个字符之前,它根本不显示 any 文本.因此,如果我想在打印每个字符之间间隔一秒钟向控制台显示“hello”,那么在出现任何文本之前大约需要 5-6 秒(当它出现时,它将是完整的字符串)。我已经尝试以任何我能想到的方式重新组织函数,但我想不出任何其他方式来顺序输出每个字符而无需循环。这只是一个 Lua 问题吗?我见过同样的一般性问题,但对于 C#,看起来它们都以相同的方式工作 (How to make text be "typed out" in console application?)。另外,郑重声明,我正在一个名为“repl.it”的在线 IDE 中编写代码,我并没有放弃这可能是个问题的想法。提前致谢。
--[[ Delay function --]]
function wait(seconds)
local start = os.time()
repeat until os.time() > start + seconds
end
--[[ Function for "saying" things --]]
function say(string)
speakchar = {}
speakindex = 1;
for c in (string):gmatch"." do
table.insert(speakchar, c);
io.write(speakchar[speakindex])
wait(1);
speakindex = speakindex + 1;
end
print("");
end
您需要刷新输出缓冲区。这是简化的代码:
function say(string)
for c in (string):gmatch"." do
io.write(c):flush()
wait(1);
end
io.write("\n")
end