Garry's Mod Wiki

Revision Difference

search.AddProvider#552028

<function name="AddProvider" parent="search" type="libraryfunc"> <description>Adds a search result provider. For examples, see 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. It has one argument: <page>string</page> searchQuery 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?</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>