Wiki

trigger · collection_size

Definition

  • Supported scope:any
  • Supported target:any

Description

Compares size of a collection to a value

NOTE! Comparison is inclusive, so "value > X" actually means "value >= X" etc.!!!

### Example

collection_size = { input = collection_id # see the documentation in common/collections for detailed info value > N # supported operators: <, >, = value < M # multiple comparisons are allowed! value < VAR # variables can be used }

实战 · 配合 · 坑

实战内容由 AI 生成,并已对照 vanilla 命令词表校验 —— 请当作起点而非权威依据。上方的定义节才是游戏自带文档原文。

实战用法

collection_size 常用于需要对动态数组长度做门控判断的场景,例如检查某个自定义科研队列、事件待处理列表或阵营成员集合的数量是否达到阈值,再决定是否触发事件或解锁决议。结合 common/collections 中定义的 collection,可以在不借助临时变量手动计数的情况下直接完成数量比较。

# 当某个自定义收藏集合中的元素超过 3 个时,允许触发特殊决议
available = {
    collection_size = {
        input = my_mod_target_collection
        value > 3   # 实际为 >= 3,注意包含边界
        value < 10  # 同时限制上限,实际为 <= 9(含)
    }
}

配合关系

  • [any_collection_element](/wiki/trigger/any_collection_element):通常先用 collection_size 确认集合非空或达到规模后,再用 any_collection_element 对其中元素逐一判断具体条件,避免在空集合上迭代。
  • [all_collection_elements](/wiki/trigger/all_collection_elements):当需要同时验证"数量足够"且"所有元素均满足某属性"时,两者配合写在同一 and 块中,逻辑层次更清晰。
  • [check_variable](/wiki/trigger/check_variable)collection_size 本身不支持将结果存入变量,若需要把数量与动态变量阈值比较,可先用 [find_highest_in_array](/wiki/effect/find_highest_in_array) 等方式处理数据,再用 check_variable 做辅助校验。
  • [every_collection_element](/wiki/effect/every_collection_element):常见模式是先用 collection_size 做 trigger 门控,通过后在对应 effect 块中用 every_collection_element 批量执行操作。

常见坑

  1. 比较运算符的包含性陷阱:官方明确说明比较是 inclusivevalue > 3 实际等同于数学上的 ≥ 3,而不是严格大于。新手若要表达"严格大于 3",需要改写为 value > 4,否则边界值会被错误纳入。
  2. collection 未在 common/collections 中注册:直接在脚本里写 input = some_collection 而没有在对应目录建立 collection 定义文件,游戏不会报语法错误但条件永远不会通过,排查时极难发现,务必确认 collection id 已正确注册并在对应 scope 下有效填充。

Hands-On Notes

Hands-on notes are AI-generated and checked against the vanilla command vocabulary — treat them as a starting point, not authoritative reference. The definition above is the game's own documentation.

Hands-On Usage

collection_size is commonly used in scenarios where you need to gate logic based on the dynamic length of an array—for example, checking whether a custom research queue, pending event list, or faction member pool reaches a threshold before triggering an event or unlocking a decision. Combined with collections defined in common/collections, you can compare quantities directly without resorting to manual counting with temporary variables.

# Allow a special decision to trigger when a custom collection contains more than 3 elements
available = {
    collection_size = {
        input = my_mod_target_collection
        value > 3   # Actually >= 3; note that the boundary is inclusive
        value < 10  # Also cap the upper limit; actually <= 9 (inclusive)
    }
}

Synergy

  • [any_collection_element](/wiki/trigger/any_collection_element): Typically use collection_size first to confirm the collection is non-empty or meets size requirements, then apply any_collection_element to individually check specific conditions on its members, avoiding iteration over empty collections.
  • [all_collection_elements](/wiki/trigger/all_collection_elements): When you need to verify both "sufficient quantity" and "all elements satisfy a property," combine both in the same and block for clearer logical hierarchy.
  • [check_variable](/wiki/trigger/check_variable): collection_size does not support storing its result in a variable. If you need to compare the count against a dynamic variable threshold, consider using approaches like [find_highest_in_array](/wiki/effect/find_highest_in_array) to process the data first, then use check_variable for supplementary validation.
  • [every_collection_element](/wiki/effect/every_collection_element): A common pattern is to gate with collection_size as a trigger first, then execute batch operations using every_collection_element in the corresponding effect block upon passing.

Common Pitfalls

  1. Inclusive boundary trap with comparison operators: The official documentation explicitly states that comparisons are inclusivevalue > 3 is mathematically equivalent to ≥ 3, not strict greater-than. If you need to express "strictly greater than 3," rewrite it as value > 4; otherwise boundary values will be incorrectly included.
  2. Collection not registered in common/collections: Writing input = some_collection in a script without creating a corresponding collection definition file in that directory will not produce a syntax error, but the condition will never evaluate to true—an extremely difficult issue to debug. Always verify that the collection ID is properly registered and correctly populated within the appropriate scope.