简体   繁体   English

如何在Lua中的函数内调用函数?

[英]How to call a function within a function in Lua?

I have this code:我有这个代码:

function test()
    function awesome()
        print("im awesome!")
    end

    function notawesome()
        print("im not awesome.")
    end

    function notevenawesome()
        print("im not even awesome...")
    end
end

test().notawesome()

When I run this, the console prints当我运行它时,控制台打印

15: attempt to index a nil value 15:尝试索引一个 nil 值

What I'm trying to do is call the function notawesome() within the function test() , how would I do that?我想要做的是在函数test() 中调用函数notawesome () ,我该怎么做?

Your function is not returning anything (thus returning nil).您的函数没有返回任何内容(因此返回 nil)。 Something like this should work:这样的事情应该工作:

function test()
    function awesome()
        print("im awesome!")
    end

    function notawesome()
        print("im not awesome.")
    end

    function notevenawesome()
        print("im not even awesome...")
    end
    result = {}
    result["notawesome"] = notawesome
    result["awesome"] = awesome
    result["notevenawesome"] = notevenawesome
    return result
end
test().notawesome()

@Axnyff explains what you might be trying to do. @Axnyff解释了您可能想要做什么。 I'll explain what you did.我会解释你做了什么。

If you are familiar with other languages, please note that Lua does not have function declarations ;如果你熟悉其他语言,请注意 Lua 没有函数声明 It has function definitions , which are expressions that produce a function value when evaluated.它有函数定义,它们是在求值时产生函数值的表达式。 Function definition statements, which you have used, are just shortcuts that implicitly include an assignment.您使用过的函数定义语句只是隐式包含赋值的快捷方式。 See the manual.请参阅手册。

When you run your code, a function definition is evaluated and the resulting function value is assigned to the variable test .当您运行代码时,会评估一个函数定义并将结果函数值分配给变量test Then the variable test is evaluated and its value is called as a function (and it is one).然后变量test被评估,它的值被称为一个函数(它是一个)。

When that function executes, three function definitions are evaluated and assigned to the variables awesome , notawesome , and notevenawesome , repsectively.当该功能执行,三个功能定义进行评估,并赋值给变量awesomenotawesomenotevenawesome ,repsectively。 It doesn't return anything.它不返回任何东西。

So, when the result of calling test ( nil ) is indexed by the string "awesome", you get the error.因此,当调用test ( nil ) 的结果由字符串 "awesome" 索引时,您会收到错误消息。

If you wanted to call the function value referred by the variable awesome , just do awesome() .如果你想调用由变量awesome引用的函数值,只需执行awesome()

If you want to achieve that instead to use the main function you can use an object:如果你想实现它而不是使用 main 函数,你可以使用一个对象:

test = {
    awesome = (function()
        return 'im awesome!'
    end),
    notawesome = (function()
        return 'im not awesome.'
    end),
    notevenawesome = (function()
        return 'im not even awesome...'
    end)
}

To call your functions use this:要调用您的函数,请使用以下命令:

print(test.notawesome())    --> im not awesome.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM