Garry's Mod Wiki

Revision Difference

util.PrecacheModel#568379

<function name="PrecacheModel" parent="util" type="libraryfunc"> <description> Precaches a model for later use. Model is cached after being loaded once. Precaches a model for later use. The model is cached after being loaded once. <warning> Modelprecache is limited to 8192 unique models. When it reaches the limit the game will crash. Model pre-caching is limited to 8192 unique models. When it reaches the limit, the game will crash. </warning> <note>Does nothing on the client.</note> </description> <realm>Shared</realm> <args> <arg name="modelName" type="string">The model to precache.</arg> </args> </function> <example> <description>If you have to deal with many models at a time, you'd better reduce the calls to that function. Too many calls will cause the game to run out of memory; e.g., too much precaching is a pre-crash. Use deep validation when you are about to spawn the model, and file validation on the client for drawing stuff. It is slow, so it's better to hash that as well.</description> <code> -- Uses a custom model check to remove the pre-caching overhead local tModels = {} tModels.Skip = {} -- General disabled models for spawning tModels.Skip[""] = true -- Empty string tModels.Skip["models/error.mdl"] = true tModels.File = {} -- When the file is available tModels.Deep = {} -- When the model is spawned -- This is the function that will validate the models and fill the info in the `tModels` table function IsModel(sModel, bDeep) if(not isstring(sModel)) then return false end if(tModels.Skip[sModel]) then return false end local vDeep = tModels.Deep[sModel] -- Read model validation status if(SERVER and bDeep and vDeep ~= nil) then return vDeep end -- Will spawn local vFile = tModels.File[sModel] -- File current status if(vFile ~= nil) then -- File validation status is present if(not vFile) then -- File is validated as invalid path print("Invalid file "..sModel); return vFile end else -- File validation status update. Status is missing. Calculate. vFile = false; tModels.File[sModel] = vFile -- Assume being invalid if(IsUselessModel(sModel)) then --Check model being Aqua from Konosuba print("File useless "..sModel); return vFile end if(not file.Exists(sModel, "GAME")) then -- Check model being a unicorn print("File missing "..sModel); return vFile end vFile = true; tModels.File[sModel] = vFile -- The file validated print("File >> "..table.concat({tostring(vDeep), tostring(vFile), sModel}, "|")) print("File >> ", vDeep, vFile, sModel) end -- At this point the file path is valid. Have to validate the model if(CLIENT or not bDeep) then return vFile end -- File is validated util.PrecacheModel(sModel); vDeep = util.IsValidModel(sModel) tModels.Deep[sModel] = vDeep -- Store deep validation print("Deep >> "..table.concat({tostring(vDeep), tostring(vFile), sModel}, "|")) print("Deep >> ", vDeep, vFile, sModel) return vDeep -- Gonna spawn on the server. Must validate. end </code> </example>