Modding/Lua Debugging: Difference between revisions

From Desynced Wiki
No edit summary
(Update debugging information)
Line 1: Line 1:
== Visual Studio Code ==
== Visual Studio Code ==
In order to debug using Visual Studio code, a few steps first need to be completed:
In order to debug using Visual Studio code, you need to install the lrdb-beamng extension: https://marketplace.visualstudio.com/items?itemName=beamng.lrdb-beamng


# [https://marketplace.visualstudio.com/items?itemName=devCAT.lua-debug Lua Debugger] VSCode extension. (Requires mono on linux)
With the extension installed, make sure to start the game with <code>-moddev</code> and keep it running.
# Following files downloaded from [https://github.com/devcat-studio devcat] and placed where the Desynced Binary is (normally in: <code>C:\Program Files (x86)\Steam\steamapps\common\Desynced\Desynced\Binaries\Win64</code>)
## [https://github.com/devcat-studio/VSCodeLuaDebug/blob/master/debuggee/dkjson.lua dkjson.lua]
## [https://github.com/devcat-studio/VSCodeLuaDebug/blob/master/debuggee/vscode-debuggee.lua vscode-debuggee.lua]
# [https://github.com/StageGames/VSCodeLuaDebugger/blob/main/mobdebug.lua mobdebug.lua] downloaded to the root of the Mods folder (normally in: <code>C:\Program Files (x86)\Steam\steamapps\common\Desynced\Desynced\Content\mods</code>)
# [http://steamcommunity.com/sharedfiles/filedetails/?id=3024638463 VSCode-Debugging] from Workshop


Note: <code>vscode-debuggee.lua</code> needs to be edited on line 3, changing <code>require 'socket.core'</code> to <code>require 'socket'</code>.
To attach the debugger, switch to the 'Run and Debug' tab in Visual Studio Code (menu 'View' -> 'Run' or by pressing <code>CTRL + SHIFT + D</code>). Click on the "create a launch.json" link and replace the content with the following:
 
With the above ready, start the game with <code>-moddev</code> (optionally <code>-log</code> too for better logging output). Once the game has started, enable the <code>VSCode Debugging</code> from the Mod list.
 
Now in Visual Studio Code, create a folder called <code>.vscode</code> and add a file called <code>launch.json</code> and paste the following content into this file:


<syntaxhighlight lang="js" line="1">
<syntaxhighlight lang="js" line="1">
Line 20: Line 11:
     "configurations": [
     "configurations": [
         {
         {
            "name": "Desynced Debugger",
             "type": "lrdb",
             "type": "lua",
             "request": "attach",
             "request": "attach",
             "workingDirectory": "${workspaceRoot}",
             "name": "Attach",
             "sourceBasePath": "${workspaceRoot}",
             "host": "localhost",
             "listenPublicly": false,
             "port": 21110,
             "listenPort": 56789,
             "sourceRoot": "C:/Program Files (x86)/Steam/steamapps/common/Desynced/Desynced/Content/mods",
             "encoding": "UTF-8"
             "stopOnEntry": false,
         }
         }
     ]
     ]
Line 33: Line 23:
</syntaxhighlight>
</syntaxhighlight>


This will create the ability to launch the debugger using Visual Studio Code, using <code>CTRL + SHIFT + D</code>. Once this has been pressed, you will now see <code>Desynced Debugger</code> with a green Play button. Upon pressing the Play button, the debugger will be launched, listening for the game.
Make sure that the "sourceRoot" field matches your Desynced installation then press F5 to attach the debugger to the game. If it doesn't end up connecting, open the "LUA REMOTE DEBUGGER - VMS" panel in the 'Run and Debug' tab of Visual Studio Code and try to double click the Desynced Lua VM which should be shown running on "localhost:21110".


Now, in game, either start a new game (or load a save game), or hit Hot Reload (<code>F7</code>) to cause the game to connect. Once connected, you'll have full access to stepping through code, as well as accessing the local and watch windows, etc. You make have to enable <code>Allow Breakpoints Everywhere</code> in your settings to set breakpoints.
Once connected, you'll have full access to breakpoints, code stepping, as well as accessing local and global variables and the watch window. Also it is recommended to open the Debug Console (menu 'View' -> 'Debug Console' or by pressing <code>CTRL + SHIFT + Y</code>) to have any logging or errors appear directly in Visual Studio Code. The debug console is interactive, too, so entering code into the input text box below the console will execute it in the game. This can be used to execute <code>Debug.Reload()</code> which will hot reload the Lua code from inside the code editor (same as pressing F7 in the game).

Revision as of 14:12, 11 April 2024

Visual Studio Code

In order to debug using Visual Studio code, you need to install the lrdb-beamng extension: https://marketplace.visualstudio.com/items?itemName=beamng.lrdb-beamng

With the extension installed, make sure to start the game with -moddev and keep it running.

To attach the debugger, switch to the 'Run and Debug' tab in Visual Studio Code (menu 'View' -> 'Run' or by pressing CTRL + SHIFT + D). Click on the "create a launch.json" link and replace the content with the following:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lrdb",
            "request": "attach",
            "name": "Attach",
            "host": "localhost",
            "port": 21110,
            "sourceRoot": "C:/Program Files (x86)/Steam/steamapps/common/Desynced/Desynced/Content/mods",
            "stopOnEntry": false,
        }
    ]
}

Make sure that the "sourceRoot" field matches your Desynced installation then press F5 to attach the debugger to the game. If it doesn't end up connecting, open the "LUA REMOTE DEBUGGER - VMS" panel in the 'Run and Debug' tab of Visual Studio Code and try to double click the Desynced Lua VM which should be shown running on "localhost:21110".

Once connected, you'll have full access to breakpoints, code stepping, as well as accessing local and global variables and the watch window. Also it is recommended to open the Debug Console (menu 'View' -> 'Debug Console' or by pressing CTRL + SHIFT + Y) to have any logging or errors appear directly in Visual Studio Code. The debug console is interactive, too, so entering code into the input text box below the console will execute it in the game. This can be used to execute Debug.Reload() which will hot reload the Lua code from inside the code editor (same as pressing F7 in the game).