Interventions

Campaign interventions offer a mechanism for script to lock the UI and the progression of the game while the player is shown or taken through a sequence of scripted events. This sequence can be as short as a few seconds to trigger some advice, or as long as a cinematic or a scripted tour of a feature. If one intervention has control of the game, no other intervention may gain control until that intervention completes.

An intervention is constructed with a unique name, a trigger function (what it calls when triggered) and a priority cost. Once constructed, one or more conditions must be added to the intervention that tell it when to trigger. The intervention starts monitoring for these conditions when intervention:start is called. Should one of these conditions be met, the intervention is enqueued for triggering, and its priority cost is considered alongside that of any other interventions that also wish to trigger at this time. The intervention that is highest priority gets to trigger first - its cost is added to a cumulative cost counter, and other interventions are queued up behind it. Once the first intervention has finished the second is started, and so on, until the list of interventions that wish to trigger is exhausted or the cumulative cost exceeds a certain value (100). The trigger monitors of any interventions that didn't get to trigger are restarted so they can trigger again in future.

By default, the state of declared interventions is saved into the savegame. The system also supports transient interventions which are not saved in to the savegame - see Transient Interventions.

Interventions are useful for delivering advice or other scripted events which demand focus, such as mid-game narrative cutscenes. Without the intervention system, there would be a risk that two such pieces of script would trigger at the same time and play simultaneously.

Loaded in Campaign loaded in campaign
Back to top

Usage

An intervention may be created with intervention:new, after which functions on it may be called in the form showed below.

Example - Specification:

<intervention_object>:<function_name>(<args>)

Example - Creation and Usage:

local intervention_example = intervention:new(
    "example",
    60,
    function() trigger_example_intervention() end,
    true
)
intervention_example:set_min_advice_level(2)
Back to top

Configuration and Starting

Once created, an intervention may optionally configured with a variety of settings (see Configuration). One or more trigger conditions can be added with intervention:add_trigger_condition. Preconditions, which are tests an intervention must pass before it's allowed to start may also be added to an intervention with intervention:add_precondition. Preconditions are checked again prior to an intervention triggering.

When set up, an intervention may be started with intervention:start. This only needs to happen once during the life-cycle of a campaign game, for an intervention that was previously started will be restarted should the script save and load. This includes interventions that have triggered and shut down, so the preconditions of each intervention must be set up to ensure that it doesn't start back up on script load unless that is desired.

Once an intervention triggers, it is imperative that it finishes, which happens when intervention:complete or intervention:cancel are called. Until one of these function is called an intervention will lock the UI and the progress of the game. However, some functions are made available that wrap common intervention templates - see the functions list in the Intervention Behaviour Wrappers section. These will automatically end the intervention at the appropriate time.

Back to top

Transient Interventions

Interventions are persistent by default, which means that their details are saved into the savegame and that they are automatically re-established when the game is reloaded. Transient interventions are also supported, which are not saved into the savegame. These are intended for use for narrative events that have to pay heed to the intervention system but that are triggered in an inline fashion from other scripts.

Transient interventions can be created by setting the transient flag on the constructor function intervention:new. The campaign_manager also provides a fire-and-forget wrapper for creating transient interventions - see campaign_manager:trigger_transient_intervention.

Transient interventions, unlike persistent interventions, can share the same name (although this should be avoided if possible). Some advanced intervention functionality is not available to transient interventions, such as intervention:take_priority_over_intervention, intervention:set_turn_countdown_restart and other related functions.

Back to top

Output

The intervention system outputs extensive amount of information to the Lua - Interventions tab.

Back to top

Creation

intervention:new(string name, number cost, function callback, [boolean debug mode], [boolean is transient])

Creates a intervention object. This should happen in the root of the script somewhere, so that the object is declared and set up by the time the first tick happens so that it can be properly restarted from a savegame.

Parameters:

1

string

Unique name for the intervention.

2

number

