Workshop Link http://steamcommunity.com/sharedfiles/filedetails/?id=617731309
This mod adds a light sword to xcom, inspired by star wars, and the sword has its own stats, different from other weapons. This sword has more armor penetration than the best xcom2 sword, wich makes it a clear improvement(The creation of this mod was for fun and for experimentation, it was never the goal to actually do a balanced weapon).
On this page ill explain how it works, and how can you create your own weapon, it doesnt have to be a sword, it can be ANY weapon of the game.
Setting up your mod
First of all, you have to download the Xcom2 development tools in steam, wich are under the software category on your library. You need to have xcom 2 installed to be able to install them. After the 66 gigabyte install its finished, then you will have an special version of xcom from the normal game, this one its unpacked and has all the scripts and data needed to change them, alongside the full unreal engine editor, albeit its the modified Xcom2 version. Follow the quickstart documentation at \XCOM 2 SDK\Documentation That one will teach you how to setup your visual studio and create the project. The WeaponMod template adds a machinegun to the game, as a new type of weapon. We are going to ignore it and create our own sword.
Explanation of WTF is the stuff in the WeaponMod project
That project its an example on how to add a rifle, with custom models. It has several parts. Under the Content folder you have 2 .upk files, you can ignore the ModShaderCache one, that one its automatically generated and not even needed. But the other one, its the one that has the assets for the gun. In the config folder you have some edits to the global configuration projects. If you add something here, when you add the mod it will be loaded by the engine. So if your mod its only config file edits, you can do those edits here and they will be added. The default config files in the weaponmod project just "link" to the WeaponMod mod so its identified. The meat of the mod its the 2 script files, under Src/WeaponMod/Classes. The X2DownloadableContentInfo_ExampleWeapon.uc file its the information of the mod, it can be called on a few different events, but the example uses OnLoadedSavedGame(), where it adds the weapon to the HQ inventory. The most important code its this
//Make the changes to the HQ state. Here we add items to the HQ's inventory
ItemTemplate = ItemMgr.FindItemTemplate('ModWeapon_CV');
//Instantiate a new item state object using the template.
ItemState = ItemTemplate.CreateInstanceFromTemplate(NewGameState);
NewGameState.AddStateObject(ItemState);
//Add the newly created item to the HQ inventory
NewXComHQState.AddItemToHQInventory(ItemState); `
This code takes care of finding the Template for the ModWeapon(a template contains the data relevant to a item, like the mesh it uses, or the damage, its what we are going to edit) And then adds it to the HQ inventory.
You could add an item only when a campaign is started by doing that code in the InstallNewCampaign function.
The X2Item_ModExample_Weapon.uc its the one that takes care of defining the weapon template than then gets created by the game
The CreateTemplates function returns the templates on that file, there is no need to have one of those files per item, you can have as many as you want in one file. Xcom2 has a file with most of the weapons in a single place. The template for the gun its created by the function CreateTemplate_ModWeapon_Conventional() The most interesting thing on that function its
`CREATE_X2TEMPLATE(class'X2WeaponTemplate', Template, 'ModWeapon_CV');
That its a macro in unrealscript, and it defines the template name and type. The name should be unique, i havent tested what happens if you override other template. As you can see, the name(3rd parameter) its the same we use in the other file to get the item template to then add to inventory. That name its very important and will be the main way of referring to the weapon we are adding.
After it, there is a few variables wich should be self explanatory, as they define the variables and habilities the weapon has, like Reload (yes, reload its a weapon specific hability) as you can se at
// Enhancements we're making to base
//=====================================================================
Template.BaseDamage.Damage += 2; //Boost damage
Template.CritChance += 5; //Boost crit
Template.Abilities.AddItem('Suppression'); //This weapon confers suppression
Template.GameArchetype = "WP_ModExampleWeapon_CV.WP_ModExampleWeapon_CV"; //Use a custom archetype
//=====================================================================
The gun has +2 damage over the basic assault rifle, +5 crit chance, adds suppression, and uses a different archetype. The GameArchetype its what defines wich mesh, animations, or effects the weapon has.
Editing assets
The .upk packages are impossible to open with anything that isnt Unreal Engine 3. And if you try to open this ones with UDK, for example, they wont probably work. You need to open them from the actual XCom version of unreal engine. To launch it, you have to go to TOOLS on visual studio, and then XCOM 2 Editor. From the editor content browser, you need to open the package, its the little folder icon. The package path can be found by clicking on the package on visual studio, and at the right it should say the path of it. That ones are the one that you can edit. After opening the package, you can browse the assets inside, alongside ALL xcom2 assets. From here you can import meshes, or change the archetypes of a weapon.
Customizing the weapon, adding a sword instead of a rifle
First of all, we are going to delete everything that is inside the CreateTemplate_ModWeapon_Conventional() function. We are going to grab the template configuration from the basic XCOM sword. For that we need to find where its that template defined, wich is X2Item_DefaultWeapons.uc, under XCOM 2 SDK/Development/Src/XComGame. That file has most of the weapons in the game, alongside some things like the Gremlins. The basic sword its defined at the function CreateTemplate_Sword_Conventional() (see,pretty much the same as the mod one) Just copypaste everything inside that function into the mod function. After that, we have to change a few things: the CREATE_X2TEMPLATE has "Sword_CV" as name, change it to the original, wich is ModWeapon_CV. Also, if you try to compile it, it will give a compile error as it cant find the ALLCAPS_CONFIGURATION_CONSTANTS . To let it find it, you need to switch those default.ALLCAPS for class'X2Item_DefaultWeapons'.default.ALLCAPS Right now, you should have a clone of the default sword, just with a different template name and a different entry on the inventory. Of course, we want to change its stats, so lets do it. Just before the return, add:
//minus 1 damage
Template.BaseDamage.Damage -= 1;
//plus 1 shred armor
Template.BaseDamage.Shred += 1;
//plus 10 critchance
Template.CritChance+= 10;
This changes the weapon a bit, first, it lowers the damage, and then it adds shred and crit chance. You could say this could work as some kind of specialist crit weapon, or as an anti armor sword, but it does less damage to normal enemies. You should be able to try this, for that, try go to BUILD -> Build solution, wich will build the scripts. IT should go fine, if it doesnt, read this again and take a look at the source code for the mod. After the project its conpiled fine, go to Debug, and Start Debugging. A xcom 2 mod launcher should appear, so enable your WeaponMod mod. After you launch xcom, try going to Strategy, and load Debug Strategy Start. This will give you a debug environment. After loading, cancel the cinematic asap, and go see your soldiers loadout. Go to a ranger, and check the swords. A extra sword should appear.
Changing the weapon graphics
Now that we have a new weapon in the game, we have to give it separate graphics. For that, we need to see how the original weapon is. Go to the XCom2 editor, and search for the archetype of the weapon you copypaste, in this case being WP_Sword_CV . Just search for sword in the content browser and it should appear. Try to find the Skeletal Mesh of your weapon, and when you do, export it to FBX. Then import it in your favourite 3d software, and just switch the mesh for another one, but keeping the bones as they are. When you have your new mesh, export it into FBX, and then import it on the editor. Load it into the mod package (wich you should load as explained above) and check that it has imported properly. Your mesh and the original mesh should be similar in size and orientation, plus having the same bones. Click the Socket Editor in the original mesh AnimSet Editor, its a red button near the top left corner, and inside it, click the "copy sockets" button. Then, in your mod asset, go and paste those sockets in the same Socket Editor window. You can move them a bit if they dont align exactly, but have care, as those are to attach particle effects and animations. Once you have your mesh properly set up, with materials and everything it has to have, you need to create a modified archetype for your weapon. Archetypes are like a configuration file, they are made of configuration variables for a given script. Just find the archetype for the weapon you are basing on and make a copy that its in your package. At last, you change the mesh by going into the archetype properties -> Weapon-> Skeletal Mesh. and changing that for your new mesh. In your script file, where you define the weapon properties, you need to change the archetype path so it links to the new one.
Template.GameArchetype = "WP_ModExampleWeapon_CV.My_Weapon_Archetype;
You can also get the path by rightclicking the archetype in the unreal editor and then clicking "Get full path", it gives you the path like above but with an extra class'something'. just delete that and leave only the part inside the " ".
Finishing
Congratulations, you should have a custom weapon implemented into the game, but now you need to actually make the mod not named ExampleWeapon or WeaponMod, but the name you want. For that, the best way its to create a new empty project, with the name you want, and copy the files from the one you have made. BUT, every folder, every instance of ExampleWeapon has to be changed to the name you want, even inside the project file itself, and the configuration files too. Once everything here its right, you can upload the mod into the workshop by clicking TOOLS -> Publish mod in visual studio. Right now im working to see how i can add custom particle effects to the weapons, like making the lightsaber glow, also, please report any problems or errors in this page into the github issues.