Just getting a global variable is so boring, today we're going to play a very difficult--get the data from the LUA table structure.
(Narrator: o O!) Should I say something? )
Previous Chapter Portal: http://www.jb51.net/article/55093.htm
1. What is table
The table is the most powerful data type in Lua, and we can think of it as an array, but it's a bit different from the other, suggesting that you take a look at the LUA Grammar tutorial because I'm not familiar with the table to explain.
(Narrator: What other tutorials do you write ...)? )
2. Get table variable
Now we add a table global variable to the Hellolua.lua file:
Copy Code code as follows:
--Hellolua.lua file
myname = "Beauty Girl"
hellotable = {name = "Mutou", IQ = 125}
We see that there is a hellotable variable, it's very similar to the array and it's similar to HashMap, and it's very powerful.
(Narrator: I think the highlight is, you have 125 IQ?) I think multiplying by 2 would be a bit ~!. )
Say, 125 times 2 equals how much? ... ... o o!
The way to get hellotable variables is the same as before:
Copy Code code as follows:
/* Get table variable, on top of Stack * *
Lua_getglobal (PL, "hellotable");
In this way, the hellotable variable is stored on top of the stack.
But we're not going to take the table variable, because the LUA table type is not recognized in C + +, so we're going to get the exact value of the table, which is the value of name and IQ.
3. lua_gettable function
There is a function similar to Lua_getglobal, called lua_gettable, which is used to obtain table-related data.
(Narrator: less nonsense. = =)
The lua_gettable function will get a value from the top of the stack, then go to the table to find the corresponding value, and finally put the found value on the top of the stack.
The lua_pushstring () function can store strings in C + + into the LUA stack;
Then use Lua_gettable () to perform the steps described earlier, and the second parameter of Lua_gettable is the index of the specified table variable on the stack.
(Narrator: Small stupid wood, I was you around the halo ...) )
To take care of the narrator, we draw a picture to understand:
This is the initial state and there is nothing in the stack, so now you have to put the hellotable variable on top of the stack:
Copy Code code as follows:
/* Get table variable, on top of Stack * *
Lua_getglobal (PL, "hellotable");
And then it becomes like this:
Next, we're going to get the value of the table's name, so the first thing to do is to put the "name" string into the stack:
Copy Code code as follows:
/* Put the C + + string into the LUA stack, at which point the top of the stack becomes "name" and the Hellotable object becomes the bottom of the stack.
Lua_pushstring (PL, "name");
And then it becomes like this:
(Narrator: Without this ah, you secretly added the index of the stack ~! )
Note that I have added the index of the stack as we are about to use, this time we use negative index (the friend who does not know the index of negative numbers please read the No. 03 chapter of the Tutorial Ha ~).
Because of the "name" stack, the hellotable variable is no longer on the top of the stack.
Next, we call the most important step to get the value of name in the table:
Copy Code code as follows:
/*
Look for the value for "name" from the Table object (the Table object is now on the stack with index-2, which is the current stack bottom),
After you get the corresponding value, put the value back on the top of the stack
*/
Lua_gettable (PL,-2);
At this point, the stack becomes like this:
(Narrator: What happened?) Why does "Mutou" suddenly appear on the top of the stack?! For Mao! You painted it yourself! )
What did lua_gettable do?
First, let's explain Lua_gettable's second argument--2--what it means, and -2 is the index of just hellotable variables on the stack.
Lua then goes to the top of the stack (the top of the stack is "name"), and then takes the value to the hellotable variable to find the corresponding value, of course, find the "Mutou".
Finally, Lua puts the found value into the stack, so "Mutou" is on top of the stack.
(Narrator: Your Sister paper ...) It's okay, I swear.
Finally we just need to take out the data on the top of the stack, the complete code is as follows:
Copy Code code as follows:
/* Initialize * *
lua_state* PL = Lua_open ();
Luaopen_base (PL);
/* Execute script/*
Lual_dofile (PL, "Hellolua.lua");
/* Get table variable, on top of Stack * *
Lua_getglobal (PL, "hellotable");
/* Put the C + + string into the LUA stack, at which point the top of the stack becomes "name" and the Hellotable object becomes the bottom of the stack.
Lua_pushstring (PL, "name");
/*
Look for the value for "name" from the Table object (the Table object is now on the stack with index-2, which is the current stack bottom),
After you get the corresponding value, put the value back on the top of the stack
*/
Lua_gettable (PL,-2);
/* Now the name of the table corresponding to the value is already on the top of the stack, directly removed can be * * *
Const char* sname = lua_tostring (PL,-1);
Cclog ("name =%s", sname);
Okay, here's the end of this chapter.