0
local maps = find.File("maps/*.bsp", "GAME")
map = (maps[math.random( #maps )])
    print("Map randomized to " .. map )

This code above works on "ULX" on Garry's Mod, it uses find.File to read the directory of garrysmod/maps and return (in TABLE) all of the files in it ending with .bsp (all of the maps), however I do not wan't it to include maps that begin with certain parts, like "arena_" and "gm_", is there a way I can go about removing them and/or making it keep checking until It gets a map not beginning with that.

Any way I could do this? and preferable pure Lua please. Oh and the site I'm using to test it is MOAIFiddle

user3521680
  • 97
  • 1
  • 2
  • 7
  • 1
    The `find` function is a string function and is available through the default string metatable (so you can do `strvar:find(...)`. That function isn't available via the default table metatable (there is no such metatable). What are you trying to do? Test each value in the table against the pattern? – Etan Reisner May 12 '15 at 03:20
  • I'm using file.Find (from garry's mod) to return the full directory of my maps in a table, and I want to make sure the map doesn't start with "arena_" (http://wiki.garrysmod.com/page/file/Find) – user3521680 May 12 '15 at 03:27
  • If the table is like the one in your question `{"hello world", "test"}`, do you want to check if the first string starts with `hello`, or any string, or all strings? – Yu Hao May 12 '15 at 03:34
  • `file.find` and `string.find` are *entirely* different functions and I'm not sure how `file.find` is relevant here. That snippet is using `string.find` and the error is because you tried to replace a string with a table in that snippet which doesn't work. What is your goal for the snippet in your question when `ss1` is a table? – Etan Reisner May 12 '15 at 03:34
  • Alright I was trying to use that as an example because find.File isn't normally in lua, but I'll edit it and re-explain, sorry. – user3521680 May 12 '15 at 03:40
  • Write a function that takes a name and checks it (using `string.find` or `string.match`) against each prefix you want to avoid and when it finds a name that works it returns the name. Then loop while getting random values from the returned list until you get a name that passes the function. – Etan Reisner May 12 '15 at 16:18

1 Answers1

1

It looks like the function you meant is file.Find instead of find.File. http://wiki.garrysmod.com/page/file/Find

The following should work in 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)

What it does is read all maps in maps/ and then add all maps that do not have a certain prefix, defined in ignoredPrefixes, to the maps table. When it is done doing so, a random map will be selected from the table.

Kurt Stolle
  • 322
  • 4
  • 18