S&box Wiki

Revision Difference

Linking_Entities_to_Hammer#544012

<cat>Code.Game</cat> <title>Linking Entities to Hammer</title> # TLDR⤶ * Use `[HammerProp(name)]` for properties # 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" ] ``` ⤶ Our FGD will look something like this⤶ # 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:⤶ ⤶ ```csharp⤶ public partial SomeEntity : Entity { public string SomeProperty { get; set; } public bool SomeOtherProperty { get; set; } public void SomeInput() { } public void DoSomethingElse(string str) { } } ``` ⤶ It should look something like this⤶ # 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) { } } ``` 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⤶ ⤶ ```⤶ You can fire your output using `Entity.FireOutput(name, sender)` like this:⤶ ⤶ ```csharp⤶ public static void MakeItDoSomething(SomeEntity ent) { ent.FireOutput("OnSomething", null); } ```