Getting started with lua5.1

Source: Internet
Author: User

Lua is a very powerful scripting language. It can be used to write AI processes, write state machines, and generate Interfaces Based on this configuration on the client.
If you want to use this stuff, download it to the http://www.lua.org first.
When I write this article, the latest version is 5.1.
When using 5.1, it may be difficult to read the previous articles and examples. the reason is that when the new version of Lua initializes some libraries, there is a library that cannot be initialized luaopen_io (), and this function will be finished when called.
This is not the case in the new version.
L = lual_newstate (); // create a new lua_staue
Lual_openlibs (l); // Initialize all libraries
In the new version, we do not need to call it one by one. two functions. (It just needs to be too hard. Some class libraries may not be used in my application, which is a waste)
This time I used the Lua script to make a configuration file, instead of the previous XML document. the most important thing to use Lua in C ++ is the stack. you need to understand how this stack works. for convenience of debugging. you need to write a function to check the stack:

Void stackdump (lua_state * l)
{
 
Int I;
Int Top = lua_gettop (L );
For (I = 1; I <= top; I ++)
{
Int T = lua_type (L, I );
Switch (t)
{
Case lua_tstring:/* strings */
Cout <"String/t" <lua_tostring (L, I );
Break;
Case lua_tboolean:/* booleans */
Printf (lua_toboolean (L, I )? "True": "false ");
Break;
Case lua_tnumber:/* Numbers */
Printf ("number/T % G/N", lua_tonumber (L, I ));
Break;
Default:/* Other values */
Printf ("other values/T % s/n", lua_typename (L, t ));
Break;
}
Printf ("");/* put a separator */
}
Printf ::::::::::::::::: /n ");
}

After calling this item, we can export the stacks in lua_statue. This is very convenient.
Describes the functions:
When lual_newstate is used, a lua_statue will be created.
Lual_openlibs initializes all libraries
If (lual_loadfile (L, szfilename) | lua_pcall (L, 0, 0, 0) There are two functions. one is to load files. after the second pcall function is called, The loaded Lua file will be loaded into the memory block. for later query.
Lua_getglobal (L, "width"); this is to press a variable in the global stack, and then it will call the query immediately, pop once during the call, after the result is queried, the result is pushed to the stack. use the lua_tonumber () function to obtain the content of an element in the stack. lua_pop () is used in the pop stack. pop is required after the query is complete. the lua_gettable () function is special. when you enter a table and want to query the content in the table, you need to call this function after lua_pushstring. you can get the content in this table. the process in lua_getglobal is separated into two parts. there is a push process and a query process.

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.