How to pre-cache a SpawnIcon?

Hi,
I’m coding some UI that uses SpawnIcons, but whenever I run it for the first time the icons have to be rendered. Here’s an example of what I’m talking about:
20210123092740_1

(Note: My particular problem does not pertain to the SpawnMenu).

So I was wondering if there was any way I could just pre-cache these icons in the very beginning. I’ve looked at util.PrecacheModel and Panel:SpawnIconRebuild() but I’m not sure if they’re what I’m after. Any help or explanation on the subject would be much appreciated.

Thank you.

Fixed

Ended up doing what TankNut said, just pre-drew all the icons as DSpawnIcon in the top left corner of the screen before I’d actually be using them. I used Panel:SetAlpha( 0 ) so that you don’t notice them being rendered.

Since the limit of the cache is 4096 models (see util.PrecacheModel - Garry's Mod Wiki), I don’t advise you to precache all of them.

@Florian4016 is right

-- non-cheat developer commands can reveal precaching the first time equipment
-- is bought, so trigger it at the start of a round instead
function WEPS.ForcePrecache()
   for k, w in ipairs(weapons.GetList()) do
      if w.WorldModel then
         util.PrecacheModel(w.WorldModel)
      end
      if w.ViewModel then
         util.PrecacheModel(w.ViewModel)
      end
   end
end

Have a look at how it’s done in the TTT for the weapon shop (Here’s the full code)

I try this, unfortunately my problem still persists. I’ve even got a printout to prove that it’s caching them, and checked using util.IsValidModel()
I’m doing all of this from shared.lua if that matters

[!] Note: If it matters, my panel creates these icons at an irregular size (i.e. not default gmod 64x64) - Possible CLUE?

It shows this on serverside and clientside:

faction detected
precaching: models/adi/farmeros/male_01.mdl
precaching: models/adi/farmeros/male_02.mdl
precaching: models/adi/farmeros/male_03.mdl
precaching: models/adi/farmeros/male_04.mdl
precaching: models/adi/farmeros/male_05.mdl
precaching: models/adi/farmeros/male_06.mdl
precaching: models/adi/farmeros/male_07.mdl
precaching: models/adi/farmeros/male_08.mdl
precaching: models/adi/farmeros/male_09.mdl
precaching: models/adi/farmeros/male_10.mdl

Here’s me checking on both:

  lua_run print( util.IsValidModel( "models/adi/farmeros/male_01.mdl" ) )
  > print( util.IsValidModel( "models/adi/farmeros/male_01.mdl" ) )...
  true
  lua_run_cl print( util.IsValidModel( "models/adi/farmeros/male_01.mdl" ) )
  true

Precaching the model doesn’t have anything to do with spawnicons, if you want to ‘pre-render’ your icons you’ll have to create the actual spawnicon panels beforehand with both the correct size and model.

I think it’s this function right here:

Fixed

Ended up doing what TankNut said, just pre-drew all the icons as DSpawnIcon in the top left corner of the screen before I’d actually be using them. I used Panel:SetAlpha( 0 ) so that you don’t notice them being rendered.

1 Like