Basic Applications of Lua and Delphi

Source: Internet
Author: User

Basic usage of Lua.
1. Open Lua:
VaR L: plua_state; </P> <p> // open Lua <br/> L: = lua_open; </P> <p> // you can continue to load the Lua standard library <br/> lua_baselibopen (l); <br/> lua_iolibopen (L ); <br/> lua_strlibopen (l); <br/> lua_mathlibopen (l); <br/>

2. After running, you must turn off:
Lua_close (L );

3. Add a method in Lua to pass function parameters to the Host Program. For example, add the print ("mrlong") method to the Lua script, which must be defined as follows:
Function luaprint (L: plua_state): integer; cdecl; <br/> var <br/> I, n: integer; <br/> begin <br/> N: = lua_gettop (l); // n = Total number of function parameters obtained <br/> for I: = 1 to n do <br/> showmessage (lua_tostring (L, i); <br/> result: = 0; <br/> end;

In the Lua script, you can use the print ("hello", "world", "USD") function to transmit data to the Host Program for processing.

 

4. Add a method in Lua to pass the host program variables to Lua, such as getuserid (), which can be defined as follows:

Function luagetuserid (L: lua_state): integer; cdecl; <br/> begin <br/> lua_pushnumber (L, 10000000); // the return value is 10000000, push 30 to the stack <br/> // lua_pushstring (L, 'usd000000'); // or return 'usd000000' <br/> result: = 1; // define the number of returned parameters, which is not a specific value. The value is 1 <br/> end;

In the Lua script, you can use the getuserid () function to obtain a value of 10000000.

5. After activating Lua, the registration method is as follows:

Lua_register (L, 'print ', luaprint );

Lua_register (L, 'getuserid', luagetuserid );

 

6. Load script:

// Load the script <br/> lual_loadfile (L, 'testscript. lua'); // load the script file to the memory <br/> If (lua_resume (L, 0) <> 0) then begin // execute the script loaded into the memory <br/> showmessage ('err'); <br/> end;

 

7. Retrieve the parameters of the registration method: for example, print ("mrlong") is used in the right Lua script. In this case, when I want to retrieve mrlong, I will use it.
S: = lua_tostring (L, 1); // note that this is from 1, not both 0 and Delphi and C/C ++. /
If (lua_gettop (l) <> 2) then // Where lua_gettop (l) is the data of the retrieved Parameter
Lual_error (L, 'number of parameter errors ');

Resource: http://luaforge.net/frs? Group_id = 157 & release_id = 706

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.