codeAPI Reference
Sandbox.GameResource
Sandbox.GameResource
Base class for custom asset types.
Overview
GameResource is the base class for creating custom asset types that appear in the editor with full hotloading support.
Creating a Custom Asset
CSHARP
[AssetType(Name = "Weapon Definition", Extension = "weapon", Category = "gameplay")]
public class WeaponDefinition : GameResource
{
public string DisplayName { get; set; }
public float Damage { get; set; }
public float FireRate { get; set; }
[ResourceType("vmdl")]
public string Model { get; set; }
[ResourceType("sound")]
public string FireSound { get; set; }
}AssetType Attribute
| Property | Description |
|---|---|
| Name | Display name in editor |
| Extension | File extension (max 8 chars, lowercase) |
| Category | Menu category |
PostLoad
CSHARP
public class WeaponDefinition : GameResource
{
[JsonIgnore]
public Model CachedModel { get; private set; }
protected override void PostLoad()
{
base.PostLoad();
if (!string.IsNullOrEmpty(Model))
{
CachedModel = Model.Load(Model);
}
}
}Loading
CSHARP
// By path
var weapon = ResourceLibrary.Get<WeaponDefinition>("weapons/assault_rifle.weapon");
// Safe loading
if (ResourceLibrary.TryGet<WeaponDefinition>(path, out var weapon))
{
// Use weapon
}
// Get all of type
var allWeapons = ResourceLibrary.GetAll<WeaponDefinition>();Properties
CSHARP
// GameResource provides:
string path = resource.ResourcePath;
string name = resource.ResourceName;Editor Features
- Appears in asset browser
- Draggable into component properties
- Hotloads on save
- JSON serialization
Inheritance
GameResources can inherit:
CSHARP
public class BaseItem : GameResource
{
public string Name { get; set; }
public int Cost { get; set; }
}
[AssetType(Name = "Weapon", Extension = "wpn")]
public class WeaponItem : BaseItem
{
public float Damage { get; set; }
}
Was this helpful?