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.
Next open your extension folder in VSCode (this is your workspace), I have opened my mod and the infocenter.lua file for this demo.
Next in VSCode open the command pallete, in VSCode on Windows the default is "CTRL + SHIFT + P" and select "Debug: Add Configuration.."
Select "EmmyLua New Debugger"
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.
Next go to our mods init function and position your cursor ready to insert some code.
Press "CTRL + SHIFT + P" to open the command pallete and select "EmmyLua: Insert Emmy Debugger Code"
X4 is 64 bit so select "x64"
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.
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.
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.
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.
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.
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.
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!
[TUTORIAL] X4 UI Dev with debugging (VSCode and EmmyLua))
Moderators: Moderators for English X Forum, Scripting / Modding Moderators
-
Forleyor
- Posts: 44
- Joined: Thu, 11. Jun 15, 19:09

-
ChemODun
- Posts: 562
- Joined: Mon, 12. Feb 07, 21:58

Re: [TUTORIAL] X4 UI Dev with debugging (VSCode and EmmyLua))
Many thanks!
Possibility to debug lua part breaks may be main showstopper for me in this area of modding
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
Freedom in space
-
ChemODun
- Posts: 562
- Joined: Mon, 12. Feb 07, 21:58

Re: [TUTORIAL] X4 UI Dev with debugging (VSCode and EmmyLua))
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
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!
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", ...)
Otherwise, breakpoints in VS Code GUI will be ignored by debugger!
Multiply entropy by absolute zero
Freedom in space
Freedom in space















