检查表格值是否以...开头。

local maps = find.File("maps/*.bsp", "GAME")  // 读取 garrysmod/maps 目录下所有以 .bsp 结尾的文件并放入 TABLE 中
map = (maps[math.random( #maps )])  // 从 maps TABLE 中随机选取一张地图
    print("Map randomized to " .. map )  // 输出选定的地图名称

上面的代码适用于 Garry's Mod 的 "ULX",它使用 find.File 来读取 garrysmod/maps 目录并返回所有以 .bsp 结尾的文件(即所有地图)。但是,我不希望它包含以 "arena_" 和 "gm_" 开头的地图,有没有一种方法可以删除它们或保持检查,直到它获得不以这些内容开头的地图呢?请问有纯 Lua 的解决方案吗?另外,我正在使用 MOAIFiddle 网站进行测试。

点赞
用户3696649
用户3696649

看起来你想用的是 file.Find 函数,而不是 find.Filehttp://wiki.garrysmod.com/page/file/Find

以下代码可以在 Garry's Mod 中使用。

local maps = {}
local ignoredPrefixes = {"gm_","arena_"}

for _,map in ipairs(find.File("maps/*.bsp", "GAME"))do
  local hasPrefix=false
  for _,prefix in ipairs(ignoredPrefixes)do
    if string.Left(map,string.len(prefix)) == prefix then
      hasPrefix=true
      break
    end
  end
  if not hasPrefix then
    table.insert(maps,map)
  end
end

local randomMap = table.Random(maps);

print("Map randomized to "..randomMap)

它的作用是读取 maps/ 目录下的所有地图,并将不带有 ignoredPrefixes 中定义的特定前缀的所有地图添加到 maps 表中。完成后,从表中随机选择一个地图。

2015-05-12 17:47:42