S&box Wiki

Revision Difference

Linking_Entities_to_Hammer#544145

<cat>Code.Game</cat> <title>Linking Entities to Hammer</title> ⤶ # TL;DR⤶ ⤶ * Use `[HammerProp(name)]` for properties⤶ * Use `[Input(name)]` for inputs (on methods) * use `ent.FireOutput(name, sender)` for outputs⤶ ⤶ # Creating the FGD⤶ ⤶ First, create an [FGD](https://developer.valvesoftware.com/wiki/FGD) and put it into your addons `config` folder (or if you don't have one, make one in your addons root directory)⤶ ⤶ Our FGD will look something like this:⤶ ⤶ ```⤶ @PointClass = some_entity⤶ [⤶ some_property(string) : "Some Property"⤶ some_other_property(boolean) : "Some other Property"⤶ ⤶ // outputs⤶ output OnSomething(void) : "Fires when something happens"⤶ ⤶ // inputs⤶ input DoSomething(void) : "Does Something"⤶ input DoSomethingElse(string) : "Does something with parameters"⤶ ]⤶ ```⤶ ⤶ # Creating the entity in code⤶ ⤶ After you have created your FGD, you have to create your entity in the code.⤶ ⤶ Create a partial class and make it inherit from `Entity` or `ModelEntity` if you want it to have a model.⤶ ⤶ It should look something like this:⤶ ⤶ Entities defined in your addon will automatically generate an [FGD](https://developer.valvesoftware.com/wiki/FGD) and put it into your addons `config` folder.⤶ ⤶ ## Creating an entity⤶ ⤶ Create a partial class and make it inherit from <page text="Entity">Sandbox.Entity</page> - <page text="ModelEntity">Sandbox.ModelEntity</page>, <page text="AnimEntity">Sandbox.AnimEntity</page>, etc.. derive from this too. ⤶ To link this entity to Hammer you need to add a <page text="[Library] Attribute">Sandbox.LibraryAttribute</page> to your class, this will define the entity name and description.⤶ A basic example:⤶ ```csharp public partial SomeEntity : Entity⤶ [Library("info_player_spawn", Description = "It indicates the position and facing direction at which the player will spawn.")]⤶ public partial class SpawnPoint : Entity⤶ { public string SomeProperty { get; set; }⤶ ⤶ public bool SomeOtherProperty { get; set; }⤶ ⤶ public void SomeInput()⤶ {⤶ ⤶ }⤶ ⤶ public void DoSomethingElse(string str)⤶ {⤶ ⤶ }⤶ } ```⤶ ⤶ ## Optional class attributes⤶ ⤶ All of these are optional attributes that can be added to your class to change the behaviour in Hammer.⤶ ⤶ ### [Hammer.Solid]⤶ ⤶ <page text="[Hammer.Solid]">Hammer.SolidAttribute</page> tells Hammer this entity is brush based.⤶ ⤶ ```csharp⤶ [Hammer.Solid]⤶ ```⤶ ⤶ ### [Hammer.EditorModel]⤶ ⤶ <page text="[Hammer.EditorModel]">Hammer.EditorModelAttribute</page> tells Hammer what model to use for the entity and is added to the class.⤶ ⤶ ```csharp⤶ [Hammer.EditorModel( "models/editor/playerstart.vmdl" )]⤶ ```⤶ ⤶ ### [Hammer.EditorSprite]⤶ ⤶ <page text="[Hammer.EditorSprite]">Hammer.EditorSpriteAttribute</page> tells Hammer what model to use for the entity and is added to the class.⤶ ⤶ ```csharp⤶ [Hammer.EditorSprite( "editor/info_target.vmat" )]⤶ ```⤶ ⤶ ### [Hammer.EntityTool]⤶ ⤶ <page text="[Hammer.EntityTool]">Hammer.EntityToolAttribute</page> 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" )]⤶ ```⤶ ⤶ ### [Hammer.Model]⤶ ⤶ <page text="[Hammer.Model]">Hammer.ModelAttribute</page> makes it so the model, skin and bodygroups can be set and changed in Hammer.⤶ ⤶ ```csharp⤶ [Hammer.Model]⤶ ``` ⤶ # Linking your entity to the FGD⤶ ⤶ Add `[Library("name")]` to your class where `name` is the name of the class in hammer (ex: info_player_spawn, team_control_point) ⤶ Next, add `[HammerProp(name)]` to all of your properties where `name` is the name you gave the property in the FGD⤶ ⤶ After that, add `[Input(name)]` to every method you want to act as an input where `name` is the name you gave the input in the FGD again.⤶ <validate>Do inputs with parameters work as you would expect?</validate>⤶ ⤶ ```csharp⤶ [Library("some_entity")]⤶ public partial SomeEntity : Entity⤶ {⤶ [HammerProp("some_property"))]⤶ public string SomeProperty { get; set; }⤶ ⤶ [HammerProp("some_other_property")]⤶ public bool SomeOtherProperty { get; set; }⤶ ⤶ [Input("DoSomething")]⤶ public void SomeInput()⤶ {⤶ ⤶ }⤶ ⤶ [Input]⤶ public void DoSomethingElse(string str)⤶ {⤶ ⤶ }⤶ }⤶ ⤶ ### [Hammer.Skip]⤶ ⤶ <page text="[Hammer.Skip]">Hammer.SkipAttribute</page> can be used if you do not want your entity exposed in Hammer. ⤶ ```csharp⤶ [Hammer.Skip]⤶ ``` ⤶ A functioning entity which is linked to the FGD we made earlier⤶ ⤶ ⤶ # Firing Outputs⤶ ⤶ You can fire your output using `Entity.FireOutput(name, sender)` like this:⤶ ⤶ ## Exposing properties to Hammer⤶ ⤶ Any property in your class can be exposed to Hammer using the <page text="[Property] attribute">Sandbox.PropertyAttribute</page>.⤶ ⤶ An example of a property being defined:⤶ ```csharp public static void MakeItDoSomething(SomeEntity ent)⤶ public partial class MyEntity : Entity⤶ { ent.FireOutput("OnSomething", null);⤶ [Property( Name = "variable_name", Title = "Variable Title", Help = "Help text for variable" )]⤶ public string MyVariable { get; set; } = "Default Value";⤶ } ``` ⤶ Your entity properties will then show up in Hammer:⤶ ⤶ <upload src="a5727/8d92f36d98ce869.png" size="11599" name="image.png" />⤶ ⤶ Supported property types:⤶ ⤶ * int⤶ * float⤶ * bool⤶ * Vector3⤶ * string⤶ * Rotation⤶ * Angles⤶ * SoundEvent⤶ * Material