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
相关推荐
技术小黑屋_8 小时前
Vibe Coding 真正的问题并不是 Bug
bug
初见无风8 小时前
3.1 Lua代码中的元表与元方法
开发语言·lua·lua5.4
haierccc12 小时前
我发现了windows的tracert命令的一个bug---ICMP重定向包详尽分析
bug
千里镜宵烛15 小时前
Lua--数据文件和持久性
开发语言·junit·lua
2401_841495641 天前
Windows 系统中ffmpeg安装问题的彻底解决
windows·python·ffmpeg·bug·语音识别·下载·安装步骤
壹佰大多1 天前
【Redisson分布式锁源码分析-3】
数据结构·分布式·mysql·spring·spring cloud·wpf·lua
l1t1 天前
对luasql-duckdb PR的测试
c语言·数据库·单元测试·lua·duckdb
初见无风1 天前
3.3 Lua代码中的协程
开发语言·lua·lua5.4
l1t1 天前
利用DeepSeek辅助改写luadbi-duckdb支持日期和时间戳数据类型
c语言·数据库·人工智能·junit·lua·duckdb·deepseek