Total War: WARHAMMER II

Total War: WARHAMMER II

38 ratings
TW:WH Script Mod: Creating Self-calling Scripts.
By IfThenOrElse
This is an adaptation of my older guide. This mod uses a script loading method designed by CA in the Tomb Kings patch & is more robust than previous versions.
   
Award
Favorite
Favorited
Unfavorite
Preface:
This is not an in-depth guide on scripting, but will cover enough to get you started. This guide assumes you understand the basics of Total War modding and are looking to delve into scripting.

As such, you should understand how to create and edit pack files and be willing to do your own research into scripting fundamentals.

This guide utilises a methodology introduced to the game during the Tomb Kings patch. If you would like to see an alternate version – check out this guide here.
Introduction:
Script mods for Total War are very powerful. They allow you to do a large array of specific commands at specific moments or ‘Events’.

Unfortunately, unlike the table edits of regular mods, scripts are entirely contained entities and so editing a vanilla script makes your mod completely incompatible with any other mod who also edits that script.

As such this guide is here to help you create your own custom script to maximise compatibility and reduce the risk of your mod breaking the game.
But Why?
  • Custom scripts massively reduce mod script conflicts by being self-contained.
  • Custom scripts massively reduce the risk of breaking vanilla scripts by being self-contained.
  • By being self-contained – your mod becomes more transparent – ensuring your changes are not hidden away amongst the masses of vanilla scripts.
Where do script files live in a pack file?
Total War script files are Lua script files and so have the .lua externsion. They are stored in a pack file under the script directory.

There are a number of sub folders in here and I would recommend you take the time to look through them – these should be a primary resource when looking at creating scripts. As of writing this guide – all vanilla scripts live in data.pack – however this could change in the future as CA adds new pack files to the game.



This guide will detail how to store your scripts later.
Editing a lua file.
Script editing is at its core, text editing. I would recommend you use a tool like Notepad++ (although the editor you use is ultimately up to you).

The advantage of using an editor like this over, say notepad is:
  • You can set the language to lua – which will recognise and colour code commands.
  • It can help you with correct formatting (tabulating).
  • Has nifty tools like line numbers, collapsing statements and MUCH more.

Click to expand.

With this tool you can edit vanilla scripts, or create your own from scratch.
Creating your new script file.
First things first, create your new file. Right click to create a new text file then, when renaming it do so as below.
[MODNAME]_script.lua
Note: It doesn’t matter where you do this, but I would recommend you do so in a new folder you set up specifically to hold working files for your mod.



With this done, remember the name of the file - this is important for the function we will write that will initialise your script.
Building your start function.
The game loading your script is only part of the solution - you will also need to ensure your script initialises itself. Consider script initialising like pressing the 'play' button on video stream.

To do this, you need to create a start function which has the same name as the script file you just made.


The above function allows you to run your script funtions either at the start of the campaign or each time the script loads - when you load into the campaign.

Be sure to carefully consider where you are calling your functions to avoid unexpected behaviour or unecessary script processing.
Loading your script into your pack file.
It is important to place your script in the correct directory within your pack file for the game to load it properly.

There are three possible directories you could place your script depending on when you want it to activate.

Your script is intended to run for Mortal Empires Only.

script > campaign > main_warhammer > mod > [MODNAME]_script.lua

Your script is intended to run for Vortex Campaign Only.

script > campaign > wh2_main_great_vortex > mod > [MODNAME]_script.lua

Your script is intended to run for both campaigns.

script > campaign > mod > [MODNAME]_script.lua
The rest of your script.
Alas, this is where the scope of this guide ends. Events, conditions and listeners is a topic for another guide.

However until that guide is released, you should take a look at the vanilla scripts and other mod scripts to give you a pointer in the right direction.
Other Notes:
-issues with this loading method appear to have been fixed in the Resurgence Update.
8 Comments
Autumnchain 3 Sep, 2021 @ 7:16am 
Does the script file have to be named according to the convention described or can it be named however a modder wishes as long as the start function matches the scripts filename?
Sir Draskar 31 Mar, 2021 @ 4:08am 
It is possible to alter tables via script?
For example, is it possible to double up all units recruitement cost or upkeep via script?
DudeChris33 10 Jun, 2020 @ 2:27pm 
ok but how do you get the lua script into those folders without editing the base game files?
phazer 26 Sep, 2018 @ 1:35pm 
Thanks.)
IfThenOrElse  [author] 26 Sep, 2018 @ 12:39pm 
Hey phazer, I'd recommend you pop over to the discord modding den. lots of great script references as well as modders to give advice! https://discord.gg/XY69bT
phazer 26 Sep, 2018 @ 11:29am 
Can I ask you where to get a referemce to all the functions callable from TW scripts, I really need one. Thanks in advance.
Nayran 1 Jun, 2018 @ 4:10am 
Thank you for this guide ^^
[PuB] Axlegolas 16 Apr, 2018 @ 11:22am 
thanks for this guide it helps a lot. but i have still a prob i can't solve. i would like to add ror units but they are not shown up in campaign, only in custom battle. so i searched the lua files for a script and found: wh_campaign_ror_recruitment.lua. i added my units in a copied version of this file but the units still doesn't apear in campaign mode.... do i have to implement my edited version somehow via required files or will my script override the original one automatically??