S&box Wiki

Mounting assets at runtime

Mounting assets at runtime

Sometimes you might need to download and mount certain resources from 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.

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 : Game { 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"); } } }

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

DownloadAsset("garry.before_pinetree")