I need to execute a Lua script from inside another Lua script. How many ways are there, and how do I use them?
With the Entity Inspector view pane visible, select the entity in the viewport. Click Add Component, and then open Scripting, Lua Script. Scroll down to the Scripting section, and then click Lua Script. A Lua Script component appears in the inspector.
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! <newline>
Notice that you do not use the .lua
extension when using require()
, but you DO need it for dofile()
.
More information here if needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With