|
| 1 | +# Working in VS Code |
| 2 | + |
| 3 | +Develop and debug SwfitGodot Code in Visual Studio Code |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Visual Studio Code provides a compelling alternative to Xcode for SwiftGodot developers |
| 8 | +on Mac, Linux, or Windows. |
| 9 | + |
| 10 | + |
| 11 | +### Prerequisites |
| 12 | + |
| 13 | +To develop with SwiftGodot in Visual Studio Code, you will need the following prerequisites: |
| 14 | + |
| 15 | +1. Godot Engine without .NET support from the [official Godot website](https://godotengine.org/download/). |
| 16 | + |
| 17 | +2. Visual Studio Code from the [official Visual Studio Code website](https://code.visualstudio.com/Download). |
| 18 | + |
| 19 | +3. Install Swift from the [official Swift website](https://www.swift.org/install/Swift). |
| 20 | + |
| 21 | +### Configuring Visual Studio Code |
| 22 | + |
| 23 | +The Swift Extension for Visual Studio Code from the Swift Work Group provides code |
| 24 | +completion, code navigation, build task creation, and integration with LLDB for |
| 25 | +debugging, along with many other features for devloping code with Swift. |
| 26 | + |
| 27 | +Install the Swift Extension for Visual Studio from the |
| 28 | +[Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=sswg.swift-lang), |
| 29 | +or search for Swift on the Extensions tab in Visual Studio Code. |
| 30 | + |
| 31 | +Installing the Swift Extension will automatically install the CodeLLDB Extension |
| 32 | +as a dependency for Visual Studio Code for debugging support. |
| 33 | + |
| 34 | +### Create a Swift library package for your project |
| 35 | + |
| 36 | +Create a folder on disk in a file location near the Godot project that you want |
| 37 | +to use with SwiftGodot. For this article, we will assume you are working against |
| 38 | +the sample project and code from the [Meet SwiftGodot Tutorial](https://migueldeicaza.github.io/SwiftGodotDocs/tutorials/swiftgodot-tutorials) |
| 39 | + |
| 40 | +On the commmand line, change directories into the folder you created and run the |
| 41 | +swift package command to initialize a new library package: |
| 42 | + |
| 43 | +`swift package init --type library` |
| 44 | + |
| 45 | +You can now open your package folder in Visual Studio Code: |
| 46 | + |
| 47 | +`code .` |
| 48 | + |
| 49 | +Or use the "Open Folder..." File menu option in Visual Studio Code. |
| 50 | + |
| 51 | +### Setup SwiftGodot in Package.swift |
| 52 | + |
| 53 | +Inside Visual Studio Code, open Package.swift |
| 54 | + |
| 55 | +Set your library type to dynamic by adding `type: .dynamic,` to the products.library |
| 56 | +section of your package configuration. E.g., |
| 57 | + |
| 58 | +```swift |
| 59 | +.library( |
| 60 | + name: "SimpleRunnerDriver", |
| 61 | + type: .dynamic, |
| 62 | + targets: ["SimpleRunnerDriver"]), |
| 63 | +``` |
| 64 | + |
| 65 | +Add the SwiftGodot dependency to your package. |
| 66 | + |
| 67 | + |
| 68 | +```swift |
| 69 | +dependencies: [ |
| 70 | + .package(url: "https://github.com/migueldeicaza/SwiftGodot", branch: "main") |
| 71 | +], |
| 72 | +``` |
| 73 | + |
| 74 | +Modify your library target to reference the SwitGodot dependency, and add necessary |
| 75 | +swift compiler and linker settings: |
| 76 | + |
| 77 | + |
| 78 | +```swift |
| 79 | +.target( |
| 80 | + name: "SimpleRunnerDriver", |
| 81 | + dependencies: [ |
| 82 | + "SwiftGodot", |
| 83 | + ], |
| 84 | + swiftSettings: [.unsafeFlags(["-suppress-warnings"])] |
| 85 | +), |
| 86 | +``` |
| 87 | + |
| 88 | +At this point, you should be able to follow the Meet SwiftGodot Tutorial beginning in Section 2. |
| 89 | + |
| 90 | +### Building your SwiftGodot package |
| 91 | + |
| 92 | +When you are ready to build your SwiftGodot package, the Swift Extension provides |
| 93 | +a default Build task you can execute with Visual Studio Code's Build Shortcut - |
| 94 | +Ctrl+Shift+B (or Cmd+Shift+B on Mac). |
| 95 | + |
| 96 | +The initial build, especially on Windows, may take a very long time. |
| 97 | + |
| 98 | +### Setting up your gdextension |
| 99 | + |
| 100 | +When creating your `gdextension` file, your configuration file will need to |
| 101 | +contain settings specific to your platform, and you will need to copy the libraries |
| 102 | +for your operating system and architecture to the `bin` folder inside your |
| 103 | +Godot project. |
| 104 | + |
| 105 | +#### Windows |
| 106 | + |
| 107 | +If you are developing on Linux, your `libraries` and `dependencies` will need |
| 108 | +to be specified as `windows.debug.x86_64` and your libraries will be compiled into |
| 109 | +`.dll` files, so that these sections should look like this: |
| 110 | + |
| 111 | +``` |
| 112 | +[libraries] |
| 113 | +window.debug.x86_64 = "res://bin/SimpleRunnerDriver.dll" |
| 114 | +
|
| 115 | +[dependencies] |
| 116 | +windows.debug.x86_64 = {"res://bin/SwiftGodot.dll" : ""} |
| 117 | +``` |
| 118 | + |
| 119 | +You can copy these files to your Godot projects `bin` folder from the build |
| 120 | +output folder located in `.build\x86_64-unknown-windows-msvc\debug\` inside |
| 121 | +the directory where you initialized your Swift package. |
| 122 | + |
| 123 | +As an additional step on Windows, you will need to copy all of the Swift |
| 124 | +runtime libraries into the `bin` folder with SwiftGodot.dll. This is means |
| 125 | +copying all `*.dll` files from `C:\Program Files\Swift\runtime-development\usr\bin\` |
| 126 | + |
| 127 | +#### Linux |
| 128 | + |
| 129 | +If you are developing on Linux, your `libraries` and `dependencies` will need |
| 130 | +to be specified as `linux.debug.x86_64` and your libraries will be compiled into |
| 131 | +`lib*.so` files, so that these sections should look like this: |
| 132 | + |
| 133 | +``` |
| 134 | +[libraries] |
| 135 | +linux.debug.x86_64 = "res://bin/libSimpleRunnerDriver.so" |
| 136 | +
|
| 137 | +[dependencies] |
| 138 | +linux.debug.x86_64 = {"res://bin/libSwiftGodot.so" : ""} |
| 139 | +``` |
| 140 | + |
| 141 | +You can copy these files to your Godot projects `bin` folder from the build |
| 142 | +output folder located in `.build/x86_64-unknown-linux-gnu/debug/` inside |
| 143 | +the directory where you initialized your Swift package. |
| 144 | + |
| 145 | + |
| 146 | +### Debugging your SwiftGodot code |
| 147 | + |
| 148 | +You can debug your SwiftGodot code on Windows or Linux using the |
| 149 | +CodeLLDB Extension to attach to your game launched from Godot. |
| 150 | + |
| 151 | +In order to do this, you will need to add an Attach to Process |
| 152 | +launch task to your project. |
| 153 | + |
| 154 | +In Visual Sutdio Code, switch to the "Run and Debug" tab. |
| 155 | + |
| 156 | +Create a `launch.json` file by tapping on "create a launch.json file" |
| 157 | +and selecting "LLDB", which should be the first suggested option. |
| 158 | + |
| 159 | +In the newly created launch.json file, you should have a single |
| 160 | +lldb task. To turn this task into an attach task: |
| 161 | + |
| 162 | +1. Change the value for `request` from `launch` to `attach` |
| 163 | +2. Change the value for `name` to `Attach to PID` |
| 164 | +3. Remove the lines for `program`, `args`, and `cwd` |
| 165 | +4. Add a line for `pid` with value `${command:pickProcess}` |
| 166 | + |
| 167 | +Your launch configuration for lldb should now look like this: |
| 168 | + |
| 169 | +```json |
| 170 | +"type": "lldb", |
| 171 | +"request": "attach", |
| 172 | +"name": "Attach to PID", |
| 173 | +"pid": "${command:pickProcess}" |
| 174 | +``` |
| 175 | + |
| 176 | +Once you save this file, Attach to PID should be the default debug |
| 177 | +task, and can be run by pressing F5. |
| 178 | + |
| 179 | +To debug your app, |
| 180 | +1. First, take note of running Godot process PIDs by running "Attach to PID" |
| 181 | +and searching for Godot. |
| 182 | + |
| 183 | +2. Launch your game from Godot |
| 184 | + |
| 185 | +3. Return to Visual Studio and press F5 to run "Attach to PID" again. |
| 186 | + |
| 187 | +4. Search again for Godot and select the PID for your game, which should be the only |
| 188 | +process that wasn't listed in step (1) above. |
| 189 | + |
| 190 | +> On Linux or Mac, it may be possible to differentiate your game's PID from other |
| 191 | +> Godot PIDs by looking at the additional information Visual Studio Code lists about |
| 192 | +> each process, including command line options. On Windows, the Godot processes are |
| 193 | +> pretty much identical, making it difficult to differentiate your game from the editor. |
| 194 | +
|
| 195 | +At this point, Visual Studio Code should now stop on any breakpoints you have set, |
| 196 | +and you should be able to inspect Variables, set Watches, etc. |
| 197 | + |
| 198 | +> Warning: |
| 199 | +> On Mac, you will need to make your Godot engine debuggable following the steps from |
| 200 | +> this article [Debug in Xcode](https://migueldeicaza.github.io/SwiftGodotDocs/documentation/swiftgodot/debuginxcode) |
| 201 | +
|
| 202 | + |
| 203 | + |
| 204 | + |
0 commit comments