Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua 函数和while语句创建无限打印循环_Lua_Infinite Loop - Fatal编程技术网

Lua 函数和while语句创建无限打印循环

Lua 函数和while语句创建无限打印循环,lua,infinite-loop,Lua,Infinite Loop,首先,我知道这是一堆杂乱无章的Lua,但我还在学习,所以请原谅我 我的问题是: 我在iPad上的TouchLua+上运行我的代码,它运行良好且平稳,直到我开始使用tinput=io.read(),无论我键入“攻击”还是“逃跑”,我最终都会在屏幕上无限地打印“这不是有效的命令” 我认为while语句中的get_input()与我在整个脚本中将io.read()定义为tinput而不是标准输入之间存在冲突 我可以接受这一点,除了我正在学习的脚本(目前的youtube视频)没有做到这一点,我比较了两者

首先,我知道这是一堆杂乱无章的Lua,但我还在学习,所以请原谅我

我的问题是: 我在iPad上的TouchLua+上运行我的代码,它运行良好且平稳,直到我开始使用tinput=io.read(),无论我键入“攻击”还是“逃跑”,我最终都会在屏幕上无限地打印“这不是有效的命令”

我认为while语句中的get_input()与我在整个脚本中将io.read()定义为tinput而不是标准输入之间存在冲突

我可以接受这一点,除了我正在学习的脚本(目前的youtube视频)没有做到这一点,我比较了两者和函数,而“if tinput”和它们写的完全一样

我试图在谷歌上找到答案,在视频上找到评论,在github上找到原始脚本,但我还不了解github,谷歌也没有帮助

我做错了什么,错过了什么,或者总体上搞砸了什么

另外,请原谅我的大量使用评论,对不起

    print("Welcome to the game")

    -- expilictly set the variable to an empty string
    -- because it is used in a 'while' loop
    input = ""
    inv = {"sword", "coin", "armour"}

    -- inventory function
    function get_inv()
        for i, v in pairs(inv) do
            print(i .. " " .. v)
        end
    end

    -- invalid command function
    function inv_com()
        print("You did not type a valid command...")
    end

    -- function to simplify 'while' statement throughout
    -- entire programme
    function get_input()
        print("What do you want to do?")
        i = io.read() -- get what the user types
        return i
    end

    -- fuction adding item to inventory
    function push_inv(item)
        table.insert(inv, item)
    end

    -- function removing item from inventory
    function pop_inv(item)
        for i, v in pairs(inv) do
            if v == item then
                table.remove(inv, i)
            end
        end
    end

    -- '~=' means not equal to
    while input ~= "leave cave" do
        input = get_input() -- get what the user types

        -- '==' means equal to
        if input == "inspect" then
            print("You are in a cave")
        elseif input == "leave cave" then
            print("You leave cave")
        elseif input == "inv" then
            get_inv()
        else
            inv_com()
        end
    end

    input = ""
    while input ~= "follow path" do
        input = get_input()

if input == "inspect" then
    print("You are at the base of a hill. There is a path")
elseif input == "follow path" then
    print("You follow the path")
    print("A troll appears wielding an axe")
    print("What do you want to do")
    tinput = io.read()
    if tinput == "attack" then
        print("You smack it, and it falls dead")
    elseif tinput == "run away" then
        print("You cowardly run away and it smack you, and steals your coin")
        pop_inv("coin")
    else
        print("You stand there, it stabs you, you die")
        os.exit()
    end
elseif input == "inv" then
    get_inv()
else
    inv_com()
end
    end


    input = ""

    -- a boolean value can only have 1 of 2 values
    -- 'true' or 'false', this works great for the have_key
    -- value because there are only 2 possibilities:
    -- you either have the key or you don't.
    have_key = false

    -- this stement mean: while this statement:
    -- 'input == "open gate"
    -- and have_key' is true
    while not (have_key == true and input == "open gate") do
input = get_input


if input == "inspect" then
    --- if we dont have the key, tell us
    if have_key == false then
        print("there is a path behind you, a gate infront of you and key is hidden in the grass")
        -- if we have the key, dont tell us
    elseif have_key == true then
        print("There is a path behind you, and a gate infront of you")
    else
        print("you did not put a valid command")
    end

    -- we now have the key, set variable to true
elseif input == "grab key" then
    have_key = true
    print("You grabbed the key")
    table.insert(inv, "gate key")
elseif input == "inv" then
    get_inv()
elseif input == "open gate" then
    if have_key == true then
        print("You've escaped")
    elseif have_key == false then
        print("The gate is locked")
    end
elseif input == "pick up magic" then
    push_inv("MAGIC")
elseif input == "inv" then
    get_inv()
else
    inv_com()
end
    end


    print("You won the game. Please leave!")

其中一个while循环包含代码

input = get_input
这应该是

input = get_input()
所发生的事情是,您将变量input设置为函数get_input,而不是调用它的结果,因此input永远不会是有效字符串,它将始终调用inv_com


编辑:我知道这超出了问题的范围,但我有一些提示,可以帮助你解决这个特定问题之外的程序

  • 命名函数时要小心,不要产生歧义。您有用于库存管理的pop_inv和push_inv等功能,但同时有用于无效命令的inv_com
  • 读这本书。您的代码有一些非常疯狂的缩进,这使得其他人很难理解发生了什么
  • 尝试并思考如何将所有这些代码转换为一个可能处理所有可能性的主循环,而不是一个接一个地使用一堆循环和if语句。如果以后想添加更多功能,请考虑一下程序的结构将变得多么复杂

  • 你所做的让我想起了很多我是如何开始编程,进行文本冒险的。如果你对文本冒险感兴趣,看看Zork,看看他们的游戏循环是如何工作的。另外,对状态机进行一些研究,因为这似乎对您现在正在构建的东西很有用。快乐编码:)

    如果我的答案有帮助,请您将其标记为已接受好吗?如果它仍然在一个无限循环中,我很乐意再看一眼并相应地编辑我的答案。