[Info] Code complete scripting with Visual studio

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

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

Post Reply
Antaran
Posts: 166
Joined: Wed, 6. Nov 02, 20:31
x3tc

[Info] Code complete scripting with Visual studio

Post by Antaran » Thu, 13. Dec 18, 22:46

Hi,

I wanted to share a little information for those that are perhaps like me, new to x4 scripting by using visual studio. (Not sure if this information exists somewhere or if anyone needs it at all)
This will give you code complete for scripting

First thing you need to do is download Visual studio community edition, this is a free version, requires if memory serves a microsoft account (also free), see included link.
https://visualstudio.microsoft.com/vs/community/

After installing and running, for those that are not used to VS you start by creating a project from the "File->New->Project" (sc: CTRL+SHIFT+N)
This should give you a window for creating a new project that includes some details that really are not important for the work of creating X4 scripts, the window contains on the left side the project template group and in the center the project type, selecting something simple like "Windows Desktop" on the left side and "Console App" in the center should give you a pretty stripped down template.
Just write in a name for the project (f.e. X4Scripts) and have everything else on the default parameters, press ok. (For advanced stuff you could go into Source control and that way could check in your changes and keep a change history but lets not go there since it can be complicated stuff)
This should drop you back to the main VS window, on your right side you should see the "Solution explorer" window, with something like Solution 'X4Scripts' (1 project) and there below 'X4Scripts', then in there Dependencies (No need to worry about that since it is not used) and then Class1.cs (Select and delete this file since it is not used.)

New project window looks something like this.
New project

Now here comes the fun part, for code complete to work we need to have the cat files exported all .xsd files from the md and the aiscript directory into a location that can be pointed to.

Start by right clicking the project "X4Scripts" the second line in the "Solution Explorer" and click "Add->New Folder" and name it MdScripts, right click that folder and "Add->New Folder" and give that a name that you intend to name your mod in my case I will call it "Signal_Reader", right click the mod folder and "Add->New folder" and create a folder called "md" or "aiscript" depending on the type of script you are working with, in my case I will call it "md", this should give you a folder structure of "MdScripts->Signal_Reader->md", now right click the "Signal_Reader" folder and "Add->New Item"(sc: CTRL+SHIFT+A) select "XML file" and give it the name "content.xml", this is where you would insert all your content information (this is really not needed in there since you are mostly working with the script itself for code complete, but not bad to have in your project for future use), now right click the "md" folder and "Add->New Item" select "XML file" and give it the name of the script and add _MD or something to the back, giving the name for my example "Signal_Reader_MD.xml", so this is the file you would write your script in.

Small run down of what has been done.
Create a project something simple and clean up unused files and folders.
Add folder for MdScripts in the root of the project (this is where your mods will go)
Add a folder for your mod in the MdScripts folder with the name of your mod
Add a folder called md in your mod folder
Add a content.xml file to your mod folder (Optional)
Add a your_mod_MD.xml file to your mods md folder.

Your setup should be something like this.
Project file structure


Now comes the part where you add the file that will give you the code complete.
First you need to add the file md.xsd that is in your extracted X4 cat files folder, in there you should find a directory called md and in there a file called md.xsd, this file needs to be added to your mods md directory, easy way to do that is to right click the md folder for your mod in visual studio and click "Add->Existing Item"(sc: SHIFT+ALT+A), navigate to the md folder of where you extracted the x4 cat files and select the file md.xsd.
Next thing you need to do is to modify this file you just added md.xsd so it points to where the rest of the xsd files that got extraced from the cat files, this one is pretty much just a link to the actual md.xsd file that is located in the libraries folder where your cat files were extracted.
So right click the md.xsd file and click "View Code" (sc: Select it and press F7) there you should see a line that is pointing to a file called md.xsd in the libraries folder, something like this <xs:include schemaLocation="../libraries/md.xsd" /> replace the schemaLocation to the location of the libraries folder that you extracted from the cat files, f.e lets say you extracted the cat files to a folder on your C: drive called "c:\X4ExtratedCats" then you would use that for your line, giving you <xs:include schemaLocation="c:\X4ExtratedCatslibraries\libraries\md.xsd" /> (Notice that I am using \ instead of / since it is pointing to a hard location on the hard drive). Save your changes in the md.xsd file and this should give you code complete with visual studio.

This should make the md.xsd file look like this :

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:include schemaLocation="c:\X4ExtratedCatslibraries\libraries\md.xsd" />
</xs:schema>
For AI scripts you would do exactly the same, but use the aiscripts.xsd file from the aiscripts directory instead of the md one. Schemalocation would be the same but instead of pointing to md.xsd it would be aiscripts.xsd

Code: Select all

<xs:include schemaLocation="c:\X4ExtratedCatslibraries\libraries\aiscripts.xsd" />
This should give you code complete like this.
Code complete

Cyron43
Posts: 213
Joined: Sun, 6. Jan 19, 17:30
x4

