Lua: preliminary understanding and use of require

Foreword:

  1. Which paths require is looking for when loading and how to find it in the folder will not be discussed here, because I don’t understand it very well. If you want to know how to load the underlying layer, you can check out other blogs, there are many.
  2. The purpose of this article is to let you understand the use of require in Lua as much as possible, as well as some points that should be paid attention to.

understand:

  1. You can think of require as using in C#, except that require in Lua is global and can be used in other scripts after being referenced in one script.
  2. A required script is essentially "executed" and all global members in the script are loaded into _G Including tables, variables, methods, execution statements outside the method will also be executed, and the method will not be executed until it is called. (Students who don’t understand _G can read the blog about this thing)
  3. In order to avoid repeated loading, all required scripts will be "marked" inpackage.loaded. When loading, they will be Go to package.loaded to find whether it has been referenced. If so, it will not be loaded again. (I wrote this paragraph to mark it, because I am not sure whether the value stored in package.loaded is the value of this script, although other blogs say it is the value saved in this , but what I output is a Boolean value. Just understand the meaning of this sentence, that is, it will not be recorded repeatedly after being loaded; my understanding: all required scripts will be processed in package.loaded Whether the tag has been loaded. If it exists, it will not be loaded. You can use it by calling it directly because they are all stored in _G. I don’t know if my understanding is correct. If you have different opinions or good ideas, I welcome them. Discussion.)

(Digression: Some companies’ Lua scripts will put the basic require in one script and call it uniformly.)

I created two scripts, one executes require and one is required

--脚本名module.lua  这个是被require的脚本

module = {
    
    };
module.testFunc=function()
	print("我是module表中的testFunc方法,我执行了");
end
--上面的module表是为了模拟类,所以和文件名保持一致,下面声明的a表和变量b都是可以用的
--但是在实际开发中,通常使用上面module表的规范,模拟类的引用。

print("单独的语句在被require时就会被执行");

function aloneTestFunc()
	print("方法被调用时才会执行");
end

a={
    
    };
a.name="asd";
b="a";
local c="123";

Test one:

  1. Purpose: Provide how to use require, test the output of variables and method calls, and whether individual statements will be executed.
  2. Method: require the module script above in the script, and then call the method of the module table to output the variables of the a table, the individual variable b, and the local variable c.
require "module";		--require的格式(可能还有其他格式),后面跟脚本的名称。

module.testFunc();		--调用module表的这个方法

print(a.name);			--输出a表的变量

print(b);				--输出变量b

print(c);				--输出局部变量c

operation result:

>lua -e "io.stdout:setvbuf 'no'" "TestRequireChange.lua" 
单独的语句在被require时就会被执行
我是module表中的testFunc方法,我执行了
asd
a
nil
>Exit code: 0

in conclusion:

  • When the script is required, all members in the script will be traversed, and executable statements outside the method will be executed directly.
  • Global variables and members in the table in the required script can be called, but local variables in the required script cannot be accessed.

Test 2:

  • Purpose: To test the module module, whether it can be used directly without rerequiring it in other scripts.
  • Method: First create a new script, write a method in it that calls the variables in the module module, and then call this method in another script to test whether it can be used directly.

This is a new script

--脚本名requireTest.lua

function change() 
print(a.name);     --直接使用module脚本中的变量a 测试是否可以直接使用
end

Reference the module script and requireTest script in this script, and call the change method in the requireTest script.

require "module";
require "requireTest"

change();

operation result:

>lua -e "io.stdout:setvbuf 'no'" "TestRequireChange.lua" 
单独的语句在被require时就会被执行
asd
>Exit code: 0

Summarize:

  • As long as it is a required script, it can be called in other scripts in the project.
  • Tip: The string above the output string "asd" is executed when require "module". If you forget, you can look up the code in the module script.

This is enough to see the use of require here. Deeper and more complex uses will be updated in other articles. In this article, we first have a preliminary understanding of require and how to use it.

Guess you like

Origin blog.csdn.net/u011229164/article/details/106763899