Total War: WARHAMMER II

Total War: WARHAMMER II

Not enough ratings
TW:WH Script Mod: Scripts & Save States
By IfThenOrElse
This guide informs you on how scripts interract with the game across save states. Highlights problems that can occur for scripters when a player might reload a previous turn and how you can overcome these issues.

This is an important guide if you are using variables to track progress in your script.
   
Award
Favorite
Favorited
Unfavorite
Preface:
This is not an in-depth guide on scripting, but focuses in on a specific aspect of scripting. This guide assumes you understand the basics of Total War modding and are looking to delve further 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.
Introduction:
With TW:WH it is important to know that when you save your campaign - it doesn't save your script progress. This means each time you load up your game, the game scripts will start from scratch.

A good analogy is like an old CD player. Each time you turn it on, the CD will always start from track 1 - regardless of how far into that album you got the last time you listened to it.
Lets have an example:
Lets say you create a script that spawns an Empire army after you have sacked 5 settlements and use a varaible to track the number of settlements you have sacked.

Disclaimer: not real code!
counterVariable == 0 event
if settlement is empire
if counterVaraible <5 then
counterVaraible +1
elseif counterVaraible =5 then
sapwn killer army
endif
endif
endevent[/code]

You test your mod - and on the 5th sack - the army spawns as expected. Hooray!

Let's be a bit more destructive with our testing to highlight an issue.

This time:
  • You sack four towns then save the game.
  • You quit the game then load back in.
  • You sack the 5th town and nothing happens!

Sad times!
What's going on?
The problem we have here is that when we re-load the game, the script starts over again from scratch. So even though the player has sacked four towns, the script thinks the player has not sacked any.

In the above example, the script isn't broken - if the player goes on to sack 5 settlemnts in a single session the army will spawn.

The problem is, we didn't tell the script when it loaded that 4 settements had already been sacked.

Fear not, there is a way we can do this.
Saving & Loading Game callbacks:
Creative Assembly were clever folks and created a way for scripts to remember the value of their varaibles across save states.

cm:add_saving_game_callback() cm:add_loading_game_callback()

The former creates a "callback" when the game is saving and the latter references that callback when the game is loading.

What these allow scripters to do is save the value of necessary variables when a game is saved, then recall those values when a game is loaded.

Let's take a look at how we can use these commands in more detail.
The simple solution:
variableCount = 0 cm:add_saving_game_callback( function(context) output("===SAVING===") cm:save_named_value("variableCount", variableCount, context); end ); cm:add_loading_game_callback( function(context) output("===LOADING===") variableCount = cm:load_named_value("variableCount", 0, context); end );

That right there is all you need to ensure that the script will always know the true value of variableCount. But lets delve into it and figure out what is happening.

cm:save_named_value("variableCount", variableCount, context);
This saves a named value "variableCount", assigns it the value of the variable varaibleCount and references it to the current save game callback.

variableCount = cm:load_named_value("variableCount", 0, context);
This changes the value of variable count from 0 to the value stored for the named value "varaibleCount" that is attached to the save callback created earlier.
Useful script references:
I hope that this guide has givben you enough info to use this method to help your scripts maintain their values across saves.

This method of saving/ loading script variables is a straight up imitation of that used by CA in their vanilla scripts.

If you need a reference to see how it works I recommend checking out the following script files:
  • wh_dlc05_wood_elves.lua
  • wh_campaign_moon_mechanic.lua
  • wh2_dlc09_books_of_nagash.lua
  • wh_grudges.lua
Notes:
  • This method of varaible saving will NOT work if your script is an export_helpe_xxx.lua script. Those scripts load up after the loading event and as such - will never load saved values.
  • This is NOT an issue when using the new script loading method explained here.
  • Crynsos Modding Framework loads all scripts before the gameload event has occured but doesn't fire the main function of the script until after, so as long as the load callback is registered outside of a function it should function correctly.
2 Comments
James Howlett 15 Jul, 2023 @ 11:18pm 
Thanks a lot
Ciaphas Cain 21 Jun, 2021 @ 4:33pm 
Disclaimer: not real code!
:'D
love it haha