O3DE Script Canvas & Lua Scripting

📜 O3DE Script Canvas & Lua Scripting

O3DE provides three ways to write gameplay logic, each targeting a different complexity tier:

Method Best For Performance Iteration Speed
Script Canvas Designers, prototyping, simple logic Slowest Fastest (visual)
Lua Gameplay programmers, medium complexity Medium Fast (text, no recompile)
C++ Systems programming, performance-critical Fastest Slow (full rebuild)

🎨 Script Canvas — Visual Scripting

Script Canvas is O3DE's node-based visual scripting system — conceptually similar to Unreal's Blueprints but with a different node vocabulary.

Getting Started

  1. Add the ScriptCanvas Gem to your project
  2. Create a new Script Canvas asset: Asset Browser → Right-click → Create → Script Canvas
  3. Attach it to an entity via the Script Canvas component in the Entity Inspector

Node Categories

Category Examples
Events On Graph Start, On Entity Activated, On Tick
Logic If/Else, While Loop, For Each, Switch
Math Add, Multiply, Lerp, Clamp, Vector3 ops
Entity Get Entity Transform, Find Entity by Tag, Spawn Prefab
EBus Any EBus request/notification exposed to Script Canvas
Variables Get Variable, Set Variable, local/entity-scoped
Timing Delay, Timer, Heartbeat
Debug Print, Draw Debug Line/Sphere

Script Canvas Example: Rotating an Entity

[On Tick] → deltaTime
    │
    ├─→ [Multiply] (deltaTime × 90.0) → rotationSpeed
    │
    └─→ [Rotate Entity] (Self, Y-axis, rotationSpeed)
Tip

Script Canvas graphs compile to Lua under the hood. They're great for rapid prototyping, but for anything involving complex state machines or performance-sensitive loops, drop down to Lua or C++.


🌙 Lua Scripting

Lua scripting in O3DE gives you full text-based control without recompiling the engine. O3DE uses Lua 5.4 with automatic bindings to the EBus system.

Setting Up a Lua Script

  1. Add the LuaIDE Gem (optional — gives you an in-editor Lua debugger)
  2. Create a .lua file in your project's Assets/Scripts/ directory
  3. Attach a Lua Script component to an entity and assign the script

Lua Component Lifecycle

-- PlayerController.lua

local PlayerController = {
    Properties = {
        MoveSpeed = { default = 5.0, description = "Movement speed (m/s)" },
        LookSensitivity = { default = 0.3, description = "Mouse look sensitivity" },
        JumpForce = { default = 8.0, description = "Jump impulse force" },
    }
}

function PlayerController:OnActivate()
    -- Called when the entity enters the world
    self.tickHandler = TickBus.Connect(self)
    self.inputHandler = InputEventNotificationBus.Connect(self, InputEventNotificationId("move_forward"))

    -- Cache the entity ID for EBus calls
    self.entityId = self.Properties.entityId or self.entityId
    
    Debug.Log("PlayerController activated on entity: " .. tostring(self.entityId))
end

function PlayerController:OnDeactivate()
    -- Called when the entity leaves the world
    if self.tickHandler then
        self.tickHandler:Disconnect()
    end
    if self.inputHandler then
        self.inputHandler:Disconnect()
    end
end

function PlayerController:OnTick(deltaTime, scriptTime)
    -- Called every frame (like _process in Godot or Tick in Unreal)
    self:UpdateMovement(deltaTime)
    self:UpdateCamera(deltaTime)
end

function PlayerController:UpdateMovement(dt)
    local forward = TransformBus.Event.GetWorldTM(self.entityId):GetBasisY()
    local right = TransformBus.Event.GetWorldTM(self.entityId):GetBasisX()

    local moveDir = Vector3(0, 0, 0)
    moveDir = moveDir + forward * self.forwardInput
    moveDir = moveDir + right * self.strafeInput

    if moveDir:GetLength() > 0.01 then
        moveDir = moveDir:GetNormalized() * self.Properties.MoveSpeed * dt
        CharacterControllerRequestBus.Event.TryRelativeMove(self.entityId, moveDir)
    end
end

return PlayerController

The Properties Table

The Properties table is O3DE's Lua equivalent of Unreal's UPROPERTY or Godot's @export:

