visual_studio_code.rst 9.4 KB

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