Garry's Mod Wiki

search.AddProvider

  search.AddProvider( function provider, string id = nil )

Description

Adds a search result provider. For examples, see gamemodes/sandbox/gamemode/cl_search_models.lua

Arguments

1 function provider
Provider function.
Function argument(s):
1 string searchQuery - The search query user has inputed.
Function return value(s):
1 table data - You must return a list of tables structured like this:
  • string text - Text to "Copy to clipboard"
  • function func - Function to use/spawn the item
  • Panel icon - A panel to add to spawnmenu
  • table words - A table of words?
2 string id = nil
If provided, ensures that only one provider exists with the given ID at a time.

Example

Model search from gamemodes/sandbox/gamemode/cl_search_models.lua with added comments

-- -- Model Search -- local model_list = nil search.AddProvider( function( str ) if ( model_list == nil ) then model_list = {} GetAllFiles( model_list, "models/", ".mdl", "GAME" ) end local models = {} for k, v in pairs( model_list ) do -- Don't search in the models/ and .mdl bit of every model, because every model has this bit, unless they are looking for direct model path local modelpath = v if ( modelpath:StartWith( "models/" ) && modelpath:EndsWith( ".mdl" ) && !str:EndsWith( ".mdl" ) ) then modelpath = modelpath:sub( 8, modelpath:len() - 4 ) end if ( modelpath:find( str, nil, true ) ) then if ( IsUselessModel( v ) ) then continue end local entry = { text = v:GetFileFromFilename(), -- Text to "Copy to clipboard" func = function() RunConsoleCommand( "gm_spawn", v ) end, -- Function to use/spawn the item icon = spawnmenu.CreateContentIcon( "model", g_SpawnMenu.SearchPropPanel, { model = v } ), -- A panel to add to spawnmenu words = { v } -- A table of words? } table.insert( models, entry ) end if ( #models >= sbox_search_maxresults:GetInt() / 2 ) then break end end return models end, "props" ) -- ID: "props"