1

我对 lua 编码相当陌生,一直在为 GTA 5 FiveM 脚本做这件事我也是新手,如果我这样做或说解释错误,我很抱歉。

我一直在尝试制作一个脚本来处理车轮的名称、它们的 ID 以及它们是否已经安装在车辆上。

我遇到的问题是更多的强迫症正在创建它的表。获取轮子名称的方法是通过它们的 ID,但这会导致重复名称的问题,但它们实际上是它们的不同变体。

为了清楚起见,这是我用来制作表格的代码片段:

for i = 1, (GetNumVehicleMods(vehicle, 23) +1) do

    local modName = GetLabelText(GetModTextLabel(vehicle, 23, (i-1)))

    txt = ""
    if GetVehicleMod(vehicle, 23) == (i-1) and tonumber(originalWheel) == tonumber(wheeltype) then  
        txt = "Currently Installed"
    end
        validMods[i] =
        {
            id = (i-1),
            name = modName,
            install = txt
        }
end

这...据我所知,它通过 ID 列出了车轮列表,例如:

validMods = {
    [1] = { id = 0, name = "Inferno", install = "Currently Installed" },
    [2] = { id = 1, name = "Deep Five", install = "" },
    [3] = { id = 2, name = "Inferno", install = "" },
    [4] = { id = 3, name = "Deep Five", install = "" }
}

这显然只是一个基本的类型示例,其中实际的表格可以有多达 200 个杂乱无章和重复的结果..但我一生无法弄清楚如何将表格重新排列为按字母顺序排列。保持内容完整,但将它们重新组织为从名称开始而不是这样?

validMods = {
    [1] = { id = 0, name = "Inferno", install = "Currently Installed" },
    [2] = { id = 2, name = "Inferno", install = "" },
    [3] = { id = 1, name = "Deep Five", install = "" },
    [4] = { id = 3, name = "Deep Five", install = "" }
}

另外,如果可能的话……是否可以在重复名称的末尾添加额外的文本……例如:Inferno、Inferno var2、Inferno Var3


感谢您提供的任何帮助

4

1 回答 1

0

当您按特定顺序获取这些名称时,您需要在填充表格后对它们进行排序。

要重命名重复项,请计算您遇到名称的频率。

-- demo input
local validMods = {
    [1] = { id = 0, name = "Inferno", install = "Currently Installed" },
    [2] = { id = 1, name = "Deep Five", install = "" },
    [3] = { id = 2, name = "Inferno", install = "" },
    [4] = { id = 3, name = "Deep Five", install = "" }
}
-- count and rename
local names = {}
for i,v in ipairs(validMods) do
 if names[v.name] then
   names[v.name] = names[v.name] + 1
   v.name = v.name .. " var" .. names[v.name]
 else
  names[v.name] = 1
 end
end
-- sort the table by name ascending
table.sort(validMods, function(a,b) return a.name < b.name end)

-- print sorted names
for i,v in ipairs(validMods) do print(v.name) end
于 2021-12-06T15:47:50.660 回答