Version:

使用 Lua 编辑器进行调试

Open 3D Engine (O3DE) Lua Editor (Lua IDE) 提供了一个直观的集成开发环境 (IDE),让您在创建或扩展游戏时可以轻松编写、调试和编辑 Lua 脚本。Lua Editor 是一个独立的应用程序,但您可以直接在 O3DE Editor 的 Tools 菜单中打开它。

本教程介绍如何使用 O3DE Lua 编辑器对示例脚本执行调试操作。

将示例 Lua 脚本添加到实体

  1. Tools 菜单打开 Lua Editor。

  2. File 菜单中选择 New 以创建新的 Lua 脚本。

  3. 将以下代码 CopyPaste 到新脚本中。

    -- ConstantRotation.lua
    
    local ConstantRotation =
    {
        Properties =
            {
            Rotation = { default = Vector3(0, 0, 90), description = "Constant rotation (in degrees) to apply over time." },
            },
    }
    
    function ConstantRotation:OnActivate()
        self.rotationRadians = self.Properties.Rotation;
        self.rotationRadians.x = Math.DegToRad(self.rotationRadians.x);
        self.rotationRadians.y = Math.DegToRad(self.rotationRadians.y);
        self.rotationRadians.z = Math.DegToRad(self.rotationRadians.z);
        self.tickBusHandler = TickBus.Connect(self)
    end
    
    function ConstantRotation:OnTick(deltaTime, timePoint)
        TransformBus.Event.RotateAroundLocalX(self.entityId, self.rotationRadians.x * deltaTime);
        TransformBus.Event.RotateAroundLocalY(self.entityId, self.rotationRadians.y * deltaTime);
        TransformBus.Event.RotateAroundLocalZ(self.entityId, self.rotationRadians.z * deltaTime);
    end
    
    function ConstantRotation:OnDeactivate()
        self.tickBusHandler:Disconnect()
    end
    
    return ConstantRotation
    
  4. 将脚本 Save 为项目目录中的ConstantRotation.lua

  5. 关闭 Lua 编辑器。

  6. Entity Outliner 中,选择要向其添加 Lua Script 组件的实体。

  7. Entity Inspector 中,选择 Add Component,然后选择 ScriptingLua Script

  8. Entity Inspector 中,找到 Lua Script 组件,然后单击 以打开文件浏览器。

  9. Pick Lua Script 窗口中,选择 ConstantRotation.lua,然后选择 OK

  10. Lua Script 组件中,单击 以启动 Lua 编辑器。

    Launch Lua Editor from Lua Script component in O3DE Editor

连接到 O3DE 编辑器

Remote Tools Gem 促进 O3DE 应用程序之间的本地连接。

注意:

远程工具 Gem 必须 在您的项目中启用 才能进行调试。

远程工具 Gem 行为在发布版本中被禁用。

Remote Tools Gem 会在 Lua Editor 启动时自动启动,并且必须在后台运行,Lua Editor 才能找到可以连接的目标。 由于调试功能是通过网络套接字启用的,因此必须先将 Lua Editor 连接到运行脚本的目标,然后才能进行调试。在本教程中,您将连接到 O3DE Editor。

  1. 在 Lua Editor 工具栏中,选择 Target: None,然后选择 Editor(ID) 以连接到 O3DE Editor。

    Target selector

    注意:
    您可能需要展开 Lua Editor 窗口,才能看到 Lua Editor 工具栏上接下来几个步骤的按钮。
  2. 在 Lua Editor 工具栏中,将 Context 设置保留为 Default 作为调试上下文。默认设置适用于调试组件实体脚本,例如本教程中的脚本。

    Context selector

  3. Debugging 图标变为绿色,表示 Lua Editor 和 O3DE Editor 已连接:

    Lua Editor connected to O3DE Editor

    单击 Class Reference 中的 Classes 以显示可用的 Lua 库。您可以对 事件总线 EBuses和 全局变量 Globals执行相同的操作。

    Classes Reference

    Classes

    注意:
    类引用功能仅对默认上下文和组件实体脚本处于活动状态。

设置断点

连接后,您可以通过设置断点来暂停脚本。

  1. 在 Lua Editor 工具栏中,单击 Breakpoints 图标 Breakpoints Icon 以显示 Breakpoints 窗口。

  2. 在 Lua 编辑器中,点击 constantrotation.lua 脚本中的一个或多个行号来设置一个或多个断点。添加断点时,每个断点的行号和脚本路径将添加到 Breakpoints 窗口中。

  3. 在 O3DE 编辑器中,按 Ctrl+G 运行游戏,或单击视区底部的 Simulate 图标以启用游戏模拟并运行脚本。Lua Editor 将打开,并在遇到的第一个断点处停止一个黄色标记。

    Debugger stopped on breakpoint

    当执行在断点处停止时,Lua LocalsStackWatched Variables 窗格中将提供更多信息。

  4. 点击 Stack 图标 Stack Icon 以显示 Stack 窗口。

  5. 点击 Lua Locals 图标 Lua Locals Icon 以显示局部 Lua 变量。

  6. 点击 Watched Variables 图标 Watched Variables Icon 以打开 Watched Variables 窗口,您可以在其中指定要监视的变量。

  7. 按 F11 键几次以逐步执行代码。请注意 Stack, Lua Locals, 和 Watched Variables 窗口的内容是如何变化的。

    提示:
    为了更加方便,您可以浮动或停靠这些窗口。
  8. 要从调试中分离,请单击Debugging.

    Click to detach from debugging

  9. 在 O3DE 编辑器中,按 Esc 停止游戏。

调试时可用的选项

下表总结了调试时可用的常用选项。

图标动作键盘快捷键说明
Run in Editor IconRun in EditorAlt+F5在 O3DE 编辑器中运行。
Run on Target IconRun on TargetCtrl+F5将脚本发送到连接的目标并运行它。
Run/Continue IconRun/ContinueF5运行或继续运行当前脚本。
Step Into IconStep IntoF11单步执行当前行上调用的函数。
Step Out IconStep OutShift+F11跳出被调用的函数。
Step Over IconStep OverF10单步执行在当前行上调用的函数。
Toggle Breakpoint IconToggle BreakpointF9启用或禁用当前行上的断点。