Component

Base class for all components. Components add functionality to GameObjects.

using Xengine.Scene;

Creating Components

Create a component by extending the Component base class:

public class MyComponent : Component
{
    protected override void OnStart()
    {
        Log.Info("Component started!");
    }
    
    protected override void OnUpdate()
    {
        // Called every frame
    }
}

Lifecycle Methods

Override these methods to add behavior:

MethodWhen Called
OnAwake()Once when component is first created
OnStart()Once before the first OnUpdate
OnEnable()When component becomes active
OnDisable()When component becomes inactive
OnUpdate()Every frame
OnFixedUpdate()Fixed timestep (for physics)
OnLateUpdate()After all OnUpdate calls
OnRender(RaylibRenderer)When rendering the scene
OnDestroy()When component is destroyed

Example: Full Lifecycle

public class LifecycleDemo : Component
{
    protected override void OnAwake()
    {
        // Initialize fields, called once
    }
    
    protected override void OnStart()
    {
        // Setup that needs other components
        var renderer = GetComponent<CubeRenderer>();
    }
    
    protected override void OnUpdate()
    {
        // Game logic, input handling
        if (RaylibInput.IsKeyPressed(KeyCode.Space))
            Jump();
    }
    
    protected override void OnFixedUpdate()
    {
        // Physics calculations
        velocity += gravity * Time.FixedDelta;
    }
    
    protected override void OnLateUpdate()
    {
        // Camera follow, UI updates
    }
    
    protected override void OnRender(RaylibRenderer r)
    {
        // Draw visuals
        r.DrawCube(Transform.Position, Vector3.One, Color.Red);
    }
    
    protected override void OnDestroy()
    {
        // Cleanup
    }
}

Properties

GameObject

GameObject? GameObject { get; }

The GameObject this component is attached to.

Transform

GameTransform? Transform { get; }

Shortcut to GameObject.Transform.

Transform.Position += Vector3.Forward * Time.Delta;

Scene

Scene? Scene { get; }

The scene containing this component's GameObject.

Enabled

bool Enabled { get; set; }

Enable/disable the component. Disabled components don't receive updates.

// Disable component
myComponent.Enabled = false;

// Check if active
if (component.Active) { ... }

Active

bool Active { get; }

True if component is enabled AND GameObject is active.

Methods

GetComponent<T>

T? GetComponent<T>()

Get another component on the same GameObject.

var renderer = GetComponent<CubeRenderer>();
if (renderer != null)
    renderer.Color = Color.Red;

GetComponents<T>

IEnumerable<T> GetComponents<T>()

Get all components of type T on the same GameObject.

AddComponent<T>

T AddComponent<T>()

Add a new component to the same GameObject.

var audio = AddComponent<AudioSourceComponent>();
audio.Play();

GetComponentInChildren<T>

T? GetComponentInChildren<T>()

Find component in this object or any children.

GetComponentInParent<T>

T? GetComponentInParent<T>()

Find component in this object or any parents.

Example Components

Simple Movement

public class PlayerMovement : Component
{
    public float Speed = 5f;
    
    protected override void OnUpdate()
    {
        var move = Vector3.Zero;
        
        if (RaylibInput.IsKeyDown(KeyCode.W)) move.Z += 1;
        if (RaylibInput.IsKeyDown(KeyCode.S)) move.Z -= 1;
        if (RaylibInput.IsKeyDown(KeyCode.A)) move.X -= 1;
        if (RaylibInput.IsKeyDown(KeyCode.D)) move.X += 1;
        
        if (move.LengthSquared > 0)
        {
            move = move.Normalized * Speed * Time.Delta;
            Transform.Position += move;
        }
    }
}

Rotating Object

public class Rotator : Component
{
    public float DegreesPerSecond = 90f;
    
    protected override void OnUpdate()
    {
        Transform.Rotate(new Vector3(0, DegreesPerSecond * Time.Delta, 0));
    }
}

Health System

public class Health : Component
{
    public int MaxHealth = 100;
    public int CurrentHealth { get; private set; }
    
    protected override void OnStart()
    {
        CurrentHealth = MaxHealth;
    }
    
    public void TakeDamage(int amount)
    {
        CurrentHealth -= amount;
        if (CurrentHealth <= 0)
        {
            CurrentHealth = 0;
            GameObject.Destroy();
        }
    }
    
    public void Heal(int amount)
    {
        CurrentHealth = System.Math.Min(CurrentHealth + amount, MaxHealth);
    }
}