Modding/Lua Debugging

From Desynced Wiki
Revision as of 14:12, 11 April 2024 by Bernhard (talk | contribs) (Update debugging information)

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).