Skip to content

Variables

udkudk edited this page Jul 4, 2026 · 23 revisions

Table of Contents



This Wiki page was last updated for $$\textcolor{aqua}{Stellaris}$$ version v4.4.*



1. Wiki Section

See https://stellaris.paradoxwikis.com/Variables

2. From Stellaris Game Files

2.1. common/scripted_variables/00_scripted_variables.txt

Add global variables here (common/scripted_variables/) that will be available in all script files.

@something = 42

They can be overridden in each normal script file.

2.2. events/000_how_to_use_variables_in_script.txt

2.2.1. THE GRAND VARIABLES GUIDE

As of version 3.1, you can now do fancy things with variables in script. This is a short write-up of what exactly you can do. (For exact formatting of effects and triggers mentioned, see logs/scripting_documentation)

2.2.2. Checking Variable Values

  • When you are using the value of a variable, you can now use dot scoping. E.g. value = owner.capital_scope.my_variable
  • You can also directly refer to the value of a trigger, e.g. value = trigger:pop_amount
  • A combination of the two is also possible: value = owner.capital_scope.trigger:pop_amount
  • Similarly, you can check the value of modifiers applying to this scope: value = modifier:pop_citizen_happiness
  • And you can use script values defined in common/script_values via value:my_value
  • This works almost everywhere where you are using variables. The exception is in the basic variable effects and triggers where you are specifying which variable to check or change.

2.2.3. Setting Variables

You can make a variable out of various numbers. The easiest is set_variable and change_variable, but you can also do:

  • get_galaxy_setup_value: galaxy setup options
  • export_trigger_value_to_variable: the value of a trigger (e.g. number of pops in the empire). Should work for all triggers that are comparing a single numerical value. For triggers with , you can specify parameters =
  • export_modifier_to_variable: the sum of the specific modifier applying to this scope, e.g. the amount of pop_citizen_happiness that a pop is gaining from all sources (including any country and planet modifiers).
  • export_resource_income_to_variable, export_resource_stockpile_to_variable: country's monthly income or current stockpile of a resource

2.2.4. Manipulating Variables

Once the variable is set, you can then alter it with standard mathematical operations: change_variable (addition, subtraction), multiply/divide/modulo_variable, etc.

Rounding has several options: round_variable will round to the closest integer, floor/ceiling_variable will round it down or up respectively. round_variable_to_closest will let you round to a multiple of the specified value

In all of these effects except round/floor/ceiling_variable, you can add/multiply/whatever the variable by another one, with dot scoping and referring to triggers allowed. E.g.:

multiply_variable = {
    which = my_var #must be directly referring to a variable
    value = fromfromfrom.owner.trigger:pop_amount
}

Once you are done with a variable, you can clear it using clear_variable.

Also worth noting: if you need to manipulate a variable in a trigger, you can use the check_variable_arithmetic trigger (which now supports an unlimited number of add/subtract/divide/multiply/modulo operations)

2.2.5. Using Variables - The Wheres

