S&box Wiki

Revision Difference

Entity_Prefabs#560144

<cat>Code.Entity</cat>⤶ <title>Entity Prefabs</title>⤶ ⤶ 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..⤶ <upload src="ae247/8db4dd0a802412c.png" size="71284" name="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.⤶ <upload src="ae247/8db4dd11ff8639c.png" size="564139" name="image.png" />⤶ ⤶ # Spawning the Entity Prefab⤶ You can spawn a prefab through code using `PrefabLibrary`:⤶ ```csharp⤶ 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.⤶ ```csharp⤶ [Prefab]⤶ public partial class Weapon : AnimatedEntity⤶ {⤶ [Prefab, Net]⤶ public int MaxAmmo { get; set; } = 8;⤶ }⤶ ```⤶ <note>Currently, prefabs do not get deserialized on the client, so you need to mark your properties with [Net].</note><cat>removed</cat>