visual_studio_code.rst 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. .. _doc_configuring_an_ide_vscode:
  2. Visual Studio Code
  3. ==================
  4. .. note::
  5. This documentation is for contributions to the game engine, and not using
  6. Visual Studio Code as a C# or GDScript editor. To code C# or GDScript in an external editor, see
  7. :ref:`the C# guide to configure an external editor <doc_c_sharp_setup_external_editor>`.
  8. `Visual Studio Code <https://code.visualstudio.com>`_ is a free cross-platform code editor
  9. by `Microsoft <https://microsoft.com>`_ (not to be confused with :ref:`doc_configuring_an_ide_vs`).
  10. Importing the project
  11. ---------------------
  12. - Make sure the C/C++ extension is installed. You can find instructions in
  13. the `official documentation <https://code.visualstudio.com/docs/languages/cpp>`_.
  14. Alternatively, `clangd <https://open-vsx.org/extension/llvm-vs-code-extensions/vscode-clangd>`_
  15. can be used instead.
  16. - When using the clangd extension, run ``scons compiledb=yes``.
  17. - From the Visual Studio Code's main screen open the Godot root folder with
  18. **File > Open Folder...**.
  19. - Press :kbd:`Ctrl + Shift + P` to open the command prompt window and enter *Configure Task*.
  20. .. figure:: img/vscode_configure_task.png
  21. :align: center
  22. - Select the **Create tasks.json file from template** option.
  23. .. figure:: img/vscode_create_tasksjson.png
  24. :align: center
  25. - Then select **Others**.
  26. .. figure:: img/vscode_create_tasksjson_others.png
  27. :align: center
  28. - 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 <https://code.visualstudio.com/docs/editor/tasks#_custom-tasks>`_ for more details on tasks.
  29. - Within the ``tasks.json`` file find the ``"tasks"`` array and add a new section to it:
  30. .. code-block:: js
  31. :caption: .vscode/tasks.json
  32. {
  33. "label": "build",
  34. "group": "build",
  35. "type": "shell",
  36. "command": "scons",
  37. "args": [
  38. // enable for debugging with breakpoints
  39. "dev_build=yes",
  40. ],
  41. "problemMatcher": "$msCompile"
  42. }
  43. .. figure:: img/vscode_3_tasks.json.png
  44. :figclass: figure-w480
  45. :align: center
  46. An example of a filled out ``tasks.json``.
  47. Arguments can be different based on your own setup and needs. See
  48. :ref:`doc_introduction_to_the_buildsystem` for a full list of arguments.
  49. Debugging the project
  50. ---------------------
  51. To run and debug the project you need to create a new configuration in the ``launch.json`` file.
  52. - Press :kbd:`Ctrl + Shift + D` to open the Run panel.
  53. - If ``launch.json`` file is missing you will be prompted to create a new one.
  54. .. figure:: img/vscode_1_create_launch.json.png
  55. :align: center
  56. - Select **C++ (GDB/LLDB)**. There may be another platform-specific option here. If selected,
  57. adjust the configuration example provided accordingly.
  58. - Within the ``launch.json`` file find the ``"configurations"`` array and add a new section to it:
  59. .. tabs::
  60. .. code-tab:: js LinuxBSD
  61. {
  62. "name": "Launch Project",
  63. "type": "lldb",
  64. "request": "launch",
  65. // Change to godot.linuxbsd.editor.dev.x86_64.llvm for llvm-based builds.
  66. "program": "${workspaceFolder}/bin/godot.linuxbsd.editor.dev.x86_64",
  67. // Change the arguments below for the project you want to test with.
  68. // To run the project instead of editing it, remove the "--editor" argument.
  69. "args": [ "--editor", "--path", "path-to-your-godot-project-folder" ],
  70. "stopAtEntry": false,
  71. "cwd": "${workspaceFolder}",
  72. "environment": [],
  73. "externalConsole": false,
  74. "preLaunchTask": "build"
  75. }
  76. .. code-tab:: js LinuxBSD_gdb
  77. {
  78. "name": "Launch Project",
  79. "type": "cppdbg",
  80. "request": "launch",
  81. // Change to godot.linuxbsd.editor.dev.x86_64.llvm for llvm-based builds.
  82. "program": "${workspaceFolder}/bin/godot.linuxbsd.editor.dev.x86_64",
  83. // Change the arguments below for the project you want to test with.
  84. // To run the project instead of editing it, remove the "--editor" argument.
  85. "args": [ "--editor", "--path", "path-to-your-godot-project-folder" ],
  86. "stopAtEntry": false,
  87. "cwd": "${workspaceFolder}",
  88. "environment": [],
  89. "externalConsole": false,
  90. "setupCommands":
  91. [
  92. {
  93. "description": "Enable pretty-printing for gdb",
  94. "text": "-enable-pretty-printing",
  95. "ignoreFailures": true
  96. },
  97. {
  98. "description": "Load custom pretty-printers for Godot types.",
  99. "text": "source ${workspaceRoot}/misc/utility/godot_gdb_pretty_print.py"
  100. }
  101. ],
  102. "preLaunchTask": "build"
  103. }
  104. .. code-tab:: js Windows
  105. {
  106. "name": "Launch Project",
  107. "type": "cppvsdbg",
  108. "request": "launch",
  109. "program": "${workspaceFolder}/bin/godot.windows.editor.dev.x86_64.exe",
  110. // Change the arguments below for the project you want to test with.
  111. // To run the project instead of editing it, remove the "--editor" argument.
  112. "args": [ "--editor", "--path", "path-to-your-godot-project-folder" ],
  113. "stopAtEntry": false,
  114. "cwd": "${workspaceFolder}",
  115. "environment": [],
  116. "console": "internalConsole",
  117. "visualizerFile": "${workspaceFolder}/platform/windows/godot.natvis",
  118. "preLaunchTask": "build"
  119. }
  120. .. code-tab:: js macOS_x86_64
  121. {
  122. "name": "Launch Project",
  123. "type": "lldb",
  124. "request": "launch",
  125. "program": "${workspaceFolder}/bin/godot.macos.editor.x86_64",
  126. // Change the arguments below for the project you want to test with.
  127. // To run the project instead of editing it, remove the "--editor" argument.
  128. "args": ["--editor", "--path", "path-to-your-godot-project-folder"],
  129. "cwd": "${workspaceFolder}",
  130. "preLaunchTask": "build"
  131. }
  132. .. code-tab:: js macOS_arm64
  133. {
  134. "name": "Launch Project",
  135. "type": "lldb",
  136. "request": "launch",
  137. "program": "${workspaceFolder}/bin/godot.macos.editor.arm64",
  138. // Change the arguments below for the project you want to test with.
  139. // To run the project instead of editing it, remove the "--editor" argument.
  140. "args": ["--editor", "--path", "path-to-your-godot-project-folder"],
  141. "cwd": "${workspaceFolder}",
  142. "preLaunchTask": "build"
  143. }
  144. .. figure:: img/vscode_2_launch.json.png
  145. :figclass: figure-w480
  146. :align: center
  147. An example of a filled out ``launch.json``.
  148. .. note::
  149. Due to sporadic performance issues, it is recommended to use LLDB over GDB on Unix-based systems.
  150. Make sure that the `CodeLLDB extension <https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb>`_
  151. is installed for configurations using `lldb`.
  152. If you encounter issues with lldb, you may consider using gdb (see the LinuxBSD_gdb configuration).
  153. Do note that lldb may work better with LLVM-based builds. See :ref:`doc_compiling_for_linuxbsd` for further information.
  154. The name under ``program`` depends on your build configuration,
  155. e.g. ``godot.linuxbsd.editor.dev.x86_64`` for 64-bit LinuxBSD platform with
  156. ``target=editor`` and ``dev_build=yes``.
  157. Configuring Intellisense
  158. ------------------------
  159. For the C/C++ extension:
  160. To fix include errors you may be having, you need to configure some settings in the ``c_cpp_properties.json`` file.
  161. - First, make sure to build the project since some files need to be generated.
  162. - Edit the C/C++ Configuration file either with the UI or with text:
  163. .. figure:: img/vscode_edit_configurations.webp
  164. :align: center
  165. - Add an include path for your platform, for example, ``${workspaceFolder}/platform/windows``.
  166. - Add defines for the editor ``TOOLS_ENABLED``, debug builds ``DEBUG_ENABLED``, and tests ``TESTS_ENABLED``.
  167. - Make sure the compiler path is configured correctly to the compiler you are using. See :ref:`doc_introduction_to_the_buildsystem` for further information on your platform.
  168. - The ``c_cpp_properties.json`` file should look similar to this for Windows:
  169. .. code-block:: js
  170. :caption: .vscode/c_cpp_properties.json
  171. {
  172. "configurations": [
  173. {
  174. "name": "Win32",
  175. "includePath": [
  176. "${workspaceFolder}/**",
  177. "${workspaceFolder}/platform/windows"
  178. ],
  179. "defines": [
  180. "_DEBUG",
  181. "UNICODE",
  182. "_UNICODE",
  183. "TOOLS_ENABLED",
  184. "DEBUG_ENABLED",
  185. "TESTS_ENABLED"
  186. ],
  187. "windowsSdkVersion": "10.0.22621.0",
  188. "compilerPath": "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.39.33519/bin/Hostx64/x64/cl.exe",
  189. "cStandard": "c17",
  190. "cppStandard": "c++17",
  191. "intelliSenseMode": "windows-msvc-x64"
  192. }
  193. ],
  194. "version": 4
  195. }
  196. - 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.
  197. - 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.
  198. If you run into any issues, ask for help in one of
  199. `Godot's community channels <https://godotengine.org/community>`__.
  200. .. tip::
  201. To get linting on class reference XML files, install the
  202. `vscode-xml extension <https://marketplace.visualstudio.com/items?itemName=redhat.vscode-xml>`__.