In recent problem has been to find the function stack like, just have the opportunity today and share it.
Want lua script file transfer function in C ++ using relatively simple, because lua scripts come and C \ C ++ through the stack to stop, according to the number of interactive,
So the original reason is also very simple, just need to put function lua script pushed onto the stack
Then sent to the function parameters may also be needed in the order from left to right is pressed into the stack,
Then able to perform this function a
After the function is executed, it will move from being deleted just pushed us to, according to the number of the stack, then the function's return value onto the stack
Li original way, but it is through the stack to stop, according to several interactive
Look at a small order process:
This sequence Cheng will demonstrate how to adjust the overall amount of the lua script file with
void main()
{
lua_State* pluaState=luaL_newstate();
luaL_openlibs(pluaState);
// Lua script execution
luaL_dofile(pluaState,"../Script/test.lua");
// The script file Pi amount of PSA into the heap Village
lua_getglobal(pluaState,"Pi");
cout << "read script lua amount Pi:" << (float) lua_tonumber (pluaState, 1) << endl;
lua_pop(pluaState,1);
// release resources
lua_close(pluaState);
getchar();
}
-------------------------------------------------- ------- the following is a lua script file -------------------------------------- -----------------------------------------
- there is only one line of code defines a global quantitative pi
Pi=3.1415926
This part of the order process in bold face the reality of this place is the function code
Lua_getglobal function () is used to overall volume lua script file transformer onto the stack, this function has two parameters, the first parameter is lua state machine (in number with the stored data),
The second parameter is the amount you want to get a global name identifier ..
Lua_tonumber function () is read, according to the number of designated stack location and converted into numerical, the first parameter to this function is lua state machine, the second parameter is the position of the data to be read
Finally, we will read out the data and then converted to a floating-point number, according to the number printed to the console by cout ..
The last function lua_pop () does not matter, but in order to continue following the current party and then lua stack according to the number of stops for the interactive operation, adjustment to the best use of this function ..
Of this function is to delete the stack according to the number specified location
love, sometimes not vows of commitment required, but she must needs meticulous care and greetings to detail; love, sometimes do not need to tragic Butterfly butterfly, but she must needs heart have a tacit understanding with the appeals; love, sometimes do not need to follow Fei from the female, but she must need to profound feelings of support and understanding.
Tune with lua scripts with similar functions and quantitative global dimming lua script file, as little reason as the original, but with a twist
Bold part code before the code replacement, the rest does not change
// function call with lua script
lua_getglobal(pluaState,"Add");
lua_pushnumber(pluaState,1);
lua_pushnumber(pluaState,1);
lua_call(pluaState,2,1);
cout<<"1+1="<<(int)lua_tonumber(pluaState,1)<<endl;
lua_pop(pluaState,1);
-------------------------------------------------- --------- the following is a script file ------------------------------------- ----------------------
- function, which in ten brief, an adder is considered realistic operation, returns the result of adding two numbers
function Add(intX,intY)
return intX+intY;
end ------------------------------------------------- the following is a script file ------------------------------------ ---------- -----------------------
This C ++ sequence above process:
lua_getglobal (pluaState, "Add"); This function Add lua script () function is pushed onto the stack
lua_pushnumber (pluaState, 1); This function will assign numeric data onto the stack number in
lua_pushnumber (pluaState, 1); ibid.
lua_call (pluaState, 2,1); performing functions in the stack just been pressed, the last two parameters of this function are the number of arguments and return values of the number of
After the function is executed will have been deleted is forced into the stack data number, and then will return the value onto the stack
cout<<"1+1="<<(int)lua_tonumber(pluaState,1)<<endl;
lua_pop(pluaState,1);
It is now more than two pieces of code in the stack return value of the output, and then delete ,, he will continue following the current party and lua stack stop, according to several interactive
The end of the article to share some of jokes quotations under programmer: Real Programmers like and sell popcorn, they use the CPU heat emanating popcorn, you can hear what the program is running according to rice flower burst of speed.