S&box Wiki

Revision Difference

Downloading_Assets_For_Your_Server#547789

<cat>Code.Misc</cat> <title>Downloading assets for your server</title> #Downloading assets for future use on your server through code Sometimes, a developer may need to download and use resources from the [assert party website](https://asset.party) The most common reasoning for doing so is using a model/asset within their code, without manually downloading it. ## 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. ``` 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. ``` public partial class frpGame : Game { public frpGame() { 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 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> The packageName must always match org.ident, meaning, if you wish to download https://asset.party/garry/before_pinetree then you would ⤶ 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") ```