Hands-On Usage
random_state is commonly used in mod random events or decision effects — for example, picking a random state across the country that meets certain conditions to add buildings, resources, or trigger a specific event, preventing the effect from always landing on the same fixed location. Pairing it with prioritize lets you favor particular states (such as the capital or key industrial regions), making the logic more predictable and controlled.
# Randomly construct a level-1 naval base in a coastal state controlled by the player's country
country_event = {
id = my_mod.1
immediate = {
random_state = {
prioritize = { 113 114 } # state ids to consider first
limit = {
is_controlled_by = ROOT
is_coastal = yes
free_building_slots = {
building = naval_base
size > 0
include_locked = no
}
}
add_building_construction = {
type = naval_base
level = 1
instant_build = yes
}
}
}
}
Synergy
- is_controlled_by — Used inside a
limit block to restrict selection to states controlled by your own country (or a specific nation), preventing the effect from accidentally applying to enemy-occupied territory.
- free_building_slots — Commonly used as a validity check before building effects, ensuring the target state has available building slots before construction is triggered.
- add_building_construction — The most typical companion effect, constructing a building in the randomly selected state.
- state_event — Fires a state event on the randomly selected state, combining with
random_state to achieve a narrative effect of "a random region experiencing an outbreak of events."
Common Pitfalls
- Forgetting
limit causes the effect to land on any state: When random_state is used without a limit, it selects randomly from every state on the map, potentially picking an enemy nation's state or an unclaimed region. You should always include at least a basic filter such as is_owned_by or is_controlled_by.
- Mistaking
prioritize for a guaranteed selection: States listed in prioritize are only "preferred candidates." If none of them satisfy the limit conditions, the game falls back to other states that do qualify; if no states qualify at all, the effect is silently skipped. Beginners often misread this as the logic not executing at all.