Priority cost for the intervention. When an intervention triggers, all interventions that wish to trigger at the same time will be sorted by cost, and triggered in that order (with the cheapest first). Once the total cost of all interventions triggered in a sequence exceeds a certain value (100) no more interventions can trigger. This mechanism prevents the player from being overly spammed with advice.

3

function

Callback to call when the intervention gets to trigger.

4

boolean

optional, default value=false

Activates debug mode for this intervention, causing it to output more.

5

boolean

optional, default value=false

Sets this intervention to be transient. The details of transient interventions are not saved into the savegame.

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 1790

Back to top

Preconditions

If preconditions are added to an intervention, it will check these when it attempts to either start or trigger. If any of its preconditions return false, the intervention shuts down and will not trigger.

intervention:add_precondition(function precondition)

Adds a precondition function to the intervention. This function will be called by the intervention from time to time and should return true if the intervention is allowed to start or trigger, false otherwise. Should the precondition return false when the intervention calls it the intervention will shut down.
Multiple preconditions may be added to an intervention.

Parameters:

1

function

precondition

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 1869

intervention:add_advice_key_precondition(string advice key)

Precondition wrapper function that adds a precondition to the intervention that a particular advice key must not have been triggered. The intervention won't be able to start or trigger if the advice history reveals that the advice key has been triggered before. This is useful for interventions that trigger advice - by using this function to specify a precondition, an advice intervention may prevent itself from starting or triggering if the advice it intends to deliver has been heard before.

Parameters:

1

string

advice key

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 1883

intervention:add_precondition_unvisited_page(string help page name)

Precondition wrapper function that adds a precondition to the intervention that a particular help page must be unvisited. The intervention won't be able to start or trigger if the advice history reveals that the specified help page has been visited. This may be useful for low-priority advice scripts that don't wish to trigger if the help page on the advice topic has been seen.

Parameters:

1

string

help page name

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 1900

Back to top

Trigger Conditions

At least one trigger condition must have been added to an intervention with intervention:add_trigger_condition before it's started, otherwise it will never be able to trigger.

intervention:add_trigger_condition(string event name, function condition check)

Adds a trigger event and condition to the intervention. The supplied event is listened for and, when received, the supplied condition function is called, with the context of the received event as a single parameter. Should the condition function return true the trigger is satisfied and the intervention is enqueued for triggering.
Alternatively the value true may be specified in place of a condition function - in this case, the intervention is enqueued for triggering as soon as the specified event is received.

Parameters:

1

string

Event name to listen for.

2

function

Condition check to call when the event is received. Alternatively, true may be specified.

Returns:

  1. nil

Example:

in_mission_advice:add_trigger_condition(
    "ScriptEventFirstAttackEnemyMissionIssued",
    true
);

Example:

in_money:add_trigger_condition(
    "ScriptEventPlayerFactionTurnStart",
    function(context)        
        return context:faction():treasury() < 4000;
    end
);

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 1932

Back to top

Configuration

intervention:set_disregard_cost_when_triggering([boolean diregard cost])

Set this intervention to trigger regardless of cost. Interventions set to disregard cost will trigger ahead of interventions not set to disregard cost, even if the former's assigned cost is more than the latter's. Its cost is still counted towards the maximum cost per-session, however, so if an intervention that costs 80 points and is set to disregard cost triggers, and another that costs 30 point and is not set to disregard cost is queued up behind it, the second will not be able to fire as the maximum cost is exceeded.
If triggered at the same time as another intervention which is also set to disregard cost then they will trigger in order of cost priority. Both will always trigger, though, even if their combined cost is more than the maximum session allowance.

Parameters:

1

boolean

optional, default value=true

diregard cost

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 1996

intervention:set_suppress_pause_before_triggering([boolean suppress pause])

By default, interventions wait for a short period before triggering. Use this function to suppress this wait behaviour.

Parameters:

1

boolean

optional, default value=true

suppress pause

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2009

