A detailed description of Lua script Manager

Source: Internet
Author: User

Lua scriptManager Details are the content to be introduced in this article. This is not a text editor or ide, but it is actually a dll,LuaIt is the glue language. The purpose of this manager is to make C/C ++ better support.Lua.

Management in projectsScriptThe methods are separate file storage,LuaThere is no concept of engineering, so the file is the smallest unit, so the increase in the number of script files will bring a problem, name pollution. The variables defined in lua are global by default. Only the local variables are defined locally, in this way, the function you define in script a may have the same name as the function defined in script B. This does not cause any errors or warnings, because the function is a value in lua, you can modify it at any time.

Export a global function Include, which can include other scripts, just like # Include in C. In fact, it does not contain any content, but only establishes a relationship network between scripts, each script is a vertex, and Include establishes an arrival relationship. Each script is isolated, that is, the variables in different scripts can have the same name but different variables, which are implemented through the function environment.

The key thing is the priority relationship. Although there is no statement defining variables in lua, there are index and newindex events, the index event is triggered when t [k] is set for a set of values, and the newindex event is triggered when t [k] = v is set for a set of values. The latter is equivalent to defining variables, that is, the first use.

The index priority is: local> script> register-table> global. register-table is the sum of some items exported from C. The general principle is that more specific things are considered first. First, find the local variable. If the local variable is not found, find the variable in the current environment. If the local variable is not found, find the variable in other scripts that can be reached by the Include relationship. If the variable is not found, find the variable in register-table, the global variable is not found.

The newindex priority is: local> script (no include)> global. If k is local, set k to v. Otherwise, if t is table, rawget (t, k) first) if it is found, set the value. Otherwise, the value in global is global. Otherwise, set the script. There is some confusion here. In other words, there is no value in the script and global at the same time. The value in global will still affect the value in the script, that is, do not define too many global values, for example, if you write in a script:

  1. function print()  
  2. end 

It will think of it as global print and redefine it.

In fact, you cannot define a new global Value in the script. If you define a value that does not exist in the global, it will be converted to a value in the script environment. Except for some system functions, the exported functions are not directly stored in global, so new global values cannot be defined.

Well, it limits you and gives you flexibility. There is a global function in Global that defines the global value and accepts 1 to 2 parameters. The first parameter is the name and the second parameter is the initial value. If you call an existing global value, an error message is returned, indicating that you have redefined a value. You can write in a script as follows:

  1. Global("Foo")  
  2. function Foo()  
  3. end 

In this case, Foo is not here.ScriptAnd can be accessed in other scripts without using Include.

In addition, other functions are being added, suchLuaSerialization will be added in the futureLuaUser Data and lua debugging. I still learned a lot in this process. I found that I didn't need to find any information to learn lua. I just went to the official website to check the manual. The manual has only one page, which is very convenient.

Summary: Resolve a question aboutLua scriptThe content of the manager has been introduced. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.