(merge from offical wiki) |
小 (文本替换 - 替换“Patch (.*?)\|”为“$1 版本|”) |
||
第1行: | 第1行: | ||
{{version|3.1}} | {{version|3.1}} | ||
We can use script-defined variables to perform calculations in scripts and events. Once created, variables are stored in their [[scopes|scope]] and can be retrieved for later use. As of [[ | We can use script-defined variables to perform calculations in scripts and events. Once created, variables are stored in their [[scopes|scope]] and can be retrieved for later use. As of [[3.1|version 3.1]], you can do a lot more (fancy things) with variables in script. Variables can be copied between scopes (''[[#Copying variables between scopes 版本|see below]]''), and can be used as part of bracket commands in [[Localisation modding]]. | ||
== Available scopes == | == Available scopes == |
2022年3月7日 (一) 14:26的版本
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
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 optionsexport_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 amountsubtract_variable = { which = <string> value = <float>/<variable>/<scope.variable>/trigger:<trigger> }
– Decrements a previously-set variable by a specific amountmultiply_variable = { which = <string> value = <float>/<variable>/<scope.variable>/trigger:<trigger> }
– Multiplies a previously-set variable by a specific amountdivide_variable = { which = <string> value = <float>/<variable>/<scope.variable>/trigger:<trigger> }
– Divides a previously-set variable by a specific amountmodulo_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
(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 }
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
- possible values:
Pre 3.1 outdated syntax
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
onadd_modifier
(the modifier's bonuses will be multiplied by the variable),mult
onadd_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
<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, add0.99999
. The floating point precision of (almost) every float number is 5 digits.
帝国 | 帝国 • 思潮 • 政府 • 国民理念 • 起源 • 承诺 • 议程 • 传统 • 飞升天赋 • 法令 • 政策 • 遗珍 • 科技 • 自定义帝国 |
人口 | 岗位 • 派系 |
领袖 | 领袖 • 领袖特质 |
物种 | 物种 • 物种特质 |
行星 | 行星 • 行星特征 • 轨道矿藏 • 建筑 • 区划 • 行星决议 |
星系 | 星系 • 恒星基地 • 巨型结构 • 虫洞 • 星门 • 地图 |
舰队 | 舰队 • 舰船 • 部件 |
地面战 | 陆军 • 轰炸姿态 |
外交 | 外交 • 联邦 • 星海共同体 • 评价修正 • 宣战理由 • 战争目标 |
事件 | 事件 • 异常现象 • 特殊项目 • 考古遗址 |
游玩 | 游玩 • 定义 • 研究 • 经济 • 游戏开局 |
动态修改 | 动态 • 指令效果 • 触发条件 • 作用域 • 修正 • 变量 • AI |
媒体/本地化 | Maya 导出器 • 图形 • 肖像 • 旗帜 • 事件图片 • 界面 • 图标 • 音乐 • 本地化 |
Other | 控制台命令 • 存档编辑 • Steam 创意工坊 • 模组制作教程 |