Introducing Lua in C + + projects (scenarios used by Alphago)

Source: Internet
Author: User
Tags lua

The recent blaze of the Alphago, which DeepMind has open source, can be downloaded to GitHub Https://github.com/deepmind/lab, online and a python-based open source Alphago, which is not Google. By looking at the DeepMind source code, we can know that Alphago is using C + + and LUA scenarios. Of course, language is not the focus of Alphago, but it also illustrates the ability of C + + and LUA programs to accomplish large projects.
Since 2009, the development efficiency of the lua,c++ project has been greatly introduced in the three-dimensional simulation project. In our three-dimensional simulation of this high performance-demanding project, LUA has no significant impact on performance. My earliest contact with Lua was to see the task scripts in World of Warcraft, and later read the cloud blog, which he introduced into the online games of NetEase, which allowed me to make sure I introduced this script in my project. There are also many applications and games that use LUA as the embedded scripting language for configuration and extensibility. This includes World of Warcraft, the gate of Lourdes, Angry Birds, VOCALOID3, sun god Three Kingdoms kill. When you play the game, look at his installation directory, you will often see the Lua suffix of the script file.
Since 2010, our company began to enter the field of video processing, due to the high performance requirements and technical requirements of video analysis processing, so the development cost is also very high, generally a year or two of C + + experience developers are difficult to produce results, but after the introduction of LUA, some new people can also participate in the project, after the core structure is built , the introduction of LUA to the development of some of the business logic into LUA has not only improved development efficiency, but also reduced developer requirements.
In this multi-year project development, summed up, for the C + + project no matter how big, be sure to reference a script, Lua is one of the more mature and reliable solution. Lua script source is very small without dependencies, can easily be compiled on various platforms, LUA source not only provides code execution, but also provides compiled code, that is, you can write the Lua script you wrote to prevent source leakage, and improve execution efficiency. The Lua script also provides debugging breakpoint functions, so you can develop a debugging tool yourself, and of course there are third-party provided open-source breakpoint Debugging tools, we all know that writing C + + program if not debugging that development efficiency will be greatly reduced. That's why I'm using LUA and I'm not using node. js for the time being.
Here's a look at Lua:
Lua is a small scripting language. A research group in the Catholic University of Rio de Janeiro, Brazil (Pontifical Catholic University of Rio de Janeiro), by Roberto Ierusalimschy, Waldemar Celes and Luiz Henr Ique de Figueiredo was formed and developed in 1993. It is designed to be embedded in the application, providing flexible extension and customization capabilities for the application. LUA is written in standard C and can be compiled and run on almost all operating systems and platforms. LUA does not provide a powerful library, which is determined by its positioning. So Lua is not suitable as a language for developing standalone applications. Lua has a simultaneous JIT project that provides instant compilation capabilities on a specific platform.
Lua scripts can be easily called by C/C + + code, or they can be used in turn to C + + functions, which makes LUA widely available in applications. Not only as an extension script, but also as a normal configuration file, instead of the Xml,ini file format, and easier to understand and maintain. LUA is written in standard C, and the code is simple and beautiful, and can be compiled and run on almost all operating systems and platforms. A complete LUA interpreter but 200k, the speed of Lua is the fastest in all of the scripting engines available. It all determines that Lua is the best choice for embedded scripting.
      Lua has other features as well: support for process-oriented (procedure-oriented) programming and functional programming (functional programming); Automatic memory management Only a common type table is provided, which can implement array, hash table, set, object, language built-in pattern match, closure (closure), function can also be regarded as a value; provide multithreading (collaborative process, not supported by operating system) support , closures and table can easily support some of the key mechanisms required for object-oriented programming, such as data abstraction, virtual functions, inheritance, and overloading.
We do an example of Lua calling C + + functions
//////////////////////////////////////////////////////
///c++ Code Section
int LUACALLC (lua_state *l)
{
   //Get first parameter
     int para1 = Lua_tonumber (L, 1);

   //Get the second parameter
    int para2 = lua_tostring (L, 2);
    return 0;
}
//register this C + + function to call Lua
Lua_register (LUA, "LUACALLC", LUACALLC);

-----------------------------------------------------
---The LUA code section
---call C + + functions
LUACALLC (100, " TestString ")


     This example is just a demonstration of a downgrade process, a multi-type parameter, a return value, Type checking is not added. But with this example you can see that C + + is simple to interact with LUA, and the same C + + call Lua function is relatively straightforward. I'll come back with some more detailed teaching articles later.
can also see me a video tutorial

C + + LUA co-programming (C + + programmer skills required)


Http://edu.51cto.com/course/course_id-7559.html


650) this.width=650; "Src=" Https://s1.51cto.com/images/201611/366a8b831204aff34233422f66d1cf2f6dbd39_big.png " Width= "487" height= "365" alt= "366a8b831204aff34233422f66d1cf2f6dbd39_b"/>

This article from "Xia Chaojun" blog, declined reprint!

Introducing Lua in C + + projects (scenarios used by Alphago)

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.