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:
Copy CodeThe 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:
Copy CodeThe code is as follows:
LoadFile ("Hellofile.lua");
Print ("End");
The output results are as follows:
Copy CodeThe 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:
Copy CodeThe code is as follows:
Dofile ("E:/android/wordspace_cocosiderc0/cocosluatest/src/hellofile.lua");
Print ("End");
The output results are as follows:
Copy CodeThe 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:
Copy CodeThe 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:
Copy CodeThe 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.
Require
In Lua, the Require function loads the file as a chunk and executes as a dofile. But there are two benefits: 1. Loading files by mode 2. The same file is not loaded repeatedly.
The Require parameter is a full file name (directory name + file name, which may be a bit similar to the package in Java), i.e. Package.path, the typical Package.path value is as follows (where D:\Bin is the directory where Lua.exe resides):
.\?. Lua;d:\bin\lua\?. Lua;d:\bin\lua\?\init.lua;d:\bin\?. Lua;d:\bin\?\init.lua
The directories that are usually useful to us are. \?. Lua
So, if we want to execute the Test.lua file of the current directory, we just need to require ("test"), but if we want to execute a file in another directory, such as "D:\lua\a.lua", We need to add this path to the Package.path, for example we can write:
Package.path=package.path. ";D: \?. Lua
This allows us to use the Require function, such as require ("a").
Note: Require will only be loaded once.
Copy CodeThe code is as follows:
For callcount = 0, 2 does
Require ("test");
End
LoadFile, Dofile, require in LUA