intervention:set_allow_when_advice_disabled([boolean allow intervention])

If advice has been disabled with campaign_manager:set_advice_enabled then by default interventions won't attempt to play. Use this function to modify this behaviour for this intervention if required.

Parameters:

1

boolean

optional, default value=true

allow intervention

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2021

intervention:set_min_advice_level(number min advice level)

Sets the minimum player advice level setting at which this intervention will be allowed to trigger. By default this value is 0, so the intervention will trigger regardless of advice level. Valid minimum advice levels are:
0Minimal advice - will trigger when advice level is set to minimal, low or high
1Low advice - will trigger when advice level is set to low or high
2High advice - will only trigger when the advice level is set to high

Parameters:

1

number

min advice level

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2033

intervention:set_player_turn_only([boolean player turn only])

Sets whether or not this intervention can only happen on the player's turn. By default, interventions can only trigger if it's the player's turn. Use this function to allow interventions to trigger in the end-turn sequence, which is useful for advice triggering over diplomacy or battles.
If an intervention is set to trigger on just the player's turn and it is trigger during the end-turn sequence, it will cancel itself and then trigger again when the player's turn starts.

Parameters:

1

boolean

optional, default value=true

player turn only

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2046

intervention:set_min_turn(number minimum turn)

Sets the minimum number of turns since either the start of the campaign or when the advice history was last reset before this intervention is eligible to trigger. This is useful for ensuring non-essential advice is spaced out at the start of the campaign.

Parameters:

1

number

minimum turn

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2059

intervention:get_min_turn()

Returns the set minimum turn value.

Returns:

  1. number minimum turn

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2072

intervention:set_wait_for_battle_complete([boolean wait for battle])

By default, interventions will wait for a battle sequence to complete before triggering. A battle sequence is from when the pre-battle panel opens, to when the camera returns to its gameplay position after any battle panels have closed. Use this function to allow interventions to trigger during battle sequences if required. This is useful for delivering pre-battle or post-battle advice.

Parameters:

1

boolean

optional, default value=true

wait for battle

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2081

intervention:set_wait_for_event_dismissed([boolean wait for events])

By default, interventions will wait for any open event panels to be dismissed before triggering. Use this function to suppress this behaviour, if necessary.

Parameters:

1

boolean

optional, default value=true

wait for events

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2093

intervention:set_wait_for_dilemma([boolean wait for dilemmas])

By default, interventions will wait for open dilemmas to be dismissed before triggering. Use this function to suppress this behaviour, if necessary. This should only be used in very specific circumstances.

Parameters:

1

boolean

optional, default value=true

wait for dilemmas

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2105

intervention:set_wait_for_fullscreen_panel_dismissed([boolean wait for panels])

By default, interventions will wait for any open fullscreen panels (technology, diplomacy, recruitment etc) to be dismissed before triggering. Use this function to suppress this behaviour, if necessary.

Parameters:

1

boolean

optional, default value=true

wait for panels

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2118

intervention:add_whitelist_event_type(string event type)

Adds an event type to be whitelisted if intervention:whitelist_events is called.

Parameters:

1

string

event type

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2130

intervention:set_completion_callback(function callback)

Adds a callback to call when the intervention has completed.

Parameters:

1

function

callback

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2143

intervention:get_turn_last_triggered()

Returns the turn number of which this intervention last triggered in this campaign. If this intervention has never triggered then -1 is returned.

Returns:

  1. number turn last triggered

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2151

intervention:has_ever_triggered()

Returns whether this intervention has ever triggered in this campaign.

Returns:

  1. boolean has ever triggered

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2159

Back to top

Restart Callback

Restart events and checks are listened for if the intervention isn't running, assuming it has previously been started. If a restart callback event is received and the check returns true, then the intervention is restarted. Multiple restart callbacks may be added to one intervention.

intervention:add_restart_callback(string event name, function check)

