From Stellaris Wiki
Jump to navigation Jump to search


Outliner top.png
This article has been verified for the current PC version (3.6) of the game.

We can use script-defined variables to perform calculations in scripts and events. Once created, variables are stored in their scope and can be retrieved for later use. As of version 3.1, you can do a lot more (fancy things) with variables in script. Variables can be copied between scopes (see below), and can be used as part of bracket commands in Localisation modding.

Available scopes[edit | edit source]

As of version 3.1 we can use variables nearly within every scope. When a variable is set in a scope, it binds to the scope and persists in it.

Scopes: megastructure planet country ship pop fleet galactic_object leader army ambient_object species pop_faction war federation starbase deposit sector archaeological_site first_contact spy_network espionage_operation espionage_asset

Commands[edit | edit source]

Setting variables[edit | edit source]

You can make a variable out of various numbers. The easiest is set_variable and change_variable:

  • set_variable = { which = star_temperature value = @G2V_star_temperature } – Sets a specific star’s temperature to the scripted variable value

But you can also do it with:

  • 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 or (since 3.3) the value of count_x triggers. Otherwise 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
  • set_variable_to_random_value = { which = <some_variable> min = -100 max = 100 rounded = yes/no } – Sets a variable to a random value within the specified bounds.(with 3.6)

For more see below.

Manipulating variables[edit | edit source]

Once the variable is set, you can then alter it with standard mathematical operations:

  • change_variable = { which = <string> value = <float>/<variable>/<scope.variable>/trigger:<trigger> } – Increments a previously-set variable by a specific amount
  • subtract_variable = { which = <string> value = <float>/<variable>/<scope.variable>/trigger:<trigger> } – Decrements a previously-set variable by a specific amount
  • multiply_variable = { which = <string> value = <float>/<variable>/<scope.variable>/trigger:<trigger> } – Multiplies a previously-set variable by a specific amount
  • divide_variable = { which = <string> value = <float>/<variable>/<scope.variable>/trigger:<trigger> } – Divides a previously-set variable by a specific amount
  • modulo_variable = { which = <string> value = <float>/<variable>/<scope.variable>/trigger:<trigger> } – Modulos a previously-set variable by a specific amount i.e. X % Y

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

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

Once you are done with a variable, you can clear it using: clear_variable = <string>(since 3.0)

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).with 3.1

Checking variable values[edit | edit source]

  • check_variable = { which = <variable> value = <float>/<variable>/<scope.variable>/trigger:<trigger>/modifier:<modifier> }

This checks a variable for the chosen scope – accepts =, >=, >, <=, and < as operators.

When you are using the value of a variable, you can now (with 3.1) use dot scoping: value = owner.capital_scope.my_var
You can also directly refer to the value of a trigger: value = trigger:num_pops
A combination of the two is also possible: value = owner.capital_scope.trigger:num_pops
You can also (with 3.3) directly refer to the value of a modifier: value = modifier:pop_growth_speed_reduction
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.
check_variable_arithmetic = {
	which = <variable>
	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)

This 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).with 3.0

Copying variables between scopes[edit | edit source]

Just like with checking variables, you must (now with 3.1) use dot scoping:

set_variable = { which = var1 value = owner.capital_scope.my_var }

Pre 3.1 outdated syntax

* A scope (in the form of root/from/prev) which contains a variable with the same name: (set|change|subtract|multiply|divide|modulo)_variable = { which = <variable_name> value = <scope> }
* A scope reference to point to another variable in another scope value = { scope = <scope> variable >=< <variable> } (with 3.0)

We can copy variables direct between scopes. To do this, you can either name a scope to copy a variable of the same name, or specify both the scope and variable name. This command looks for a variable in the "owner" scope called "var1", and if it finds it, copies its value to "var1" in the current scope: set_variable = { which = var1 value = owner }

And this looks for a variable in the owner scope called "var2", to set the value of "var1": (with 3.0) set_variable = { which = var1 value = { scope = owner variable = var2 } }

Limitations and other notes[edit | edit source]