We can now use variables in a lot of different places:

  • Any trigger that is comparing a single number, including ones with . E.g. pop_amount > my_variable, intel = { who = from value < trigger:pop_amount }
  • Any effect using a single number, including ones with . E.g. add_experience = my_variable.
  • The count parameter of while loops!
  • Certain effects let you input variables for various reasons. E.g. multiplier on add_modifier (the modifier's bonuses will be multiplied by the variable), mult on add_resource (multiplies all resources granted by that effect)
  • Triggered resource tables can take a multiplier = <variable>:
    resources = {
        category = planet_buildings
        cost = {
            trigger = { <triggers> }
            minerals = 100
            multiplier = my_var/trigger:pop_amount
        }
    }
  • MTTH/AI Chance modifiers:
    ai_chance = {
        factor = 1
        modifier = {
            add/factor = my_var/trigger:pop_amount
            is_variable_set = my_var
        }
    }
  • Triggered modifiers:
    triggered_pop_group_modifier = {
        potential = { stuff }
        modifier = { stuff }
        mult = value:my_value
    }
    
  • Ordered script lists: scope to the country with the highest (or 3rd highest, or lowest) value for a variable or trigger.
  • In locs: if you refer to [This.my_variable], it will print the value of the variable, so long as the variable is set.

3. From Discord (Stellaris Modding Den)

See Discord > Stellaris Modding Den

3.1. Using Scripted Variables & Inline Scripts for Mod Compatibility:

Source: This Post

Convention is 1 for a present mod, 0 for an absent mod.

These variables can be used to create content conditional on other mods as follows (credit to TTFTCUTS for this invention):

  1. in your Target file to use Conditional Code:

    inline script = {
        script = example_conditional_script
        CASE = @example_mod_compat_var
    }
  2. in common/inline_scripts/example_conditional_script:

    inline_script = example_conditional_script_$CASE$
  3. in common/inline_scripts/example_conditional_script_1:

    script (possibly blank) to include if the mod is present
  4. in common/inline_scripts/example_conditional_script_0:

    script (possibly blank) to include if the mod is absent

More complicated logic is also possible:

CASE = @[1-(example_mod_compat_var * example_mod_compat_var_2)]

would implement case 1 of neither of two mods was present and case 0 otherwise, for example.

Some mods use flexible generic conditional scripts which also take a CONTENTS parameter containing the script to implement in case 1;

Scripted Trigger undercoat provides one such set of generic conditional scripts in the stu subfolder.

You can access a Community Maintained Database of Scripted Variables used by various mods for Compatibility purposes at https://docs.google.com/spreadsheets/d/1wbKhAVczL0By2F2D9dDy5WC-do3XqguNuj-dms9vzIo/edit?gid=0#gid=0

4. From by Github Contributors to the Project

4.1. Execution Order of Global and Local Variables and Overwriting them

Global Variables (common/scripted_variables/*) gets loaded while Stellaris is initializing, before anything else. This allows usage of Inline Script + Global Variable combination for mod compatibility (See: 3.1. Using Scripted Variables & Inline Scripts for Mod Compatibility:)

Global Variables load based on FIOS (First In, Only Served) and the order is based on ASCII). To overwrite them, your File name should be changed to load before the original file.

LVAR's gets loaded while their relevant game file gets loaded. If a LVAR's name is same as an existing GVAR, it temporarily overwrites it for that file only.

You can only overwrite LVAR's via a Full File Overwrite. Your file must have the exact same name, and load after the Vanilla/Mod File, which you can do by putting your mod below in Mod Order in Paradox Launcher.

Related Triggers & Effects & Modifiers etc

Type Code X Entry Field (If Exists) Description
Localisation [<Scope>.<variable_name>] Getting value of a Variable in specified Scope
Localisation $@<global_scripted_variable_name>$ Getting value of a Global Scripted Variable (common/scripted_variables)
Trigger is_variable_set = <variable_name> Checks if the specified variable is set on the current scope. Use to avoid unset variables errors
Trigger check_variable = { which = <variable> value >=< <float>/<variable>/<scope.variable>/trigger:<trigger> } Checks a variable for the country/leader/planet/system/fleet...
Trigger check_variable_arithmetic = {
which = <variable_name>
add/subtract/multiply/divide/modulo = <float>/<variable>/<scope.variable>/trigger:<trigger>
  (note: this line can be repeated as many times as desired)
value <=> <float>/<variable>/<scope.variable>/trigger:<trigger>
  (the value to compare against)
}
Checks a variable for the scope if a certain amount of arithmetic is done to it (note: the variable's value is not changed by this trigger)
Effect set_variable = { which = <variable_name> value = <float>/<variable>/<scope.variable>/trigger:<trigger> } Sets or creates an arbitrarily-named variable with a specific value in the current scope
Effect set_variable_to_random_value = { which = <variable_name> min = -100 max = 100 rounded = yes/no } Sets a variable to a random value within the specified bounds.
Effect clear_variable = <variable_name> Clears a previously-set variable from the game.
Effect change_variable = { which = <variable_name> value = <float>/<variable>/<scope.variable>/trigger:<trigger> } Increments a previously-set variable by a specific amount
Effect add_variable = { which = <variable_name> value = <float>/<variable>/<scope.variable>/trigger:<trigger> } Increments a previously-set variable by a specific amount
Effect subtract_variable = { which = <variable_name> value = <float>/<variable>/<scope.variable>/trigger:<trigger> } Decrements a previously-set variable by a specific amount
Effect multiply_variable = { which = <variable_name> value = <float>/<variable>/<scope.variable>/trigger:<trigger> } Multiplies a previously-set variable by a specific amount
Effect divide_variable = { which = <variable_name> value = <float>/<variable>/<scope.variable>/trigger:<trigger> } Divides a previously-set variable by a specific amount
Effect modulo_variable = { which = <variable_name> value = <float>/<variable>/<scope.variable>/trigger:<trigger> } Modulos a previously-set variable by a specific amount i.e. X % Y
Effect round_variable_to_closest = {
which = <variable_name>
value = <float>/<variable>/<scope.variable>/trigger:<trigger> (variable will be rounded to a multiple of this value)
}
Effect round_variable = <variable_name> Rounds a previously-set variable to the closest integer.
Effect floor_variable = <variable_name> Rounds a previously-set variable down to the previous integer.
Effect ceiling_variable = <variable_name> Rounds a previously-set variable up to the next integer.
Effect export_modifier_to_variable = { modifier = logistic_growth_mult variable = <variable_name> } Exports the value of a specified modifier in the current scope to a specified variable.
Effect export_modifier_duration_to_variable = { modifier = modifier_name variable = <variable_name> } Exports the remaining duration of a specified modifier in the current scope to a specified variable.
Effect export_trigger_value_to_variable = {
trigger = xxx
parameters = { xxx = yyy }
  (optional: specify extra parameters for triggers with { }
variable = <variable_name>
  (this example will print the amount of energy the scoped object has to a variable)
rounded = yes (default: no)
}
Exports the value of a trigger to a specified variable (so for num_pops, it'll export the number of pops).
Effect export_resource_stockpile_to_variable = { resource = energy variable = <variable_name> } Exports the value of the current country's stockpile of the specified resource to a variable.
Effect export_resource_income_to_variable = { resource = energy variable = <variable_name> } Exports the value of the current country's monthly income of the specified resource to a variable.
Effect export_resource_maximum_to_variable = { resource = energy variable = <variable_name> } Exports the value of the current country's maximum of the specified resource to a variable.
Effect get_galaxy_setup_value = { which = <variable_name> setting = <string> [ scale_by = <float> ] }

possible values: num_empires, num_advanced_empires, num_fallen_empires, num_marauder_empires, mid_game_year, end_game_year, victory_year, num_guaranteed_colonies, num_gateways, num_wormhole_pairs, num_hyperlanes, habitable_worlds_scale, primitive_worlds_scale, crisis_strength_scale, tech_costs_scale
Copies a value from the galaxy setup into a variable, optionally scaling it by an int value
Effect copy_flags_and_variables_from = event_target:<event_target> Copies all script flags and variables from the specified scope to the current one.

Clone this wiki locally