Search Results for

    Show / Hide Table of Contents

    Bind Components

    Using [BindComponent] attribute allows your Components to stablish dependencies with another components.

    // Bind with the Transform component of the owner entity...
    [BindComponent]
    private Transform3D transform;
    
    // Bind a list with all Camera3D components of the entire scene...
    [BindComponent(source: BindComponentSource.Scene)]
    private List<Camera3D> sceneCameras;
    
    Note

    [BindComponent] can only be used inside Components. In other case the binding cannot be resolved

    [BindComponent] Properties

    This attribute offers several ways to customize:

    isRequired (default true)

    If the value is true the dependency is required to be resolved, in the oher case, the current Component won't be attached.

    For insatnce, in the following example we have a custom component MyComponent, and a definition of an Entity with two components (a Transform3D and MyComponent)

    public class MyComponent : Component
    {
        [BindComponent(isRequired: true)] 
        private Transform3D transform;
    
        [BindComponent(isRequired: false)] 
        private Camera3D camera;
    }
    
    Entity entity = new Entity()
        .AddComponent(new Transform3D())
        .AddComponent(new MyComponent());
    

    The MyComponent will be attached correctly, because all requirements has been satisfied:

    • The required Transform3D will be injected into the transform attribute, because we previously added a Transform3D component.
    • The camera attribute won't be resolved, and this value will be equal to null, however, the component would be attached because this dependency is not required.

    In the other hand, in this other Entity, now the MyComponent instance won't be attached because the Transform3D dependency cannot be resolved:

    Entity anotherEntity = new Entity()    
        .AddComponent(new MyComponent());
    

    isExactType (default true)

    If the value is true indicates that the Type of the component to bound must be the same as the type required (nor subclass or parent class).

    For insatnce, the following component MyComponent, requires a component of the exact type Camera

    public class MyComponent : Component
    {
        [BindComponent] // isExactType: true by default
        private Camera camera;
    }
    
    Entity entity = new Entity()
        .AddComponent(new Transform3D())
        .AddComponent(new Camera3D())
        .AddComponent(new MyComponent());
    

    In that case, the dependency won't be injected, because the entity has no Camera component (it has a Camera3D component, which is a subclass, but isExactType is true)

    However, if we change the MyComponent definition and sets isExactType value to false, now the dependency will be satisfied, because the entity has a component asignable to a Camera type (the Camera3D component):

    public class MyComponent : Component
    {
        [BindComponent(isExactType: false)]
        private Camera camera;
    }
    

    source (default BindComponentSource.Owner)

    This property indicates where the component or components will be searched. There are several values:

    Source Description
    Owner (default) The Component will be searched in the owner entity.
    Scene The Component is searched in the entire Scene. It iterates over all entities in the scene to find if exist components.
    Children Search the Components in its descendant entities, including the owner entity
    ChildrenSkipOwner Search the Components in its descendant entities, not including the owner entity
    Parents Search the Components in the ascendant entities, including the owner entity
    ParentsSkipOwner Search the Components in its ascendant entities, not including the owner entity

    A brief example:

    public class MyComponent : Component
    {
        // Bind a the first Camera3D component in the scene...
        [BindComponent(source: BindComponentSource.Scene)]
        private Camera3D firstCamera;
    
        // Bind a list with all Camera3D components in the entire scene...
        [BindComponent(source: BindComponentSource.Scene)]
        private List<Camera3D> sceneCameras;
    
        // ...
    }
    

    isRecursive (default true)

    If set to true the search will include all descendants (or ascendants) in the hierarchy; otherwise, the search will only include the direct descendants.

    tag (default null)

    If the tag has value, it will only find components in entities that has the specified Tag. It works for filtering entities.

    public class MyComponent : Component
    {
        // Bind a list with all Camera3D components of all Entities tagged with "Tag"
        [BindComponent(source: BindComponentSource.Scene, tag: "Tag")]
        private List<Camera3D> sceneCameras;
    
        // ...
    }
    
    In This Article
    Back to top
    Generated by DocFX