Visual Studio Code

Visual Studio Code微软推出的免费跨平台代码编辑器(请勿与 Visual Studio 混淆)。

导入项目

  • 确保已安装 C/C++ 扩展。你可以在官方文档中找到说明。或者也可以用 clangd 代替。

  • 使用 clangd 扩展时,请执行 scons compiledb=yes

  • 现在在VS Code中打开克隆的godot文件夹 文件 > 打开文件夹… .

  • 按 Ctrl + Shift + P 打开命令提示符窗口,然后输入 Configure Task

../../../_images/vscode_configure_task.png
  • 选择从模板中创建tasks.json文件选项.
../../../_images/vscode_create_tasksjson.png
  • 然后选择其他
../../../_images/vscode_create_tasksjson_others.png
  • If there is no such option as Create tasks.json file from template available, either delete the file if it already exists in your folder or create a .vscode/tasks.json file manually. See Tasks in Visual Studio Code for more details on tasks.

  • tasks.json 文件中找到 "tasks" 数组, 并在其中添加一个新部分:

    1. {
    2. "label": "build",
    3. "group": "build",
    4. "type": "shell",
    5. "command": "scons",
    6. "args": [
    7. // enable for debugging with breakpoints
    8. "dev_build=yes",
    9. ],
    10. "problemMatcher": "$msCompile"
    11. }
../../../_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" 数组, 并添加一个新部分:

LinuxBSDLinuxBSD_gdbWindows

  1. {
  2. "name": "Launch Project",
  3. "type": "lldb",
  4. "request": "launch",
  5. // Change to godot.linuxbsd.editor.dev.x86_64.llvm for llvm-based builds.
  6. "program": "${workspaceFolder}/bin/godot.linuxbsd.editor.dev.x86_64",
  7. // Change the arguments below for the project you want to test with.
  8. // To run the project instead of editing it, remove the "--editor" argument.
  9. "args": [ "--editor", "--path", "path-to-your-godot-project-folder" ],
  10. "stopAtEntry": false,
  11. "cwd": "${workspaceFolder}",
  12. "environment": [],
  13. "externalConsole": false,
  14. "preLaunchTask": "build"
  15. }
  1. {
  2. "name": "Launch Project",
  3. "type": "cppdbg",
  4. "request": "launch",
  5. // Change to godot.linuxbsd.editor.dev.x86_64.llvm for llvm-based builds.
  6. "program": "${workspaceFolder}/bin/godot.linuxbsd.editor.dev.x86_64",
  7. // Change the arguments below for the project you want to test with.
  8. // To run the project instead of editing it, remove the "--editor" argument.
  9. "args": [ "--editor", "--path", "path-to-your-godot-project-folder" ],
  10. "stopAtEntry": false,
  11. "cwd": "${workspaceFolder}",
  12. "environment": [],
  13. "externalConsole": false,
  14. "setupCommands":
  15. [
  16. {
  17. "description": "Enable pretty-printing for gdb",
  18. "text": "-enable-pretty-printing",
  19. "ignoreFailures": true
  20. },
  21. {
  22. "description": "Load custom pretty-printers for Godot types.",
  23. "text": "source ${workspaceRoot}/misc/scripts/godot_gdb_pretty_print.py"
  24. }
  25. ],
  26. "preLaunchTask": "build"
  27. }
  1. {
  2. "name": "Launch Project",
  3. "type": "cppvsdbg",
  4. "request": "launch",
  5. "program": "${workspaceFolder}/bin/godot.windows.editor.dev.x86_64.exe",
  6. // Change the arguments below for the project you want to test with.
  7. // To run the project instead of editing it, remove the "--editor" argument.
  8. "args": [ "--editor", "--path", "path-to-your-godot-project-folder" ],
  9. "stopAtEntry": false,
  10. "cwd": "${workspaceFolder}",
  11. "environment": [],
  12. "console": "internalConsole",
  13. "visualizerFile": "${workspaceFolder}/platform/windows/godot.natvis",
  14. "preLaunchTask": "build"
  15. }
../../../_images/vscode_2_launch.json.png

填写好的 launch.json 的例子.

备注

由于偶然的性能问题,建议在基于Unix的系统上使用LLDB而不是GDB。确保 CodeLLDB extension 已经安装。

If you encounter issues with lldb, you may consider using gdb (see the LinuxBSD_gdb configuration).

Do note that lldb may work better with LLVM-based builds. See 为 Linux、*BSD 平台编译 for further information.

The name under program depends on your build configuration, e.g. godot.linuxbsd.editor.dev.x86_64 for 64-bit LinuxBSD platform with target=editor and dev_build=yes.

Configuring Intellisense

For the C/C++ extension:

To fix include errors you may be having, you need to configure some settings in the c_cpp_properties.json file.

  • First, make sure to build the project since some files need to be generated.

  • Edit the C/C++ Configuration file either with the UI or with text:

../../../_images/vscode_edit_configurations.webp
  • Add an include path for your platform, for example, ${workspaceFolder}/platform/windows.

  • Add defines for the editor TOOLS_ENABLED, debug builds DEBUG_ENABLED, and tests TESTS_ENABLED.

  • Make sure the compiler path is configured correctly to the compiler you are using. See 构建系统介绍 for further information on your platform.

  • The c_cpp_properties.json file should look similar to this for Windows:

    1. {
    2. "configurations": [
    3. {
    4. "name": "Win32",
    5. "includePath": [
    6. "${workspaceFolder}/**",
    7. "${workspaceFolder}/platform/windows"
    8. ],
    9. "defines": [
    10. "_DEBUG",
    11. "UNICODE",
    12. "_UNICODE",
    13. "TOOLS_ENABLED",
    14. "DEBUG_ENABLED",
    15. "TESTS_ENABLED"
    16. ],
    17. "windowsSdkVersion": "10.0.22621.0",
    18. "compilerPath": "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.39.33519/bin/Hostx64/x64/cl.exe",
    19. "cStandard": "c17",
    20. "cppStandard": "c++17",
    21. "intelliSenseMode": "windows-msvc-x64"
    22. }
    23. ],
    24. "version": 4
    25. }
  • Alternatively, you can use the scons argument compiledb=yes and set the compile commands setting compileCommands to compile_commands.json, found in the advanced section of the C/C++ Configuration UI.

    • This argument can be added to your build task in tasks.json since it will need to be run whenever files are added or moved.

如果遇到问题, 也可在 Godot 社区论坛 中寻求帮助.

小技巧

To get linting on class reference XML files, install the vscode-xml extension.