S&box Wiki

Revision Difference

Property#560409

<cat>Code.Misc</cat>⤶ <title>Attributes and Component Properties</title>⤶ ⤶ # Attributes⤶ Attributes in C#, generally, are metadata added to code elements such as classes, methods, or properties to assign additional semantic information that can be accessed at runtime or compile time.⤶ ⤶ In S&box, we have a few attributes that do special things in the Scene Editor.⤶ ⤶ Firstly, if you put `[Icon]`, `[Category]`, and `[Title]` before defining your Component class, you can give your Component an icon, category, and custom title!⤶ ⤶ The `[Title]` Attribute is primarily useful if the title of your component ought to be different than the auto-set title based on the .cs filename.⤶ ⤶ An example of this (from the default Box Collider component) would be:⤶ ⤶ ```csharp⤶ [Title("Collider - Box")]⤶ [Category("Physics")]⤶ [Icon("check_box_outline_blank")]⤶ // (etc...)⤶ public sealed class BoxCollider : Collider⤶ ```⤶ ⤶ You can [find a list of Icons on Google's Material Design Icons github.](https://github.com/google/material-design-icons/blob/master/font/MaterialIcons-Regular.codepoints).⤶ ⤶ # Component Properties and you⤶ ⤶ When you're creating a new Component, you may want to create variables or other objects that⤶ are exposed in the Inspector so they can be changed manually in the editor (while editing or while playing), or so you can refer to specific GameObjects or Components in the scene without getting them through code.⤶ ⤶ You can do this by adding `[Property]` before defining the object. You can also add other Attributes in addition to Property, to augment the Property in the editor.⤶ ⤶ For example, take a look at this Property from the CitizenAnimationHelper component:⤶ ```csharp⤶ [Property, Range( 0.5f, 1.5f ), Title( "Avatar Height Scale" )] public float Height { get; set; } = 1.0f;⤶ ```⤶ ⤶ This creates a new public float variable called Height with an auto-implemented Getter and Setter⤶ (properties need to have a Getter and Setter or they won't work) that will display in the editor with a⤶ slider that can be set between 0.5 and 1.5, with the title 'Avatar Height Scale'.⤶ <upload src="3b92d/8dc2a07c3e32451.png" size="1632" name="image.png" />⤶ ⤶ You can then drag those components into the inspector and your code will use the specific instance of that dragged-in component when executing the code that references that property!⤶ ⤶ Properties don't just have to be things like integers and floats, they can also be any class, such as GameObject or CharacterController or your own self-defined public classes. See the end of the article for some more examples.⤶ ⤶ # Reference⤶ ⤶ ## Range⤶ As previously discussed, you can set valid ranges for Properties which will give them a slider instead of a text entry.⤶ The precision of the slider is based on what kind of variable you set-- an integer will go increments of one, a float will have decimals.⤶ ```csharp⤶ [Property, Range( 0, 6 ), Title( "How Many Apples" )] public int Apples { get; set; } = 6;⤶ ```⤶ ⤶ ## Group⤶ Groups of properties, to make the inspector look cleaner.⤶ ```csharp⤶ [Property, Group( "Group One" ), Title( "First Thing" )] public GameObject MyFirstThing { get; set; }⤶ [Property, Group( "Group One" ), Title( "Second Thing" )] public GameObject MySecondThing { get; set; }⤶ ```⤶ ⤶ ## ToggleGroup⤶ Groups with a bool that you can toggle on/off in the inspector.⤶ To be clear- you are toggling on/off the bool, you need to do something within the code to make it respect the toggling, or the bool will just be turning on and off.⤶ ⤶ ```csharp⤶ [Property, ToggleGroup( "GroupTwoToggle", Label = "Group Two (toggleable)" )]⤶ public bool GroupTwoToggle { get; set; } = false;⤶ [Property, ToggleGroup( "GroupTwoToggle" )] public GameObject CanBeToggled { get; set; }⤶ [Property, ToggleGroup( "GroupTwoToggle" ), Title( "Third Thing" )] public GameObject MyThirdThing { get; set; }⤶ [Property, ToggleGroup( "GroupTwoToggle" ), Title( "Fourth Thing" )] public GameObject MyFourthThing { get; set; }⤶ ```⤶ ⤶ ## Enums⤶ ⤶ Enums are special, each underlying const can have `[Icon]` and `[Description]` and `[Title]` attributes when being defined as an enum, and then when you make the Property refer to the Enum type you created with those definitions, it will show up in the editor as a selector between your different enums with the fancy descriptions and titles and such for each one. Nice!⤶ ⤶ ```csharp⤶ [Property] public HygieneTypes HygieneSelection;⤶ public enum HygieneTypes⤶ {⤶ [Icon( "shower" )]⤶ [Title("Take Shower")]⤶ [Description( "Take a shower" )]⤶ TakeShower,⤶ [Icon( "bathtub" )]⤶ [Title("Bathe")]⤶ [Description( "Take a bath" )]⤶ TakeBath,⤶ [Icon( "bed" )]⤶ [Title( "Back to Bed" )]⤶ [Description( "Go back to sleep instead." )]⤶ BackToSleep,⤶ }⤶ ```⤶ ⤶ Result:⤶ <upload src="3b92d/8dc2a0e539e7eda.png" size="12749" name="image.png" />⤶ ⤶ ## Lists⤶ ⤶ Lists can be properties just like any other variable, and they work like you'd expect.⤶ ```csharp⤶ [Property] public List<int> ListPropertyTest { get; set; } = new List<int>();⤶ ```⤶ <upload src="3b92d/8dc2a1079c9f87a.png" size="4192" name="image.png" />⤶ ⤶ ## Other objects that can be used with [Property] in components⤶ ⤶ Pretty much anything you'd expect to work can be used as a Property in a Component.⤶ Here are some other examples:⤶ ⤶ ```csharp⤶ [Property] public Vector2 MyVector2 { get; set; } = new Vector2 { };⤶ [Property] public Vector3 MyVector3 { get; set; } = new Vector3 { };⤶ [Property] public Vector4 MyVector4 { get; set; } = new Vector4 { };⤶ [Property] public Color MyColor { get; set; } = new Color();⤶ [Property] public Rotation MyRotation { get; set; } = new Rotation();⤶ [Property] public AudioListener MyAudioListener { get; set; } = new AudioListener();⤶ ```⤶ ⤶ <upload src="3b92d/8dc2a11fd31f189.png" size="11472" name="image.png" />