2019-05-22 20:56:04 +00:00
---
title: "Component"
date: 2019-05-22T12:51:29-07:00
weight: 5
---
A Component is a collection of related data.
2019-12-01 21:43:31 +00:00
To define a Component, declare a struct which implements the **IComponent** interface.
2019-05-22 20:56:04 +00:00
2019-12-01 21:43:31 +00:00
```cs
using Encompass;
using System.Numerics;
2019-05-22 20:56:04 +00:00
2019-12-01 21:43:31 +00:00
public struct VelocityComponent : IComponent {
public Vector2 velocity;
2019-05-22 20:56:04 +00:00
}
```
2019-12-01 21:43:31 +00:00
Components are attached to Entities with the **SetComponent** method.
2019-05-22 20:56:04 +00:00
2019-12-01 21:43:31 +00:00
```cs
using Encompass;
...
var worldBuilder = new WorldBuilder();
var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, new VelocityComponent { velocity = Vector2.Zero });
2019-05-22 20:56:04 +00:00
```
2019-12-01 21:43:31 +00:00
**SetComponent** can also be used from within an **Engine** . We will talk more about this later.
2020-03-02 08:22:23 +00:00
Components are always structs, meaning they follow value-type semantics. (Insert link about that here). If you use them idiomatically, you don't have to worry about them being garbage-collected.
2019-05-22 20:56:04 +00:00
{{% notice warning %}}
2019-12-01 21:43:31 +00:00
Components should **never** reference other Components directly. This breaks the principle of loose coupling. You **will** regret it if you do this.
2019-05-22 20:56:04 +00:00
{{% /notice %}}