I'm learning lua.
I don't understand why this is wrong.
This is my lua code
-- lua
Enemy = {
HP = 30,
SPEED = 8,
POWER = 10
}
And this is my cpp code. It will access to Enemy table and each value.
...
lua_getglobal(L, "Enemy");
/*
Current virtual stack
-----------------------------
[1] or [-1] Enemy table
*/
lua_pushstring(L, "HP");
/*
Current virtual stack
-----------------------------
[2] or [-1] "HP"
[1] or [-2] Enemy table
*/
lua_gettable(L, 1); // pop key("HP") and push Enemy["HP"]
/*
Current virtual stack
-----------------------------
[2] or [-1] 30(Enemy["HP"])
[1] or [-2] Enemy table
*/
// using the 30
double dHP = lua_tonumber(L, -1);
// I think that a top of the stack which is 30 will be pop.
lua_pop(L, -1);
/*
Current virtual stack
-----------------------------
[1] or [-1] Enemy table
*/
lua_pushstring(L, "SPEED");
/*
Current virtual stack
-----------------------------
[2] or [-1] "SPEED"
[1] or [-2] Enemy table
*/
lua_gettable(L, -2);
/*
Current virtual stack
-----------------------------
[2] or [-1] 8(Enemy["SPEED"])
[1] or [-2] Enemy table
*/
// when I use a top of the stack, VS throws the error.
// lua error said that I'm attempting index a nil value.
double dSpeed = lua_tonumber(L, -1);
If lua_tonumber pop the value, it makes sense. but there is no desc like that.
I want to know which part of stack is wrong.. Thank you.
lua_pop(L, n)
pops n
elements from the stack.
So, lua_pop(L, -1);
is not what you want.