Adds a restart event and conditional check. Should the event be received and the conditional check return true, then the intervention will restart. Transient interventions may not have restart callbacks added.

Parameters:

1

string

Event name to listen for.

2

function

Conditional check to test when event is received. This should be a function that returns a boolean value, and it will be passed the context of the event listened for as a single parameter. Alternatively, true may be specified in place of a function, in which case the intervention is restarted as soon as the event is received.

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2181

Back to top

Restart on Turn Countdown

intervention:set_turn_countdown_restart(number turns)

If a turn countdown restart number is set, the intervention will attempt to restart the given number of turns after stopping. This countdown also applies if the intervention fails to start. Transient interventions do not support turn countdown restarts.

Parameters:

1

number

turns

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2229

intervention:register_shared_turn_countdown_intervention(string intervention name)

Registers that this intervention shares a turn countdown with another. If this is set on an intervention then when it restarts its turn countdown it will instruct the other intervention to restart its turn countdown also. This can be useful for advice interventions that share a common purpose.
Note that interventions that take or cede priority to another (see Taking and Ceding Priority) cannot share turn countdowns with other interventions.

Parameters:

1

string

Name of intervention that this intervention shares its turn countdown with. This only needs to be called on one of the two interventions.

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2254

Back to top

Taking and Ceding Priority

Interventions can set to explicitly take or cede priority to another. This allows client scripts to ensure that one intervention must stop or cannot run when another is started, and for the second to notify the first when it stops so that the first can eventually start.

intervention:take_priority_over_intervention(string intervention name)

Registers that this intervention takes priority over another intervention with the supplied name, so that they cannot run at the same time. If the supplied intervention attempts to start and this intervention is already started, the supplied intervention will fail to start. If this intervention starts and the supplied intervention has already started, the supplied intervention will be stopped.
Furthermore, when this intervention stops it will notify the supplied intervention, which will then start.
Only persistent interventions can take priority over another. Transient interventions do not support this mechanism.

Parameters:

1

string

Name of intervention to take priority over.

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2292

intervention:give_priority_to_intervention(string intervention name)

Registers that this intervention cedes priority to another intervention with the supplied name, so that they cannot run at the same time. This is the reverse of intervention:take_priority_over_intervention. If the supplied intervention starts and this intervention is already started, this intervention will be stopped. If this intervention starts and the supplied intervention has already started, this intervention will not start.
Furthermore, when the supplied intervention stops it will notify this intervention, which will then start.
Only persistent interventions can take priority over another. Transient interventions do not support this mechanism.

Parameters:

1

string

Name of intervention to give priority to.

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2323

Back to top

Starting

intervention:start()

Starts the intervention. An intervention must be started in order to trigger. This only needs to be called once per-intervention through the lifetime of a campaign - if an intervention is started, and the campaign is saved and loaded, the intervention will automatically be restarted from the savegame, even if it's triggered and stopped.

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2361

Back to top

Whitelisting Events

intervention:whitelist_events()

Perform the whitelisting of event types that have been registered with intervention:add_whitelist_event_type. This is intended to be called while the intervention is actually running to make pending events display suddenly.

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2569

Back to top

Completion

intervention:complete()

Completes the intervention when it's running. This function (or intervention:cancel) must be called at some point after the intervention has been triggered, as they are the only way the intervention will end and relinquish control of the game. The wrapper functions intervention:play_advice_for_intervention, intervention:scroll_camera_to_character_for_intervention and intervention:scroll_camera_to_settlement_for_intervention all call this function internally so if using one of those to control the behaviour of the intervention when triggered then this function does not need to be called.

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2805

intervention:cancel()

Completes the intervention without stopping its listeners. This is useful if an intervention decides it doesn't want to trigger after all after intervention:start has been called.

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2833

Back to top

Intervention Behaviour Wrappers

Call one of the functions in this section when an intervention is triggered to have it behave in a standardised manner, showing advice, infotext, a mission, optionally scrolling to a target and completing where appropriate.

