S&box Wiki

Entity Components

Entity Components

Entities can have components. These components are networked, so are also available on the client.

Defining Components

public partial class MyComponent : EntityComponent { [Net] public string MyVariable { get; set; } }

Components can also have Events.

public partial class FlyingComponent : EntityComponent { [Event.Tick] public void OnTick() { Entity.Velocity += Vector3.Up * 16.0f; } }

Creating Components

You can create, get and remove components from an Entity at any time, in this example we simply assign them on Spawn.

public partial class MyEntity : Entity { public override Spawn() { var c = Components.Create<MyComponent>(); var c = Components.GetOrCreate<MyComponent>(); var c = Components.Get<MyComponent>(); var c = new MyComponent(); Components.Add( c ); Components.Remove( c ); Components.RemoveAll(); foreach ( var c in Components.GetAll<MyComponent>() ) { } foreach ( var c in Components.GetAll<EntityComponent>() ) { } } }

Footnotes

  • Clients are also entities and can have components on Client.Components

Special Pages


Wikis

?

Render Time: 45ms

DB GetPage 33
Generate Html 1
SaveChanges (1) 7
Render Body 0
Render Sidebar 1