[Tutorial] Code-Complete for Visual Studio Code

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

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

Post Reply
Misunderstood Wookie
Posts: 377
Joined: Mon, 15. Mar 04, 08:07
x4

[Tutorial] Code-Complete for Visual Studio Code

Post by Misunderstood Wookie » Fri, 26. Apr 19, 14:38

What is this?
Not to be confused with the Visual Studio Community/Pro this info is intended to get much-needed code complete and code info on text hover, basically the general quality of life for working with XML, specifically with X4 Scripts in the MD and AiScripts folders.


Lets Begin
Prepare your working enviorment
Show
First things first you are going to want to make sure you have a nice clean copy of X4 with the CAT files extracted with the existing *.cat & *.dat files deleted from the root directory (Root= Where X4.exe is located) I like to duplicate my game install for this purpose to provide a stable environment for making new things without affecting the main game but you don't need to, you can just dump the cat files somewhere safe. If you do not understand how to extract the catalogue files for the vanilla game or lack the ability to understand your own operating system I suggest stopping here and learning some basics :roll: .
Tip - create a shortcut too or symbolically link the XTools's files into your working directory and making a handy script to read the CAT files from your main X4 Game directory and extract them into your modding environment as this will save you time.

Example:
Image
Time to Setup Visual Studio Code
Show
Requirements
For this part, you are going to want to have VSCode & Java SDK 8/Newer installed or Jave Runtimes (JRE) and that you have VSCode's Java path configured in the settings.xml.
ou can open up CMD and type

Code: Select all

java -version
if you want to test java is working.[/size]

One of the ways I like to quickly get JDK (if you go down that route) up and going system-wide is making the changes to the System Environment rather than the application.
To set things up as a system environment path you need to open View Advanced System Settings, Then open the "Environment Variables" at the bottom of the dialogue box which appears. Then you under the 'System Variables' click "New" give a name of JAVA_HOME & then browse to the JDK working directory you have installed.

Example:
Image

The Magic Touch (setting up VSCode for your mod)
Tip: root= Path to your X4 Directory (where x4.exe is located)
Tip: "schemaLocation=" does not affect your mod at all, so you can set that any path you like where your XSD schema files are located.
  • Install XML RED HAT - extentsion (Unsure how to set this up? and use Watch this!)
    Read the requirements as you will need Java JDK setup in VSCode or on your system for this to work correctly.
    If you're working with existing game scripts that were extracted into their original directories you have code lookup and everything working.

    Now if you're working inside custom mod we need to configure Scheme locations for MD scripts and/or Aiscripts depending on which your mod uses.

    MD Scripts
    The basics are as follows,
  • Copy file 'root/md/md.xsd' into your mods 'md' folder Like So
  • Open your copied 'md.xsd', change line 4 so that the location points to the "root\libraries\md.xsd"

    There are a couple of methods for this -
    1. Be smart and use the existing directory depth variables
    If you had your scripts located in 'root\extentsions\mymod\md' you would need to go up three levels to reach the root dir as the md.xsd is located in a subfolder of your mod.

    Code: Select all

    <xs:include schemaLocation="..\..\..\libraries\md.xsd" />
    Make sense. You can add more of these "..\" if located somewhere else but this code should work if you keep your mod in the extension folder like a normal mod.

    2. Change the location to a direct path, for example, my setup would look like -

    Code: Select all

    <xs:include schemaLocation="C:\Program Files (x86)\Steam\steamapps\common\X4 Foundations Dev\libraries\md.xsd" />
    AiScripts
  • Copy file 'root/aiscripts/aiscripts.xsd' into your mods 'aiscripts' folder.
  • Open your copied 'aiscripts.xsd', change line 4 so that the location points to the "root\libraries\aiscripts.xsd"

    Same as the MD Scripts methods you can do this in the following ways -
    1. Be smart and use the existing directory depth variables
    If you had your scripts located in 'root\extentsions\mymod\aiscripts' you would need to go up three levels to reach the root dir as the aiscripts.xsd is located in a subfolder of your mod.

    Code: Select all

    <xs:include schemaLocation="..\..\..\libraries\aiscripts.xsd" />
    Just the like MD Scripts the process is the same pretty much.

    2. Change the location to a direct path, for example, my setup would look like -

    Code: Select all

    <xs:include schemaLocation="C:\Program Files (x86)\Steam\steamapps\common\X4 Foundations Dev\libraries\\aiscripts.xsd" />
Prepareing New Script Files
Show
So all of the above works for the existing stuff but if you wanted to make your own new script from scratch in a mod you'll need to add a few lines. Thankfully this is pretty easy and you simply need to encapsulate your script code like so -

MD

Code: Select all

<?xml version="1.0" encoding="utf-8" ?>
<mdscript name="MyScriptName" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="md.xsd">
DO STUFF
</mdscript>
AISCRIPTS

