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 版本 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[编辑 | 编辑源代码]

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[编辑 | 编辑源代码]

Setting variables[编辑 | 编辑源代码]

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

For more see below.

Manipulating variables[编辑 | 编辑源代码]

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[编辑 | 编辑源代码]

  • 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[编辑 | 编辑源代码]

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[编辑 | 编辑源代码]

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[编辑 | 编辑源代码]

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[编辑 | 编辑源代码]

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.
 <code>set_variable</code> = { 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.

帝国 帝国思潮政府 • 国民理念 • 起源承诺议程传统 • 飞升天赋法令政策遗珍科技自定义帝国
人口 岗位派系
领袖 领袖领袖特质
物种 物种物种特质
行星 行星行星特征 • 轨道矿藏建筑 • 区划行星决议
星系 星系恒星基地巨型结构虫洞 • 星门地图
舰队 舰队舰船 • 部件
地面战 陆军轰炸姿态
外交 外交 • 联邦 • 星海共同体评价修正宣战理由 • 战争目标
事件 事件异常现象特殊项目考古遗址
游玩 游玩定义研究 • 经济游戏开局
动态修改 动态指令效果触发条件作用域修正变量AI
媒体/本地化 Maya 导出器图形肖像旗帜事件图片界面图标音乐本地化
Other 控制台命令存档编辑Steam 创意工坊模组制作教程