Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

Visual Studio Code

Visual Studio Code 是由 Microsoft 提供的免費跨平台 IDE (請不要與 Visual Studio 搞混)。

匯入專案

  • 請確定有安裝 C/C++ 延伸模組。可以在 官方說明文件 (英語) 中瞭解詳細說明。

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

  • 從 Visual Studio Code 的主畫面中通過 檔案 > 開啟資料夾... 來開啟 Godot 根目錄。

  • 按一下 Ctrl + Shift + P 來開啟命令選擇區,並輸入 Configure Task (設定工作)。

../../../_images/vscode_configure_task.png
  • 選擇 從範本建立 tasks.json 檔案 選項。

../../../_images/vscode_create_tasksjson.png
  • 接著選擇 Others

../../../_images/vscode_create_tasksjson_others.png
  • 如果沒有「從範本建立tasks.json檔案」這樣的選項可用,請刪除該檔案(如果資料夾中已存在該檔案)或手動建立「.vscode/tasks.json」檔案。有關工作的更多詳細信息,請參閱“Visual Studio Code 中的工作 <https://code.visualstudio.com/docs/editor/tasks#_custom-tasks>”。

  • tasks.json 檔案中,找到 "tasks" 陣列,並在其中新增一段新的內容:

    {
      "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 來開啟 [執行] 面板。

  • 若沒有 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 extension 已經安裝。

如果遇到lldb的問題,你可以考慮使用gdb,參閱X11_gdb配置。

請注意,lldb可能在基於llvm的建構中工作得更好。參閱 doc_compiling_for_x11 以瞭解更多資訊。

program 中的名稱會依據建置設定而有所不同,如有開啟 tools 的 64 位元 X11 版為 godot.x11.tools.64

若遇到任何問題,請在 任何一個 Godot 的社群頻道 中尋求幫助。

小訣竅

安裝 VSMonoDebugger 擴充套件