S&box Wiki

Revision Difference

Downloading_Assets_For_Your_Server#560907

<cat>Code.Misc</cat> <title>Mounting assets at runtime</title> #Mounting assets at runtime Sometimes you might need to download and mount certain resources from [asset.party](https://asset.party) at runtime. Maybe you're making a GMod style game with spawnable props, maybe you're procedurally generating something. ⤶ Sometimes you might need to download and mount certain resources from [asset.party](https://asset.party) at runtime. Maybe you're making a GMod style game with spawnable props, maybe you're procedurally generating something. ## Example method The Sandbox gamemode has a great method that allows you to download and mount models from [asset.party](https://asset.party). ```⤶ ⤶ The Sandbox gamemode has a great method that allows you to download and mount models from [asset.party](https://asset.party). ⤶ ```⤶ static async Task DownloadAsset( string packageName) { var package = await Package.Fetch( packageName, false ); if ( package == null || package.PackageType != Package.Type.Model || package.Revision == null ) { // spawn error particles return; } var model = package.GetMeta( "PrimaryAsset", "models/dev/error.vmdl" ); // downloads if not downloaded, mounts if not mounted await package.MountAsync(); Precache.Add( model ); } ``` ⤶ This would typically be placed in your Game.cs file, and called on the server realm within your constructor. ⤶ ⤶ ```⤶ public partial class MyGame : Sandbox.GameManager⤶ {⤶ public MyGame()⤶ {⤶ if ( Game.IsServer )⤶ {⤶ // The packageName should always match org.ident⤶ // In this case, we ask to download gvar.citizen_zombie -> https://asset.party/gvar/citizen_zombie⤶ DownloadAsset( "gvar.citizen_zombie" );⤶ }⤶ }⤶ }⤶ ```⤶ ⤶ ⤶ #Notes⤶ <note>The Precache.Add is still a point I'm unsure whether or not is useful. Documentation can be found here <https://wiki.facepunch.com/sbox/Precaching></note> ⤶ <note>The packageName must always match org.ident, meaning, if you wish to download https://asset.party/garry/before_pinetree then you should do:⤶ ```⤶ DownloadAsset( "garry.before_pinetree" )⤶ ```⤶ </note>