What parts of Lua are used in the project

Source: Internet
Author: User

Yesterday, a colleague told me that our hand-travel client (cocos2d-x LUA binding) code did not see Lua's special tricks, which used to be similar to other languages. After all, "programming in LUA" has nearly 400 pages, and he wants to know where the language features of Lua are used. When the answer is not up, think about it now.

The first answer to his question is two other questions:

1. Why is LUA selected for our project?

The LUA website describes Lua in this way: fast, portable, embeddable, powerful (but simple), small and free. These are part of the reason for choosing Lua, especially fast and small, and another important reason is that the game version iterations are fast and the Lua script is updated easily. I think that's why COCOS2DX officially provides the LUA bindings.

And in the case of the project group itself, because the client's colleagues before are page tour origin, use AS3, contact C + + less, are newly learned, Lua's learning cost is lower than C + +.

What language features do 2.lua have?

Automatic GC, using dynamic type, function as first-class value, closure, universal table, co-process, and simple interface for C/C + +. Think of so much for the time being, the first four are more common, the general dynamic language will have, the "characteristics" here is relative to C + + and other static language.

Combine items individually:

Automatic GC is naturally used unconditionally, to consider the timing of the GC, by default LUA is a step GC, and we have chosen to perform a full GC process at each frame. Why I did not ask the main course, may be the memory of the mobile phone is more valuable, such as the iphone over 200M immediately kill process.

Dynamic type is also unconditional use, the most obvious effect is to declare the variable does not need to write lengthy types, but sometimes you will find that writing is very cool, but the time to see the pit, so my LUA code comments more than C + +.

function as the first class of values, in fact Lua's funcion is not bound upvalue closure, so is actually a closure as the first class of values. Setting closures as callbacks is straightforward:

Btn.onclick (function () textfield.settext (TXT))

and C + + would probably write something like this:

void SetText (Uitextfield *text_field, std::string &txt) {    Text_field-SetText (TXT);} BTN->onclick (Std::bind (SetText, Text_field, TXT))

And this is the c++11, not to mention the tedious function class.

The almighty table. The implementation of the LUA table is a hash table that can be used with any value other than nil as key, and with any values as value. This allows you to emulate the struct of a C + +, in fact table is the LUA custom data structure mechanism. In addition, the table has the array part and the hash table two parts, so usually does not need to implement the array itself.

Table is naturally a lot of use in projects, but indeed metatable and weak tables are seldom used.

Co-process is a good thing, but the project is not used.

C/C + + Concise interface, the most obvious example is the package and delivery of the protocol, all calls to the module. COCOS2DX used tolua++, but apparently tolua++ used the same interface in Lua.h.

On the whole, as a developer of the application layer, it really does not need to use the things that are too advanced in Lua, but this is actually the advantage of LUA and the original goal of the project team-to allow beginners to work quickly. Another convenience, if you want to see advanced things, directly to see the bottom ah, look at the implementation of the LUA source code, see how the LUA layer setting callback C + + is how to maintain.

Anyway, the ability of a programmer is not reflected in the use of complex languages.

What parts of Lua are used in the project

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.