[TUTORIAL] X4 UI Dev with debugging (VSCode and EmmyLua))

The place to discuss scripting and game modifications for X4: Foundations.

Moderators: Moderators for English X Forum, Scripting / Modding Moderators

Forleyor
Posts: 44
Joined: Thu, 11. Jun 15, 19:09
x4

[TUTORIAL] X4 UI Dev with debugging (VSCode and EmmyLua))

Post by Forleyor »

Quick step by step guide as requested to be able to debug your lua code when working on the UI.

First open VSCode and install the EmmyLua extension.
Spoiler
Show
Image
Next open your extension folder in VSCode (this is your workspace), I have opened my mod and the infocenter.lua file for this demo.
Spoiler
Show
Image
Next in VSCode open the command pallete, in VSCode on Windows the default is "CTRL + SHIFT + P" and select "Debug: Add Configuration.."
Spoiler
Show
Image
Select "EmmyLua New Debugger"
Spoiler
Show
Image
This will create a ".vscode" folder as seen in a the workspace view with the newly generated debug config in "launch.json", the default host and port is "localhost:9966" in EmmyLua.
Spoiler
Show
Image
Next go to our mods init function and position your cursor ready to insert some code.
Spoiler
Show
Image
Press "CTRL + SHIFT + P" to open the command pallete and select "EmmyLua: Insert Emmy Debugger Code"
Spoiler
Show
Image
X4 is 64 bit so select "x64"
Spoiler
Show
Image
On pressing enter EmmyLua should add the code where you put your cursor, note the default host and port is "localhost:9966" if you changed it in the above step make sure to change it here to match.
Spoiler
Show
Image
At this point VSCode and the game are ready to set a breakpoint, so with the game running and the map menu open I set a breakpoint on the main function that will run when I click my mods button in the left sidebar, for me that is on line 1,397 as noted by the red circle on the left of the line number in the text editor view.
Spoiler
Show
Image
Now from the top menu select: Run > Start Debugging (F5) and VSCode should switch to the debugger tab and be ready to go if nothing went wrong, you can also open the console view and you should see "Connected" in the Debug Console output, you will also have some debug buttons for (pause process/stepping/restart/stop debugging) at the top middle.
Spoiler
Show
Image
Now when I click on the Info Center button In X4 to load my menu the code will execute to start creating the menu and hit the breakpoint, this will pause the game and VSCode will have a lot more info about data in the current context of the breakpoint, you will also see there is data shown for "variables/watches/call stack" etc.
Spoiler
Show
Image
At this point you can step through the code and debug as normal, you can also mouse over variables and inspect what they contain for example after my function has got a list of the players owned objects and mouse over the playerobjects variable you can see it is a table that contains 792 objects and can drill down through the entries etc.
Spoiler
Show
Image

BONUS
This is a small bonus to show how much info EmmyLua can give you very quickly and easily by adding files or folders to the workspace, Egosofts Helper.lua file has a lot of helper functions etc, but if I do "Helper." you will see I get no suggestions and doesn't know what it is.
Spoiler
Show
Image
I have the vanilla lua files extracted and so go to: "File > Add folder to workspace" navigate to where the extracted files are and add the addons folder which has all the vanilla lua files including helper.lua
Just from this EmmyLua will scan the new files and now when I type "Helper." it has all the info that makes UI modding in X4 a lot lot nicer Note how tiny the scrollbar is!
Spoiler
Show
Image
User avatar
ChemODun
Posts: 562
Joined: Mon, 12. Feb 07, 21:58
x4

Re: [TUTORIAL] X4 UI Dev with debugging (VSCode and EmmyLua))

Post by ChemODun »

Many thanks!
Possibility to debug lua part breaks may be main showstopper for me in this area of modding
Multiply entropy by absolute zero

Freedom in space
User avatar
ChemODun
Posts: 562
Joined: Mon, 12. Feb 07, 21:58
x4

Re: [TUTORIAL] X4 UI Dev with debugging (VSCode and EmmyLua))

Post by ChemODun »

Please pay attention!

There is some possible issues with breakpoints establishing via VS Code GUI:

In case you are using the commands Register_Require or Register_Require_With_Init from SirNukes Mod Support APIs, your lua source files have to be placed in the same patch, what you stated in calls in these functions.
I.e. if you have

Code: Select all

Register_Require_With_Init("extensions.extansion_folder.ui.file_name", ...)
The file in your workspace has to be accessible as extensions\extansion_folder\ui\file_name.lua.

Otherwise, breakpoints in VS Code GUI will be ignored by debugger!
Multiply entropy by absolute zero

Freedom in space

Return to “X4: Foundations - Scripts and Modding”