Garry's Mod Wiki

Revision Difference

util.PrecacheModel#567951

<function name="PrecacheModel" parent="util" type="libraryfunc"> <description> Precaches a model for later use. Model is cached after being loaded once. <warning> Modelprecache 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 libModel = {}⤶ libModel.Skip = {} -- General disabled models for spawning⤶ libModel.Skip[""] = true -- Empty string⤶ libModel.Skip["models/error.mdl"] = true⤶ libModel.File = {} -- When the file is available⤶ libModel.Deep = {} -- When the model is spawned⤶ function IsModel(sModel, bDeep)⤶ if(not isstring(sModel)) then return false end⤶ if(libModel.Skip[sModel]) then return false end⤶ local vDeep = libModel.Deep[sModel] -- Read model validation status⤶ if(SERVER and bDeep and vDeep ~= nil) then return vDeep end -- Will spawn⤶ local vFile = libModel.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; libModel.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; libModel.File[sModel] = vFile -- The file validated⤶ print("File >> "..table.concat({tostring(vDeep), tostring(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)⤶ libModel.Deep[sModel] = vDeep -- Store deep validation⤶ print("Deep >> "..table.concat({tostring(vDeep), tostring(vFile), sModel}, "|"))⤶ return vDeep -- Gonna spawn on the server. Must validate.⤶ end⤶ </code>⤶ ⤶ </example>⤶