S&box Wiki

Revision Difference

Downloading_Assets_For_Your_Server#547792

<cat>Code.Misc</cat> <title>Downloading assets for your server</title>⤶ <title>Mounting assets at runtime</title>⤶ #Downloading assets for future use on your server through code⤶ Sometimes, a developer may need to download and use resources from the [asset.party website](https://asset.party) The most common reasoning for doing so is using a model/asset within their code, without manually downloading it.⤶ #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. ## Example method The Sandbox gamemode has a great method that allows you to download and directly spawn an asset from the website, but we're doing not going to be spawning the model directly after downloading it, but rather using it later. Therefore, we need to do some modifications to it. 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 downloads, mounts if not mounted await package.MountAsync(); Precache.Add( model ); } ``` This would typically be placed in your Game.cs file, and called within your class constructor that initializes the game. This would typically be placed in your Game.cs file, and called on the server realm within your constructor. ``` public partial class frpGame : Game public partial class MyGame : Game { public frpGame() public MyGame() { if ( 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"); _ = new fRPHud();⤶ ⤶ } } ⤶ 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 downloads, mounts if not mounted⤶ await package.MountAsync();⤶ ⤶ // TBD on whether or not this is useful in the long run⤶ Precache.Add( model );⤶ }⤶ } ``` #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 would ``` DownloadAsset("garry.before_pinetree") ``` </note>