Visual Studio Code

備註

This documentation is for contributions to the game engine, and not using Visual Studio Code as a C# or GDScript editor. To code C# or GDScript in an external editor, see the C# guide to configure an external editor.

Visual Studio Code 是由 Microsoft 推出的免費跨平台程式碼編輯器(請勿與 Visual Studio 搞混)。

匯入專案

  • 請確保已安裝 C/C++ 擴充套件。相關教學可參考 官方文件。你也可以改用 clangd

  • 若使用 clangd 擴充,請執行 scons compiledb=yes

  • 於 Visual Studio Code 主畫面,透過 File > Open Folder... 開啟 Godot 根目錄。

  • 按下 Ctrl + Shift + P 開啟命令面板,輸入 Configure Task

../../../_images/vscode_configure_task.png
  • 選擇 Create tasks.json file from template 選項。

../../../_images/vscode_create_tasksjson.png
  • 然後選擇 Others

../../../_images/vscode_create_tasksjson_others.png
  • 如果沒有 Create tasks.json file from template 這個選項,請確認資料夾內沒有同名檔案,或自行手動建立 .vscode/tasks.json。更多工作相關資訊請見:Tasks in Visual Studio Code

  • tasks.json 檔案找到 "tasks" 陣列,新增一組設定內容:

    .vscode/tasks.json
    {
      "label": "build",
      "group": "build",
      "type": "shell",
      "command": "scons",
      "args": [
        // enable for debugging with breakpoints
        "dev_build=yes",
      ],
      "problemMatcher": "$msCompile"
    }
    
../../../_images/vscode_3_tasks.json.png

填妥的 tasks.json 範例。

參數可依你自身需求調整,完整參數列表請見 建置系統介紹

除錯專案

如需執行與除錯專案,必須在 launch.json 新增設定。

  • 按下 Ctrl + Shift + D 開啟執行(Run)面板。

  • 若缺少 launch.json,系統會提示你建立新檔案。

../../../_images/vscode_1_create_launch.json.png
  • 選擇 C++ (GDB/LLDB),若有平台專屬選項請依需求選擇,並依實際情況調整下方範例設定。

  • launch.json 檔案找到 "configurations" 陣列,新增一組設定內容:

{
  "name": "Launch Project",
  "type": "lldb",
  "request": "launch",
  // Change to godot.linuxbsd.editor.dev.x86_64.llvm for llvm-based builds.
  "program": "${workspaceFolder}/bin/godot.linuxbsd.editor.dev.x86_64",
  // Change the arguments below for the project you want to test with.
  // To run the project instead of editing it, remove the "--editor" argument.
  "args": [ "--editor", "--path", "path-to-your-godot-project-folder" ],
  "stopAtEntry": false,
  "cwd": "${workspaceFolder}",
  "environment": [],
  "externalConsole": false,
  "preLaunchTask": "build"
}
../../../_images/vscode_2_launch.json.png

填妥的 launch.json 範例。

備註

因偶有效能問題,建議在 Unix 系統上使用 LLDB 而非 GDB。請確認已安裝 CodeLLDB 擴充

若遇到 lldb 問題,可考慮改用 gdb(參考 LinuxBSD_gdb 設定)。

注意 lldb 在 LLVM 編譯環境下表現較佳,詳情請參考 在 Linux、*BSD 上編譯

program 欄位的名稱依你建置參數而異,例如 64 位元 LinuxBSD 編輯器開發版為 godot.linuxbsd.editor.dev.x86_64

設定 Intellisense

針對 C/C++ 擴充模組:

若遇到標頭檔 include 錯誤,請調整 c_cpp_properties.json 設定檔。

  • 首先請先建置專案,因為某些檔案需先產生。

  • 你可透過圖形介面或直接編輯文字檔來調整 C/C++ 設定檔:

../../../_images/vscode_edit_configurations.webp
  • 針對你的平台新增 include 路徑,例如 ${workspaceFolder}/platform/windows

  • 新增編輯器 TOOLS_ENABLED、除錯版 DEBUG_ENABLED,以及測試 TESTS_ENABLED 等巨集定義。

  • 請確認 compiler path 指向你實際使用的編譯器。不同平台細節可參考 建置系統介紹

  • Windows 下的 c_cpp_properties.json 範例:

    .vscode/c_cpp_properties.json
    {
      "configurations": [
        {
          "name": "Win32",
          "includePath": [
            "${workspaceFolder}/**",
            "${workspaceFolder}/platform/windows"
          ],
          "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE",
            "TOOLS_ENABLED",
            "DEBUG_ENABLED",
            "TESTS_ENABLED"
          ],
          "windowsSdkVersion": "10.0.22621.0",
          "compilerPath": "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.39.33519/bin/Hostx64/x64/cl.exe",
          "cStandard": "c17",
          "cppStandard": "c++17",
          "intelliSenseMode": "windows-msvc-x64"
        }
      ],
      "version": 4
    }
    
  • 你也可以用 scons 參數 compiledb=yes,並在 C/C++ 設定介面進階選項將 compileCommands 設為 compile_commands.json

    • 這個參數可加入 tasks.json 的建置任務,方便每次新增或移動檔案時重新產生。

如遇問題,歡迎到 Godot 社群頻道 求助。

小訣竅

如需對 class reference XML 檔案進行語法檢查,請安裝 vscode-xml 擴充套件