githubEdit

Use Lifecycle Hooks

Control when and how your component code runs

Learn how to use Needle Engine's lifecycle hooks to run code at the right time in your component's lifetime.

:::tip When to Use Lifecycle Hooks

  • Initialize components when they're created

  • Run code every frame for animation or gameplay

  • React to component being enabled/disabled

  • Clean up resources when components are destroyed

  • Synchronize logic with rendering :::


Quick Start

Basic component with common lifecycle hooks:

import { Behaviour } from "@needle-tools/engine";

export class MyComponent extends Behaviour {
    // Called when component is created
    awake() {
        console.log("Component created!");
    }

    // Called first frame after creation
    start() {
        console.log("Component started!");
    }

    // Called every frame
    update() {
        console.log("Frame update");
    }

    // Called when destroyed
    onDestroy() {
        console.log("Component destroyed");
    }
}

:::warning Performance Only implement lifecycle methods you actually need! Empty update() methods on many components hurt performance. :::


Lifecycle Methods Overview

Method
When Called
Common Uses

awake()

Component created

Cache references, initialize variables

onEnable()

Component enabled

Subscribe to events, start processes

start()

First frame

Setup that needs other components

earlyUpdate()

Before update

Input processing, pre-calculations

update()

Every frame

Main game logic, movement, animation

lateUpdate()

After update

Camera following, final adjustments

onBeforeRender()

Before render

Last-minute visual updates

onAfterRender()

After render

Post-processing, statistics

onDisable()

Component disabled

Unsubscribe events, pause processes

onDestroy()

Component destroyed

Clean up resources, remove references

Full API: Lifecycle Methods Referencearrow-up-right


Initialization: awake()

Called when the component is first created, before start().

Cache Component References

Initialize Variables


Start Logic: start()

Called on the first frame after awake(), after all components' awake() has been called.

Use Other Components

Start Coroutines


Every Frame: update()

Called every frame. Use for main game logic.

Movement

Input Handling

Rotation


Late Update: lateUpdate()

Called after all update() calls, before rendering. Perfect for cameras and final adjustments.

Camera Following

Billboard Effect


Enable/Disable: onEnable() & onDisable()

Called when component is enabled or disabled.

Event Subscriptions

Pause/Resume


Cleanup: onDestroy()

Called when component or GameObject is destroyed.

Resource Cleanup

Remove Global References


Execution Order

Understanding the order lifecycle methods are called:


Advanced: earlyUpdate()

Called before update(), useful for input processing:


Advanced: onBeforeRender() & onAfterRender()

Called just before/after rendering:

Update Shader Uniforms

Gather Statistics


Performance Best Practices

✅ DO

Cache component references in awake():

Use deltaTime for frame-rate independence:

Unsubscribe from events:

❌ DON'T

Don't search every frame:

Don't add empty methods:

Don't forget cleanup:


Complete Example: Health System

Putting it all together:


Next Steps

Learn more:

Reference:

Last updated