Should not have discussed these functions today, however, in order to gather the word number. No, in order to facilitate the future of the article, or to talk about the basic functions of the ~
1.loadfile--only compiles, does not run
LoadFile name incredible, it will only load files, compile code, will not run the code in the file.
For example, we have a Hellofile.lua file:
Copy Code code as follows:
Print ("Hello");
function hehe ()
Print ("Hello");
End
This file has a code, and a function. Try LoadFile load this file with the following code:
Copy Code code as follows:
LoadFile ("Hellofile.lua");
Print ("End");
The output results are as follows:
Copy Code code as follows:
If LoadFile will execute the code in the file, it should output the Hello string.
The results show that it does not execute code.
2.dofile--execution
Obviously, Dofile is the guy who executes the code, the following code:
Copy Code code as follows:
Dofile ("E:/android/wordspace_cocosiderc0/cocosluatest/src/hellofile.lua");
Print ("End");
The output results are as follows:
Copy Code code as follows:
[Lua-print] Hello
[Lua-print] End
Here's a little awkward, file path I used the absolute path, because dofile in the Coco Code IDE using the relative path will not find the file (even if the use of Addsearchpath)
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 in the first time to load the file, will execute the code inside.
However, the second time after the file is loaded again, it will not be repeated. In other words, it saves files that have already been loaded and does not load repeatedly.
The test code is as follows:
Copy Code code 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 of the results are as follows:
Copy Code code as follows:
[Lua-print] Hello
[Lua-print] End
As we said, the call was made two times, but the code was executed only once.
If this is replaced with Dofile, the Hello string will be output two times.
4. End
I found that it is a bit slow to finish the article while reading. It's a question of fish and paws.