S&box Wiki

Revision Difference

Components#560523

<cat>Code.Game</cat>⤶ <title>Components</title>⤶ ⤶ # Components⤶ ⤶ Components are bits of functionality you can add to [GameObjects](https://wiki.facepunch.com/sbox/GameObjects). You can create a component to do anything you wish. There are also numerous components that exist in the s&box engine by default. You can find a list of example components within the [sbox-scenestaging repo](https://github.com/Facepunch/sbox-scenestaging/tree/main/code/ExampleComponents) on Facepunch's GitHub.⤶ ⤶ ## Properties⤶ An attribute `[Property]` can be added to a [property member](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties) on your component class, which allows you to slot in information about the property outside of code. This allows your component to be more re-usable.⤶ ⤶ ```cs⤶ public sealed class Health : Component⤶ {⤶ // MaxHealth can be changed within the editor now, but defaults to 100f.⤶ [Property] public float MaxHealth { get; set; } = 100f;⤶ public float CurrentHealth { get; set; }⤶ ⤶ protected override void OnStart()⤶ {⤶ // Set current health to max health when this component starts up⤶ CurrentHealth = MaxHealth;⤶ }⤶ }⤶ ```⤶