Dynamic Languages & iOS programs-Lua basics

Source: Internet
Author: User
Tags lua

Often see some games can change something without appstore, such as adding functionality.

This is actually done by downloading the script. The common script is JS and Lua.

Individuals are more familiar with Lua.

Lua is still a bull, the interpreter is very small, the speed is very fast, and the C language function of the interaction is also easy.

Integrating LUA into Xcode engineering

Very easy,

1. To the LUA website, download the source code, http://www.lua.org/download.html

2. In the project to integrate LUA, add a target to select the static library.

3. Pull the Lua source code (. h,. c) into the new static library and compile.

4. In the target project, link just compiled the. a file. Such as:


So the environment is ready.


Using LUA

First include a few Lua header files.

extern "C"//{#include "lua.h" #include "lualib.h" #include "lauxlib.h"//}
If it is a C + + environment, you need to open extern "C".


Preparing the Lua script file

Here, it is simple to create a resource. Actually this footstep can be downloaded from the server and saved on the local machine.

Create a local resource file It's simple, I'll just create an empty file here, named My.lua.


Then write the script:


This script is very simple to define a global variable myname and a function lefthandcall.

Call a LHC function inside the function to return.


Calling LUA functions

Give the code directly, and look at the comment:

-(Ibaction) Testlua: (ID) Sender {L = lual_newstate ();  Create a new Lua state lual_openlibs (L);  Loading LUA library NSString *scriptpath = [[[NSBundle Mainbundle] resourcepath] stringbyappendingpathcomponent:@ "My.lua"]; Read the Lua file path inside the resource file//nsstring* content = [NSString stringwithcontentsoffile:scriptpath encoding:nsutf8stringencod    ing Error:nil]; int ierror = Lual_loadfile (L, ScriptPath.  utf8string);  Loading lua Files Ierror = Lua_pcall (l, 0, 0, 0);  Execute first, you can get some variables of the lua script int iTop = Lua_gettop (l);        Gets the index of the top element of the stack, which should be 0, which is the stack or empty.  Lua_getglobal (L, "myname");  To get a global variable named MyName in Lua, the value of this global variable will be stacked ITop = Lua_gettop (l);        So this should be 1, the top index of the stack is 1, that is, there is only one element.  Const char* p = lua_tostring (L,-1);    You can get the value of the myname variable inside the Lua script.            printf ("Global variable Value:%s", p);  Lua_pushcfunction (L, myTest);        Put a C-language function on the stack.  ITop = Lua_gettop (l);  It should now be 2 Lua_setglobal (L, "LHC");        The MyTest function of the top of the stack is set to the global variable in the LUA environment, and the name of the Lhc,lua_setglobal function has a stack function. ITop = Lua_gettop (l);        This time, should be 1, this element is actually the value of the myname variable, you can try to get it again.        p = lua_tostring (L,-1);  Lua_pop (L, 1);        Removes an element from the top of the stack.  ITop = Lua_gettop (l);  should be 0 Lua_getglobal (L, "Lefthandcall");    Get the Lua script inside the function named Lefthandcall, press stack.  ITop = Lua_gettop (l);  should be 1 lua_pushnumber (L, 15);  Press-in Parameter Lua_pushnumber (L, 20);  Press-in Parameter ITop = Lua_gettop (l);        The top index of the stack is 3, a LUA script function, 2 parameters.  Ierror = Lua_pcall (L, 2, 1, 0);        Calling the LUA function removes the LUA function and two parameters from the stack, and then pushes the return value to the stack.  ITop = Lua_gettop (l);  3-3 + 1 = 1 printf ("RET:%s", lua_tostring (L,-1));        Prints the return value.  Lua_pop (L, ITop);        Empty the stack.  ITop = Lua_gettop (l);  0 lua_close (L); Turns off the LUA state. }
The above code mainly does a few things:

1. Create a new Lua state

2. Get the global variables in a LUA script.

3. MyTest A C-language function and set it to a global function inside the LUA environment.

4. Get the Lua script function and press the corresponding parameter

5. Call the Lua script function.

The MyTest function code is as follows:

int myTest (lua_state* L)  //This function will be executed by script. {    //detects if the 2 parameters passed in are numeric    if (!lua_isnumber (l, 1)) {        return lua_error (L);    }    if (!lua_isnumber (L, 2)) {        return lua_error (L);    }        Double A = Lua_tonumber (L, 1);    Double b = Lua_tonumber (L, 2);    A>b?lua_pushnumber (L, a): Lua_pushnumber (L, b);  Stack the large value of the 2 parameters, which is the return value.        Root.myLabel.hidden = NO;  Displays a label that was originally hidden.        return 1;}
Basically, the whole process is:

1. The iOS button response function calls the Lua script function Lefthandcall

2. Lua script function Lefthandcall call the LHC function

3. The LHC is a global function in the LUA environment, set by # #. The LHC's own code is C language. Which is the MyTest function above.

Run it and you get 2 results:

1. The return value of the MyTest function, which is the large value of the 2 parameter

2. Inside the MyTest, a label is displayed. See, click on the Testlua button to display the label "shown by Lua" above




Summarize

Using LUA in iOS development is simple and easy to use. In the example above, we show a label that can actually do more things.

For example, extend some simple functions.

With LUA, this dynamic execution script can add some flexibility to our programs. Sometimes, it's good to do some functional changes, but it's possible to eliminate the possibility of re-auditing, but occasionally, like AppStore, it may be rejected because of the integration of such features. But anyway, this always gives us a good way to adjust the program in a small range.

Of course, if the change is large, the best way is to publish a new version of the app, or LUA will be very complex.


Report:

Xcode 6, IOS SDK 8, LUA 5.3

viewcontroller.m//testlua////Created by Kevin on 15/3/3.//Copyright (c) 2015 Kevin. All rights reserved.//#import "ViewController.h"//extern "C"//{#include "lua.h" #include "lualib.h" #include "lauxlib.h "//}lua_state *l; viewcontroller* root;int myTest (lua_state* L)//This function will be executed by the script.    {//detects if the 2 parameters passed in are numeric if (!lua_isnumber (l, 1)) {return lua_error (L);    } if (!lua_isnumber (L, 2)) {return lua_error (L);    } Double A = Lua_tonumber (L, 1);    Double b = Lua_tonumber (L, 2);  A>b?lua_pushnumber (L, a): Lua_pushnumber (L, b);        Stack the large value of the 2 parameters, which is the return value.  Root.myLabel.hidden = NO;        Displays a label that was originally hidden. return 1;}        @interface Viewcontroller () @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload];        Additional setup after loading the view, typically from a nib. root = self;}    -(void) didreceivememorywarning {[Super didreceivememorywarning];    Dispose of any resources the can be recreated. }-(ibaction) Testlua: (ID) Sender {L = lual_newstate ();  Create a new Lua state lual_openlibs (L);  Loading LUA library NSString *scriptpath = [[[NSBundle Mainbundle] resourcepath] stringbyappendingpathcomponent:@ "My.lua"]; Read the Lua file path inside the resource file//nsstring* content = [NSString stringwithcontentsoffile:scriptpath encoding:nsutf8stringencod    ing Error:nil]; int ierror = Lual_loadfile (L, ScriptPath.  utf8string);  Loading lua Files Ierror = Lua_pcall (l, 0, 0, 0);  Execute first, you can get some variables of the lua script int iTop = Lua_gettop (l);        Gets the index of the top element of the stack, which should be 0, which is the stack or empty.  Lua_getglobal (L, "myname");  To get a global variable named MyName in Lua, the value of this global variable will be stacked ITop = Lua_gettop (l);        So this should be 1, the top index of the stack is 1, that is, there is only one element.  Const char* p = lua_tostring (L,-1);    You can get the value of the myname variable inside the Lua script.            printf ("Global variable Value:%s", p);  Lua_pushcfunction (L, myTest);        Put a C-language function on the stack.  ITop = Lua_gettop (l);  It should now be 2 Lua_setglobal (L, "LHC");        The MyTest function of the top of the stack is set to the global variable in the LUA environment, and the name of the Lhc,lua_setglobal function has a stack function. Itop = lua_gettop (L);        This time, should be 1, this element is actually the value of the myname variable, you can try to get it again.        p = lua_tostring (L,-1);  Lua_pop (L, 1);        Removes an element from the top of the stack.  ITop = Lua_gettop (l);  should be 0 Lua_getglobal (L, "Lefthandcall");    Get the Lua script inside the function named Lefthandcall, press stack.  ITop = Lua_gettop (l);  should be 1 lua_pushnumber (L, 15);  Press-in Parameter Lua_pushnumber (L, 20);  Press-in Parameter ITop = Lua_gettop (l);        The top index of the stack is 3, a LUA script function, 2 parameters.  Ierror = Lua_pcall (L, 2, 1, 0);        Calling the LUA function removes the LUA function and two parameters from the stack, and then pushes the return value to the stack.  ITop = Lua_gettop (l);  3-3 + 1 = 1 printf ("RET:%s", lua_tostring (L,-1));        Prints the return value.  Lua_pop (L, ITop);        Empty the stack.  ITop = Lua_gettop (l);  0 lua_close (L); Turns off the LUA state. } @end













Dynamic Languages & iOS programs-Lua basics

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.