Revision Difference
Linking_Entities_to_Hammer#547086
<cat>Code.Game</cat>
<title>Linking Entities to Hammer</title>⤶
⤶
Entities defined in your addon are imported through the Entity Tool. Entities are now loaded directly from the game assembly making [FGDs](https://developer.valvesoftware.com/wiki/FGD) obsolete.⤶
<title>Map Placeable Entities</title>⤶
⤶
<upload src="a5727/8da57932ba181af.png" size="414975" name="image.png" />⤶
⤶
Entities can be placed through the Entity Tool, as with everything else in s&box they are automatically hotloaded within your game and Hammer.⤶
⤶
If your game is [published on s&works](CreatingAddons) people can place entities for your game without even needing to download it.⤶
# Creating an entity
⤶
To link your entity to Hammer you need to add a `[Library]` attribute to your class, this will define the entity class name. The description is automatically set from the summary code comment. You'll also need to use `[HammerEntity]` to register it in the Hammer Entity Tool.
⤶
A basic example:⤶
⤶
Creating entities that can be placed in Hammer is as simple as marking them with `[HammerEntity]`.
```csharp
/// <summary>⤶
using SandboxEditor;⤶
⤶
/// <summary>⤶
/// This entity defines the spawn point of the player in first person shooter gamemodes.
/// </summary>
[HammerEntity]⤶
[Library( "info_player_start" )]
[Display( Name = "Player Spawnpoint" ), Category( "Player" ), Icon( "place" )]⤶
[Library( "info_player_start" ), HammerEntity]⤶
[Title( "Player Spawnpoint" ), Category( "Player" ), Icon( "place" )]
public class SpawnPoint : Entity
{
}
```
⤶
<warning>⤶
As of [Pain Day III](https://github.com/Facepunch/sbox-issues/discussions/1846), Hammer entities became opt-in instead of automatically being added to the entity tool. Make sure to use the correct attribute:⤶
⤶
```csharp⤶
[HammerEntity]⤶
```⤶
⤶
</warning>⤶
⤶
# Hammer Attributes⤶
⤶
All of these are optional attributes that can be added to your class to change the behaviour in Hammer.⤶
⤶
## [Solid]⤶
⤶
Tells Hammer this entity is brush based.⤶
⤶
```csharp⤶
[Solid]⤶
```⤶
⤶
## [SupportsSolid]⤶
⤶
Tells Hammer this point entity can also be a brush entity, such as `ent_door` entity.⤶
⤶
```csharp⤶
[SupportsSolid]⤶
```⤶
⤶
## [EditorModel]⤶
⤶
Tells Hammer what model to use for the entity and is added to the class.⤶
⤶
```csharp⤶
[EditorModel( "models/editor/playerstart.vmdl" )]⤶
```⤶
⤶
## [EditorSprite]⤶
⤶
Tells Hammer what sprite to use for the entity and is added to the class.⤶
⤶
```csharp⤶
[EditorSprite( "editor/info_target.vmat" )]⤶
```⤶
⤶
## [Hammer.EntityTool]⤶
⤶
Appends this Entity to Hammer's Entity tool list like so:⤶
⤶
<upload src="a5727/8d92f33819330cb.png" size="4947" name="image.png" />⤶
⤶
```csharp⤶
[Hammer.EntityTool( "Player Spawnpoint", "Player", "Defines a point where the player can (re)spawn" )]⤶
```⤶
⤶
## [Model]⤶
⤶
Makes it so the model, skin and bodygroups can be set and changed in Hammer.⤶
⤶
```csharp⤶
[Model]⤶
```⤶
⤶
⤶
## [AutoApplyMaterial]⤶
⤶
Automatically applies the supplied material to the tied brush.⤶
⤶
```csharp⤶
[AutoApplyMaterial( "materials/tools/toolstrigger.vmat" )]⤶
```⤶
⤶
# Hammer Properties⤶
⤶
# Properties⤶
⤶
<upload src="a5727/8d92f36d98ce869.png" size="11599" name="image.png" />⤶
Any property in your class can be exposed to Hammer using the `[Property]` attribute.
⤶
An example of a property being defined:⤶
```csharp
public partial class MyEntity : Entity
{
/// <summary>
/// Help text in hammer
/// </summary>
[Property( Title = "Start Disabled" ) ]
public bool StartDisabled { get; set; } = false;
}
```
⤶
Your entity properties will then show up in Hammer:⤶
⤶
<upload src="a5727/8d92f36d98ce869.png" size="11599" name="image.png" />⤶
# Inputs
Any method in your entity class can be exposed to Hammer as an input using the `[Input]` attribute - parameters you define are automatically handled.
An example of an input being defined:
```csharp
public partial class MyEntity : Entity
{
/// <summary>
/// Description of my input to be displayed in Hammer and in Visual Studio
/// </summary>
[Input]
public void DoSomethingElse(string str)
{
}
[Input( Name = "DoSomething" )]
public void SomeInput()
{
}
}
```
# Outputs
Any output in your entity class is automatically exposed to Hammer as an output.
An example of an output being defined:
```csharp
public partial class MyEntity : Entity
{
/// <summary>
/// Description of my output to be displayed in Hammer and in Visual Studio
/// </summary>
public Output OnSomethingHappened;
}
```
# Firing Outputs
You can fire your output using `Output.Fire(sender)` like this:
```csharp
protected Output OnEndTouch { get; set; }
public virtual void OnTouchEnd( Entity toucher )
{
OnEndTouch.Fire( toucher );
}
```⤶
⤶
⤶
# Hammer Attributes⤶
⤶
All of these are optional attributes that can be added to your class to change the behavior in Hammer.⤶
⤶
## [Solid]⤶
⤶
Tells Hammer this entity is brush based.⤶
⤶
```csharp⤶
[Solid]⤶
```⤶
⤶
## [SupportsSolid]⤶
⤶
Tells Hammer this point entity can also be a brush entity, such as `ent_door` entity.⤶
⤶
```csharp⤶
[SupportsSolid]⤶
```⤶
⤶
## [EditorModel]⤶
⤶
Tells Hammer what model to use for the entity and is added to the class.⤶
⤶
```csharp⤶
[EditorModel( "models/editor/playerstart.vmdl" )]⤶
```⤶
⤶
## [EditorSprite]⤶
⤶
Tells Hammer what sprite to use for the entity and is added to the class.⤶
⤶
```csharp⤶
[EditorSprite( "editor/info_target.vmat" )]⤶
```⤶
⤶
## [Model]⤶
⤶
Makes it so the model, skin and bodygroups can be set and changed in Hammer.⤶
⤶
```csharp⤶
[Model]⤶
```⤶
⤶
⤶
## [AutoApplyMaterial]⤶
⤶
Automatically applies the supplied material to the tied brush.⤶
⤶
```csharp⤶
[AutoApplyMaterial( "materials/tools/toolstrigger.vmat" )]⤶
```