Variables - Stellaris Wiki (2024)

Version

Variables - Stellaris Wiki (1)

Please help with verifying or updating older sections of this article. At least some were last verified for version 3.6.

This article is for the PC version of Stellaris only.

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.

Contents

  • 1 Available scopes
  • 2 Commands
    • 2.1 Setting variables
    • 2.2 Manipulating variables
    • 2.3 Checking variable values
    • 2.4 Copying variables between scopes
  • 3 Limitations and other notes
    • 3.1 Using variables – the wheres
    • 3.2 Number precision and rounding

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 variablevalue = 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. It is possible to check a script_value.

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>/value:<script_value>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

This is an example of checking a script value from vanilla:

check_variable_arithmetic = { which = value:situation_machine_uprising_pops_monthly_progressvalue > 0.5 }

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_buildingscost = {trigger = { <triggers> }minerals = 100multiplier = my_var/trigger:num_pops}}
  • MTTH/AI Chance modifiers:
ai_chance = {factor = 1modifier = {add/factor = my_var/trigger:num_popsis_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.

Modding

EmpireEmpireEthicsGovernments • Civics • OriginsMandatesAgendasTraditions • Ascension perksEdictsPoliciesRelicsTechnologiesCustom empires
PopsJobsFactions
LeadersLeadersLeader traits
SpeciesSpeciesSpecies traits
PlanetsPlanetsPlanetary feature • Orbital depositBuildings • DistrictsPlanetary decisions
SystemsSystemsStarbasesMegastructuresBypassesMap
FleetsFleetsShips • Components
Land warfareArmiesBombardment stance
DiplomacyDiplomacy • Federations • Galactic communityOpinion modifiersCasus Belli • War goals
EventsEventsAnomaliesSpecial projectsArchaeological sites
GameplayGameplayDefinesResources • EconomyGame start
Dynamic moddingDynamic moddingEffectsConditionsScopesModifiersVariablesAIOn actions
Media/localisationMaya exporterPortraitsFlagsEvent picturesInterfaceIconsMusicLocalisation
OtherConsole commandsSave-game editingSteam WorkshopModding tutorial
Variables - Stellaris Wiki (2024)
Top Articles
Latest Posts
Article information

Author: Terence Hammes MD

Last Updated:

Views: 6384

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Terence Hammes MD

Birthday: 1992-04-11

Address: Suite 408 9446 Mercy Mews, West Roxie, CT 04904

Phone: +50312511349175

Job: Product Consulting Liaison

Hobby: Jogging, Motor sports, Nordic skating, Jigsaw puzzles, Bird watching, Nordic skating, Sculpting

Introduction: My name is Terence Hammes MD, I am a inexpensive, energetic, jolly, faithful, cheerful, proud, rich person who loves writing and wants to share my knowledge and understanding with you.