Properties = {
    Speed = { default = 10.0, description = "Movement speed", suffix = "m/s" },
    Health = { default = 100, description = "Starting health" },
    ProjectilePrefab = { default = SpawnableAsset(), description = "Bullet prefab" },
    DamageMultiplier = {
        default = 1.0,
        min = 0.0,
        max = 10.0,
        step = 0.1,
        description = "Damage scaling factor"
    },
}

These properties appear as editable fields in the Entity Inspector — per-instance values, just like @export in GDScript.


📡 EBus in Lua — The Communication Backbone

Every system in O3DE communicates through EBus (Event Bus). In Lua, you interact with EBuses to query transforms, apply physics forces, play sounds, and more.

Request Buses (Ask Something to Do Something)

-- Get world position of this entity
local tm = TransformBus.Event.GetWorldTM(self.entityId)
local pos = tm:GetTranslation()

-- Set a new position
TransformBus.Event.SetWorldTranslation(self.entityId, Vector3(10, 0, 5))

-- Apply a physics impulse
RigidBodyRequestBus.Event.ApplyLinearImpulse(self.entityId, Vector3(0, 0, 500))

-- Play a sound trigger
AudioTriggerComponentRequestBus.Event.ExecuteTrigger(self.entityId, "Play_Gunshot")

-- Raycast
local start = pos
local direction = tm:GetBasisY() -- forward
local distance = 100.0
local result = PhysicsSystemRequestBus.Broadcast.RayCast(start, direction, distance)
if result.hasHit then
    Debug.Log("Hit entity: " .. tostring(result.entityId))
end

Notification Buses (Listen for Events)

function PlayerController:OnActivate()
    -- Listen for collision events
    self.collisionHandler = PhysicsNotificationBus.Connect(self, self.entityId)
end

function PlayerController:OnCollisionBegin(collision)
    local otherEntity = collision.otherEntityId
    Debug.Log("Collided with: " .. tostring(otherEntity))

    -- Check if it's a pickup
    if TagComponentRequestBus.Event.HasTag(otherEntity, Crc32("HealthPickup")) then
        self.health = math.min(self.health + 25, self.Properties.MaxHealth)
        GameEntityContextRequestBus.Broadcast.DestroyGameEntity(otherEntity)
    end
end

Custom EBus from Lua

-- Define a custom notification bus
DamageNotificationBus = EBus("DamageNotificationBus")

-- Broadcasting damage events
DamageNotificationBus.Broadcast.OnDamageDealt(self.entityId, targetId, 25.0)

-- Listening for damage events (in another script)
function EnemyAI:OnActivate()
    self.damageHandler = DamageNotificationBus.Connect(self)
end

function EnemyAI:OnDamageDealt(sourceId, targetId, amount)
    if targetId == self.entityId then
        self.health = self.health - amount
        if self.health <= 0 then
            self:Die()
        end
    end
end

🆚 Script Canvas vs Lua vs C++ — When to Use What

Criteria Script Canvas Lua C++
Prototype a mechanic ✅ Best ✅ Good ❌ Slow iteration
Complex state machine ⚠️ Gets messy ✅ Good ✅ Best
Performance-critical (1000+ entities) ❌ ⚠️ Okay ✅ Required
Designer-friendly ✅ Best ⚠️ Needs coding ❌
Debugging ⚠️ Visual debugger ✅ LuaIDE debugger ✅ Full IDE debugger
EBus access ✅ Full ✅ Full ✅ Full
Hot-reload ✅ Instant ✅ Fast ❌ Full rebuild
Network replication ❌ ❌ ✅ Required for MP
Warning

Multiplayer replication only works in C++. If you're building a networked game, your replicated components must be C++. Lua and Script Canvas can handle client-only UI, VFX triggers, and local-only logic.


🐛 Lua Debugging with LuaIDE

The LuaIDE Gem provides an in-editor Lua debugger:

  1. Open Tools → Lua IDE from the editor menu bar
  2. Open your .lua script file
  3. Set breakpoints by clicking the gutter
  4. Enter Play mode — execution pauses at breakpoints
  5. Inspect variables, call stack, and step through code
-- Force a debug break in code
Debug.Break()

-- Log output to the console
Debug.Log("Health: " .. tostring(self.health))
Debug.Warning("Low ammo!")
Debug.Error("Critical failure in weapon system")