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
相关推荐
深念Y1 小时前
踩坑实录:把 Windows 默认 PowerShell 换成 7.x 到底有多坑?一条龙解决指南
windows·乱码·bug·控制台·powershell·管道·流式
油炸自行车3 小时前
【Qt bug排查】Qt项目debug模式下,关闭可视化界面后,后台程序不退出,依然在打印log
bug
晴夏。10 小时前
unlua实现原理
游戏·ue5·ue4·lua·ue·unlua
SoraShim10 小时前
2026实测:深入理解Gemini 3 Pro镜像站链式思考提示工程,三步解决复杂Bug定位
bug
晴夏。11 小时前
c++调用lua的方法
c++·游戏引擎·lua·ue
chxii1 天前
lua中Table 与 Metatable
lua
xingpanvip1 天前
星盘接口开发文档:组合三限盘接口指南
android·开发语言·前端·python·php·lua
AI 编程助手GPT1 天前
GPT-5.6意外曝光、Claude安全检查全面公测、Grok 4.3搅局价格战——多模型混战的五月,开发者如何避坑?
人工智能·gpt·ai·chatgpt·bug·ai编程
chxii2 天前
lua流程控制语句和table(表)数据结构
开发语言·junit·lua
Zxxxxxy_2 天前
测试入门:从 0 到 1 搞懂开发与 Bug
bug