It is not required to "initialize" a variable before use. When a variable is first used, it is assumed by the game to be zero. However, the game will throw an error if you use an unset variable. To avoid unset variables errors, use a check if the specified variable is set on the current scope.

is_variable_set = <string>

We can assign more than only simple trigger/modifier values to a variable:(with 3.0)

  • export_modifier_to_variable = { modifier = pop_growth_speed_reduction variable = <string> } – Exports the value of a specified modifier in the current scope to a specified variable.
  • Resource stockpiles and incomes can also be exported using one of one of the following:
export_resource_stockpile_to_variable = { resource = <resource> variable = <string> } – Exports the value of the current country’s stockpile of the specified resource to a variable.
export_resource_income_to_variable = { resource = <resource> variable = <string> } – Exports the value of the current country’s monthly income of the specified resource to a variable.
  • get_galaxy_setup_value = { which = <string> setting = <string> [ scale_by = <float> ] } – Copies a value from the galaxy setup into a variable, optionally scaling it by an int value.
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

Pre 3.1 outdated syntax

(with 3.0) the only trigger value that can be exported is fleet_power: export_trigger_value_to_variable = { trigger = fleet_power variable = <string> rounded = yes (default: no) } – Exports the value of a specified simple value trigger (i.e. no { }, returns a number on the right hand side) to a specified variable.

Using variables – the wheres[edit | edit source]

We can (now with 3.1) use variables in a lot of different places:

  • Any trigger that is comparing a single number, including ones with { }. E.g. "num_pops > my_var", "intel = { who = from value < trigger:num_pops }".
  • Any effect using a single number, including ones with { }. E.g. "add_experience = my_var".
  • 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).
  • Resource tables and triggered resource tables can take a "multiplier = <variable>", the variable must be in the planet scope and may not be dot scoped:
	resources = {
		category = planet_buildings
		cost = {
			trigger = { <triggers> }
			minerals = 100
			multiplier = my_var/trigger:num_pops
  • MTTH/AI Chance modifiers:
	ai_chance = {
		factor = 1
		modifier = {
			add/factor = my_var/trigger:num_pops
			is_variable_set = my_var
  • 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_var], it will print the value of the variable, so long as the variable is set.

Number precision and rounding[edit | edit source]

We have several native options for rounding variables:(with 3.0)

round_variable = <string> – Rounds a previously-set variable to the closest integer.
floor_variable = <string> – Rounds a previously-set variable down to the previous integer.
ceiling_variable = <string> – Rounds a previously-set variable up to the next integer.
round_variable_to_closest – Rounds a previously-set variable to the closest X (multiple of the specified value).

Pre 3.0 rounding syntax

You had to use an operation workaround here, by simply dividing and multiplying.
 set_variable = { which = myvar value = 360.3451 }
 divide_variable = { which = myvar value = 100000 }
 multiply_variable = { which = myvar value = 100000 }
 # gives 360 
Note: This rounds the number only down, if you want round to the nearest above 0, add 0.5. If you want round up above 0, add 0.99999. The floating point precision of (almost) every float number is 5 digits.

Empire EmpireEthicsGovernments • Civics • OriginsMandatesAgendasTraditions • Ascension perksEdictsPoliciesRelicsTechnologiesCustom empires
Pops JobsFactions
Leaders LeadersLeader traits
Species SpeciesSpecies traits
Planets PlanetsPlanetary feature • Orbital depositBuildings • DistrictsPlanetary decisions
Systems SystemsStarbasesMegastructuresBypassesMap
Fleets FleetsShips • Components
Land Warfare ArmiesBombardment stance
Diplomacy Diplomacy • Federations • Galactic communityOpinion modifiersCasus Belli • War goals
Events EventsAnomaliesSpecial projectsArchaeological sites
Gameplay GameplayDefinesResources • EconomyGame start
Dynamic modding Dynamic moddingEffectsConditionsScopesModifiersVariablesAI
Media/localisation Maya exporterGraphicsPortraitsFlagsEvent picturesInterfaceIconsMusicLocalisation
Other Console commandsSave-game editingSteam WorkshopModding tutorial