How do I run another script from inside Lua?

前端 未结 1 683
离开以前
离开以前 2021-02-19 00:31

I need to execute a Lua script from inside another Lua script. How many ways are there, and how do I use them?

1条回答
  •  执笔经年
    2021-02-19 00:49

    Usually you would use the following:

    dofile("filename.lua")
    

    But you can do this through require() nicely. Example:

    foo.lua:

    io.write("Hello,")
    require("bar")
    

    bar.lua:

    io.write(" ")
    require("baz")
    

    baz.lua:

    io.write("World")
    require("qux")
    

    qux.lua:

    print("!")
    

    This produces the output:

    Hello, World! 
    

    Notice that you do not use the .lua extension when using require(), but you DO need it for dofile(). More information here if needed.

    0 讨论(0)
提交回复
热议问题