S&box Wiki

Entity Prefabs

An Entity Prefab is a definition of an entity that can be created outside of the game, and then spawned through code.

Creating an Entity Prefab

To set up a definition for an Entity Prefab, right click in the Asset Browser, and navigate to New Prefab..

image.png

Once the Prefab is created, you can configure it to your liking by filling out fields and using the gizmos to adjust the entities to your liking. You can add other child entities and components to the prefab using the entity tree on the left.

image.png

Spawning the Entity Prefab

You can spawn a prefab through code using PrefabLibrary:

if ( PrefabLibrary.TrySpawn<Entity>( "grenade.prefab", out var grenade ) ) { grenade.Position = tr.HitPosition; grenade.Rotation = Rotation.LookAt( tr.Normal ); }

Setting up your own Entity Prefab Types

You can use the [Prefab] attribute to allow your Entity and EntityComponent types to be used in prefabs.

[Prefab] public partial class Weapon : AnimatedEntity { [Prefab, Net] public int MaxAmmo { get; set; } = 8; }
Currently, prefabs do not get deserialized on the client, so you need to mark your properties with [Net].