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
- Inclusive boundary trap with comparison operators: The official documentation explicitly states that comparisons are inclusive—
value > 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.
- 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.