intervention:scroll_camera_to_character_for_intervention(
  number
cqi,
  string
advice key,
  [table
infotext],
  [mission_manager
mission],
  [number
duration],
  [function
scroll callback],
  [function
continuation callback]
)

Scrolls the camera to a character during an intervention, showing advice and optionally showing infotext and a mission. This should only be called while this intervention is actually running after having been triggered.

Parameters:

1

number

Character cqi.

2

string

Advice key.

3

table

optional, default value=nil

Table of string infotext keys.

4

mission_manager

optional, default value=nil

Mission manager of mission to trigger, if any.

5

number

optional, default value=3

Duration of camera scroll in seconds.

6

function

optional, default value=nil

Callback to call when the camera movement is complete.

7

function

optional, default value=nil

If supplied, this callback will be called when the intervention would usually complete. It will be passed this intervention object as a single argument, and takes on the responsibility for calling intervention:complete to relinquish control.

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 2973

intervention:scroll_camera_to_settlement_for_intervention(
  string
region key,
  string
advice key,
  [table
infotext],
  [mission_manager
mission],
  [number
duration],
  [function
scroll callback],
  [function
continuation callback]
)

Scrolls the camera to a settlement during an intervention, showing advice and optionally showing infotext and a mission. This should only be called while this intervention is actually running after having been triggered.

Parameters:

1

string

Region key.

2

string

Advice key.

3

table

optional, default value=nil

Table of string infotext keys.

4

mission_manager

optional, default value=nil

Mission manager of mission to trigger, if any.

5

number

optional, default value=3

Duration of camera scroll in seconds.

6

function

optional, default value=nil

Callback to call when the camera movement is complete.

7

function

optional, default value=nil

If supplied, this callback will be called when the intervention would usually complete. It will be passed this intervention object as a single argument, and takes on the responsibility for calling intervention:complete to relinquish control.

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 3021

intervention:scroll_camera_for_intervention(
  string
region key,
  number
x,
  number
y,
  string
advice key,
  [table
infotext],
  [mission_manager
mission],
  [number
duration],
  [function
scroll callback],
  [function
continuation callback]
)

Scrolls the camera to a supplied position on the campaign map during an intervention, showing advice and optionally showing infotext and a mission. This should only be called while this intervention is actually running after having been triggered.

Parameters:

1

string

Target region key. If a region is supplied here the shroud over it will be lifted while the intervention is playing.

2

number

Display x co-ordinate of position to scroll to.

3

number

Display y co-ordinate of position to scroll to.

4

string

Advice key.

5

table

optional, default value=nil

Table of string infotext keys.

6

mission_manager

optional, default value=nil

Mission manager of mission to trigger, if any.

7

number

optional, default value=3

Duration of camera scroll in seconds.

8

function

optional, default value=nil

Callback to call when the camera movement is complete.

9

function

optional, default value=nil

If supplied, this callback will be called when the intervention would usually complete. It will be passed this intervention object as a single argument, and takes on the responsibility for calling intervention:complete to relinquish control.

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 3059

intervention:play_advice_for_intervention(
  
string advice key,
  [table
infotext],
  [mission_manager
mission],
  [number
mission delay],
  [function
continuation callback]
)

Shows advice and optionally some infotext and a mission. This should only be called while this intervention is actually running after having been triggered.

Parameters:

1

string

Advice key.

2

table

optional, default value=nil

Table of string infotext keys.

3

mission_manager

optional, default value=nil

Mission manager of mission to trigger, if any.

4

number

optional, default value=2

Delay in seconds after triggering intervention before triggering mission.

5

function

optional, default value=nil

If supplied, this callback will be called when the intervention would usually complete. It will be passed this intervention object as a single argument, and takes on the responsibility for calling intervention:complete to relinquish control.

Returns:

  1. nil

defined in ../working_data/script/_lib/lib_campaign_intervention.lua, line 3359

Last updated 07/02/21 06:39:14