lua table.remove引发的偶现bug

故事背景,两只宠物育种的时候要从背包删除,放入育种栏。育种完成再放回背包。现在育种完成背包多了一只。调试发现是删除的时候少删除了一只。

Lua 复制代码
if source_list_a == take_list then
	table.remove(take_list, index_a)
else
	table.remove(ware_list, index_a)
end
if source_list_b == take_list then
	table.remove(take_list, index_b)
else
	table.remove(ware_list, index_b)
end

如果a在b的前面,那么b remove会失败,remove后b的index_b其实已经改变了.

其实这个bug是ai写的,下面的代码也是ai改的

Lua 复制代码
-- 先移除索引较大的宠物,再移除索引较小的,避免索引变化问题
local remove_indices = {}
if source_list_a == take_list then
    table.insert(remove_indices, {list = take_list, index = index_a})
else
    table.insert(remove_indices, {list = ware_list, index = index_a})
end
if source_list_b == take_list then
    table.insert(remove_indices, {list = take_list, index = index_b})
else
    table.insert(remove_indices, {list = ware_list, index = index_b})
end
-- 按索引从大到小排序,先移除索引大的
table.sort(remove_indices, function(a, b)
    return a.index > b.index
end)
-- 按排序后的顺序移除
for _, remove_info in ipairs(remove_indices) do
    table.remove(remove_info.list, remove_info.index)
end
相关推荐
历程里程碑7 小时前
二叉树---二叉树的中序遍历
java·大数据·开发语言·elasticsearch·链表·搜索引擎·lua
万粉变现经纪人14 小时前
如何解决 pip install tensorflow-gpu 报错 未检测到 CUDA 驱动 问题
人工智能·python·深度学习·aigc·tensorflow·bug·pip
上海合宙LuatOS1 天前
LuatOS扩展库API——【exremotefile】远程文件管理系统
物联网·lua·luatos
初圣魔门首席弟子1 天前
boost配置遇到的bug
bug
上海合宙LuatOS1 天前
LuatOS扩展库API——【exremotecam】网络摄像头控制
开发语言·网络·物联网·lua·luatos
万粉变现经纪人2 天前
如何解决 pip install ta-lib 报错 本地 TA-Lib 库未安装 问题
数据库·python·scrapy·oracle·bug·pandas·pip
专吃海绵宝宝菠萝屋的派大星2 天前
使用postman测试自己编写的mcp服务
测试工具·lua·postman
Cc_Debugger2 天前
【饿了么plus-table】开启多选时,点击下面的单选按钮,页面显示是全选的样子,bug
bug
一个有温度的技术博主2 天前
Lua语法详解:从变量声明到循环遍历的避坑指南
redis·缓存·lua
上海合宙LuatOS2 天前
LuatOS扩展库API——【exmodbus】MODBUS协议
物联网·lua·luatos