Code: Select all

<?xml version="1.0" encoding="utf-8" ?>
<aiscript name="MyScriptName" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="aiscripts.xsd">
DO STUFF
</<aiscript>
Now the location does not have to be this basic, more advanced ways do this are too not copy any XSD into your mod instead set the schema location directly too the XSD you wish to use, most of these are stored in the libraries folder but you can also do a file search for *.xsd and then copy all of them anywhere you like and then set the locations from a custom path as the XSD file location plays no part in the game functionality they exist only for informational use whilst coding.
Extra: Online Based Database & VSCode Extentsions
Show
Online Database for your convenience that I keep updated as humanly possible.
I host a public URL for now which allows doing this via HTTP:// over the internet.
The website is pretty bland for now, I may move it onto an external host it is running simply on my machine at home right now. So If my PC is offline so will this be.


Useful Features provided as a side effect of hosting the libraries.
http://x4dynlib.access.ly/scriptproperties.html
http://x4dynlib.access.ly/jobeditor.html

In order to use this public URL for access to an online XSD library (you will still to enable code-complete in your IDE (editor of choice), any XSD locations simply use

Code: Select all

xsi:noNamespaceSchemaLocation="http://x4libonline.ddns.net\libraries\aiscripts.xsd"

Code: Select all

xsi:noNamespaceSchemaLocation="http://x4libonline.ddns.net\libraries\md.xsd"
Want to host your own?
  • Install IIS from control panel > programs > turn windows features on or off
  • Navigate too C:\inetpub\wwwroot and dump the extracted scriptproperties.html, jobeditor.html & libraries folder into it like so
  • Access in any web browser using 127.0.0.1\scriptproperties.html, e.t.c
    How to host that online public well that is on you, me I just used my routers built-in dynamic DNS with a No-ip.com free domain name.

VSCode Extentsions
https://github.com/Cgettys/X4CodeComplete - Suppliment extension provides very useful extra completion specific for X4 Foundations.
https://marketplace.visualstudio.com/it ... ohnson.xml - Suppliment to XML RED HAT, a few niche features.
https://marketplace.visualstudio.com/it ... scode-diff - Very fast two file differential compare.
OTHERS
You just do the same thing except you replace the XSD name with whatever one has the code complete and info for that kind of script.
Last edited by Misunderstood Wookie on Fri, 5. Feb 21, 09:00, edited 11 times in total.
*modified*
*X3 LiteCube User*
MOD GemFX Real Space Shaders
MOD Variety and Rebalance Overhaul Icon Pack
I lost my Hans and should not be flying Solo.
Image

User avatar
euclid
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 13298
Joined: Sun, 15. Feb 04, 20:12
x4

Re: [Info] Code Complete for Visual Studio Code

Post by euclid » Wed, 1. May 19, 12:27

Nice one, thank you! Link to this added to the sticky.

Cheers Euclid
"In any special doctrine of nature there can be only as much proper science as there is mathematics therein.”
- Immanuel Kant (1724-1804), Metaphysical Foundations of the Science of Nature, 4:470, 1786

Misunderstood Wookie
Posts: 377
Joined: Mon, 15. Mar 04, 08:07
x4

Re: [Info] Code Complete for Visual Studio Code

Post by Misunderstood Wookie » Sun, 12. Apr 20, 21:00

Updated OP
+ added my personal online repo for libraries and other useful functions.
*modified*
*X3 LiteCube User*
MOD GemFX Real Space Shaders
MOD Variety and Rebalance Overhaul Icon Pack
I lost my Hans and should not be flying Solo.
Image

Misunderstood Wookie
Posts: 377
Joined: Mon, 15. Mar 04, 08:07
x4

Re: [Tutorial] Code-Complete for Visual Studio Code

Post by Misunderstood Wookie » Mon, 8. Feb 21, 17:53

Updated Online lib repo URL
LIB runs Beta 4.0 Update 6 scripts.
*modified*
*X3 LiteCube User*
MOD GemFX Real Space Shaders
MOD Variety and Rebalance Overhaul Icon Pack
I lost my Hans and should not be flying Solo.
Image

Misunderstood Wookie
Posts: 377
Joined: Mon, 15. Mar 04, 08:07
x4

Re: [Tutorial] Code-Complete for Visual Studio Code

Post by Misunderstood Wookie » Wed, 17. Mar 21, 14:11

Online Lib Repo Should soon contain, 4.0 Stable and DLC 1 and DLC 2 scripts for use with modding.
*modified*
*X3 LiteCube User*
MOD GemFX Real Space Shaders
MOD Variety and Rebalance Overhaul Icon Pack
I lost my Hans and should not be flying Solo.
Image

Post Reply

Return to “X4: Foundations - Scripts and Modding”