search.AddProvider
Description
Adds a search result provider. For examples, see gamemodes/sandbox/gamemode/cl_search_models.lua
Arguments
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"