lua - 你能检查一下我对 lua 堆栈的理解是否正确吗?
问题描述
我正在学习lua。
我不明白为什么这是错误的。
这是我的lua代码
-- lua
Enemy = {
HP = 30,
SPEED = 8,
POWER = 10
}
这是我的 cpp 代码。它将访问 Enemy 表和每个值。
...
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);
如果lua_tonumber弹出该值,则有意义。但没有这样的描述。
我想知道堆栈的哪一部分是错误的。谢谢。
解决方案
lua_pop(L, n)
从堆栈中弹出n
元素。
所以,lua_pop(L, -1);
不是你想要的。
推荐阅读
- php - Flutterwave 支付 API 与 PHP 和 Mysql 的集成
- sql - 查询三层表的 SQL 性能
- python - Chromedriver远程版本:87禁用'gelocation api'
- flutter - 图像在一瞬间溢出列
- html - 滚动时的粘性叠加图像
- android - 尝试使用离子/电容器访问物理设备中的 Documents 文件夹时无法解析文件系统路径
- angular - 修改在 ag-grid 上排序的内容
- javascript - 通过对象映射并在 React Native 中渲染
- java - 如何为我的 java Swing graphics2d 组件制作动画
- javascript - 无法从 Node JS 更新 MongoDB 中的产品记录