Table of Contents

C#: Component Model

A ComponentModel handles any component level behaviour that can be enabled or disabled on supported components. To be able to fetch a component model from a Component, you need to implement the IsModelTypeSupported function. Only 1 component model of each type can exist per component.


How do I create a Component Model?

When you create a new C# class, make sure you inherit from NominalSystems.Core.ComponentModel.

using NominalSystems.Core

public class MyComponentModel : ComponentModel
{
    // ...Your code goes here...
}

How do I add/get a ComponentModel?

The ComponentModel will be created ONCE when it is requested by the parent Component. You can use the GetModel{T} function to get the desired ComponentModel. Make sure your parent class inherits from Component and that you override the IsModelTypeSupported function

using NominalSystems.Core

public class AnotherClassInTheSimulation : Component
{
    ...
    // This is a reference to `MyComponentModel` wrapped as a property
    protected MyComponentModel => GetSystem<MyComponentModel>();

    // We override this function to allow `MyComponentModel` to be created
    protected bool IsModelTypeSupported(Type type)
    {
				// check for valid 'MyComponentModel` type
        if (type == typeof(MyComponentModel)) return true;

        // otherwise, do not create any other model type
        return false
    }
    ...
}