Revision Difference
search.AddProvider#561762
<function name="AddProvider" parent="search" type="libraryfunc">
<description>Adds a search result provider. For examples, see gamemodes/sandbox/gamemode/cl_search_models.lua</description>⤶
<description>Adds a search result provider. For examples, see [gamemodes/sandbox/gamemode/cl_search_models.lua](https://github.com/Facepunch/garrysmod/blob/7c23addd2c35d3d046c80e3d0cb6052055eca3e2/garrysmod/gamemodes/sandbox/gamemode/cl_search_models.lua)</description>⤶
<realm>Client</realm>
<file line="6-L18">lua/includes/modules/search.lua</file>
<args>
<arg name="provider" type="function">Provider function.
<callback>
<arg type="string" name="searchQuery">The search query user has inputed.</arg>
<ret type="table" name="data">
You must return a list of tables structured like this:
* <page>string</page> text - Text to "Copy to clipboard"
* <page>function</page> func - Function to use/spawn the item
* <page>Panel</page> icon - A panel to add to spawnmenu
* <page>table</page> words - A table of words?
</ret>
</callback>
</arg>
<arg name="id" type="string" default="nil">If provided, ensures that only one provider exists with the given ID at a time.</arg>
</args>
</function>
<example>
<description>Model search from gamemodes/sandbox/gamemode/cl_search_models.lua with added comments</description>
<code>
--
-- 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"
</code>
</example>