Re: [Info] Code complete scripting with Visual studio

Post by Cyron43 » Fri, 11. Jan 19, 15:33

Hi, I'm a total noob regarding X4 modding. At some point you mention the "extracted X4 cat files folder". What is that and how do I extract those cat files??

cole2839
Posts: 108
Joined: Tue, 4. Dec 18, 01:43
x4

Re: [Info] Code complete scripting with Visual studio

Post by cole2839 » Sat, 12. Jan 19, 04:54

Cyron43 wrote:
Fri, 11. Jan 19, 15:33
Hi, I'm a total noob regarding X4 modding. At some point you mention the "extracted X4 cat files folder". What is that and how do I extract those cat files??
You use this tool to extract the CAT files in your game install directory.

https://www.egosoft.com/download/x_rebi ... wnload=589

The CAT files are in \Program Files (x86)\Steam\steamapps\common\X4 Foundations\

You need to create a BAT file with this code to extract the files. Make sure you add the folder called "unpacked" before you run the BAT file. Also put the XRCatTool.exe file you downloaded and BAT file in the \X4 foundations\ directory before running the BAT file.

Code: Select all

c:
cd "Program Files (x86)\Steam\SteamApps\common\X4 Foundations"
XRCatTool.exe -in 01.cat -in 02.cat -in 03.cat -in 04.cat -in 05.cat -in 06.cat -in 07.cat -in 08.cat -in 09.cat -out "c:\Program Files (x86)\Steam\SteamApps\common\X4 Foundations\unpacked"
pause

Cyron43
Posts: 213
Joined: Sun, 6. Jan 19, 17:30
x4

Re: [Info] Code complete scripting with Visual studio

Post by Cyron43 » Sat, 12. Jan 19, 14:39

Thank you very much. :)

User avatar
Hector0x
Posts: 994
Joined: Mon, 18. Nov 13, 18:03
x3tc

Re: [Info] Code complete scripting with Visual studio

Post by Hector0x » Fri, 27. Jan 23, 18:15

I'm still completely green and only know X3 scripting. Now i want to figure out X4's xml.

I managed to recreate code autocompletion by VS like shown in those screens of the op. All nice and dandy. And i can see myself getting used to it. But the problem is that i cannot find an overview of available script constants.
back in X3 they looked like this:

Code: Select all

[THIS]
[OWNER]
[SECTOR]
X3 Editor-2 had a nice list. And even if they didn't have a description i could guess the purpose and this worked well enough.

in X4 those script constants are apparently called 'script keywords'. I can see them in the code:

Code: Select all

this
this.ship.cluster
this.ship.warebasket.list
[class.station]
I know what they mean but i don't know where to look them up. I feel like they must be defined somewhere? VS doesn't provide autocompletion for them. They are an 'expression' type parameter which i must enter myself. Is this normal or did i fail to link some xml schema in my setup?
Are you supposed to guess that a certain script keyword might exist and then search vanilla scripts for hours? I must be missing something here.

temetvince
Posts: 228
Joined: Fri, 29. Jul 11, 20:58
x4

Re: [Info] Code complete scripting with Visual studio

Post by temetvince » Mon, 27. Feb 23, 08:13

Hector0x wrote:
Fri, 27. Jan 23, 18:15
I'm still completely green and only know X3 scripting. Now i want to figure out X4's xml.

I managed to recreate code autocompletion by VS like shown in those screens of the op. All nice and dandy. And i can see myself getting used to it. But the problem is that i cannot find an overview of available script constants.
back in X3 they looked like this:

Code: Select all

[THIS]
[OWNER]
[SECTOR]
X3 Editor-2 had a nice list. And even if they didn't have a description i could guess the purpose and this worked well enough.

in X4 those script constants are apparently called 'script keywords'. I can see them in the code:

Code: Select all

this
this.ship.cluster
this.ship.warebasket.list
[class.station]
I know what they mean but i don't know where to look them up. I feel like they must be defined somewhere? VS doesn't provide autocompletion for them. They are an 'expression' type parameter which i must enter myself. Is this normal or did i fail to link some xml schema in my setup?
Are you supposed to guess that a certain script keyword might exist and then search vanilla scripts for hours? I must be missing something here.
Hi Hector, good to run into you this way. I'm also trying to learn more about X4 scripting, it can be quite bewildering I feel for some of us x3 scripters. As far as I can tell, once you extract the cats then constants you seek are located in "libraries/scriptproperties.xml". I find folding the xml in visual studio code with CTRL + K CTRL + 0 to be helpful in getting an overview.

I found this by using astrogrep (or alternatively grepwin) to search the library folder for known script keywords which led me to scriptproperties.xml. I later found this thread helpful: viewtopic.php?f=181&t=432098, particuarly around how kuertee got started and their dev process, plus the python tip for running a server to serve scriptproperties.html!

Post Reply

Return to “X4: Foundations - Scripts and Modding”