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
相关推荐
不断学习加努力2 天前
使用rviz2进行可视化时,cpu资源占用过高的bug
bug
happyness442 天前
如何利用 AI 自动编写单元测试(Unit Test)来捕捉隐藏的边缘情况 Bug?
人工智能·单元测试·bug
寒水馨2 天前
Windows下载、安装neovim-v0.12.4(附安装包nvim-win64.msi)
windows·编辑器·vim·lua·终端·lsp·neovim
深念Y2 天前
Windows幽灵端口占用:HNS如何无声偷走你的端口
windows·python·bug·环境·端口·特权
烤代码的吐司君2 天前
Lua 脚本
开发语言·junit·lua
寒水馨2 天前
macOS下载、安装neovim-v0.12.4(附安装包nvim-macos-arm64.tar.gz)
macos·vim·lua·文本编辑器·终端·lsp·neovim
背书包的甜瓜3 天前
Python Excel转lua配置工具
lua
minglie14 天前
esp32集成lua
lua
番茄炒鸡蛋加糖4 天前
Redis--Lua 脚本原子性与滑动窗口限流
数据库·redis·lua
星空露珠5 天前
28种颜色对应名称,
开发语言·数据库·算法·游戏·lua