S&box Wiki

Revision Difference

Precaching#560517

<cat>Code.Misc</cat>⤶ <title>Precaching</title>⤶ ⤶ <note>Precaching will probably change in the future with more stuff being automatically handled so you don't have to care as much.</note>⤶ ⤶ Precaching loads all the required assets for your game before the player loads in. By precaching properly you can avoid the engine trying to load assets in the middle of the game which causes stuttering.⤶ ⤶ Ideally everything in your game should be precached automatically for you, but in some cases that might not be possible right now.⤶ ⤶ # Automatic Precaching⤶ ⤶ Whenever a resource is used on the server e.g a model is set, material loaded, sound played, it is added to the precache list. This works well for anything spawned in by the map, but anything after that with a player joined it will cause a cache miss and need to be loaded on the players.⤶ ⤶ A good way to make sure resources get precached is to use their Load method and store a static reference e.g:⤶ ⤶ ```csharp⤶ public partial class Ball : ModelEntity⤶ {⤶ static readonly Model BallModel = Model.Load( "models/golf_ball.vmdl" );⤶ ⤶ public override void Spawn()⤶ {⤶ Model = BallModel;⤶ }⤶ }⤶ ```⤶ ⤶ If you are using a Material or Texture, you should approach it the same way.⤶ ⤶ # Manual Precaching⤶ ⤶ You can manually precache with Precache.Add( string ), currently this should be done for sounds and particles.⤶ Do this in your Game constructor or something similar serverside.⤶ ⤶ ```csharp⤶ Precache.Add( "particles/gameplay/ball_circle/ball_circle.vpcf" );⤶ Precache.Add( "sounds/minigolf.sink_into_cup.vsnd" );⤶ ```⤶