1.loadfile--only compile, do not run
LoadFile Name Incredibles, it only loads the file, compiles the code, does not run the code in the file.
For example, we have a Hellofile.lua file:
The code is as follows:
Print ("Hello");
function hehe ()
Print ("Hello");
End
There is a code in this file, and a function. Try LoadFile load this file with the following code:
The code is as follows:
LoadFile ("Hellofile.lua");
Print ("End");
The output results are as follows:
The code is as follows:
[Lua-print] End
If LoadFile executes the code in the file, the hello string should be output.
The result is that it does not execute code.
2.dofile--execution
Obviously, Dofile is the guy who executes the code, the following code:
The code is as follows:
Dofile ("E:/android/wordspace_cocosiderc0/cocosluatest/src/hellofile.lua");
Print ("End");
The output results are as follows:
The code is as follows:
[Lua-print] Hello
[Lua-print] End
It's a little awkward here, the file path I used the absolute path, because Dofile uses the relative path in the Coco Code IDE to find the file (even if Addsearchpath is used)
But it doesn't matter, it doesn't affect the content of this article.
3.require--I only do it once.
Require and dofile a bit like, but very different, require the first time the file is loaded, the code will be executed.
However, after the second time, the file is loaded again and will not be repeated. In other words, it saves the files that have already been loaded and does not load repeatedly.
The test code is as follows:
The code is as follows:
For i = 1, 2, 1 do
Require ("Hellofile.lua");
End
Print ("End");
In order to illustrate this situation, I deliberately called two times require, the output results are as follows:
The code is as follows:
[Lua-print] Hello
[Lua-print] End
As we said, it was called two times, but the code was executed only once.
If this is replaced with dofile, it will output two times the Hello string.
Dofile, loadfile, require differences in LUA