Loot: how it is built ​
The Loot Rules and Loot Tables chapters explain the options. This page explains the code.
The pieces ​
| Class | What it is |
|---|---|
LootTable, LootEntry | What can drop. roll_drops(context) is the roll; generate_loot(looter) is the older answer (item id → quantity) |
LootRule | A trigger, a table (shared by table_id, or own_table saved with the holder), a level source, uses_receiver |
LootLevelSource | Where an item level comes from: a source, an offset, a lowest and a highest level |
LootLevel | resolve(sources, holder_kind, holder, receiver, hub) answers the level; resolve_nested, from_choice |
LootDispatcher | run finds the rules of a trigger and rolls them into an inventory; roll_rule, deliver |
Entity, InteractableObject | The holders: get_loot_rules(), run_loot_rules(trigger, receiver), loot_rolled |
The roll of a table ​
LootTable.roll_drops(context) returns {"items": [...], "currency": {id: amount}}. Each item drop is {item_id, quantity, item_level, min_quality_tier, magic_find}, so a generated item can be made later with everything it needs. The context holds looter (an entity with modify_gain and modify_gain_int), item_level, rng, and level_resolver (a Callable(source, inherited_level) that answers the level of a table inside a table).
- The looter's magic find (
GainChannels.LOOT_RARITY) is read once. - The guaranteed entries that fit the level are dropped.
randi_range(min_items, max_items)rolls: each picks among the entries that fit the level and are not used (an entry that cannot duplicate is used up), by weight; magic find raises the weight of entries under the average;nothing_weightis a share of the roll that picks nothing.- An entry's chance is checked after it is picked. An item entry rolls its quantity (and the looter's loot quantity channel); a currency entry rolls an amount; a table entry rolls the other table at the level its own
level_sourcegives (the resolver), up toMAX_DEPTH(6) levels.
What a holder does ​
A holder has get_loot_rules(): an NPC's loot_rules plus the rules its older settings make (NPCDefinition.get_legacy_loot_rules: the old table with its logic, and the fixed inventory as an own table of guaranteed entries; LootRule.from_inventory), or the rules of a placed NPC (UniqueEntityData.get_effective_loot_rules: its own replace everything). An object has InteractableDefinition.loot_rules plus the rules of its container's older settings (ContainerInteraction.get_legacy_loot_rules), or those of the placed object (UniqueInteractableData.loot_rules_override).
run_loot_rules(trigger, receiver) calls LootDispatcher.run(holder, kind, rules, trigger, inventory, loot_rolled, receiver, hub):
- For each rule of the trigger: a rule on Initialize or First Access that is already in
loot_rolled(by its index in the list) is skipped; the rule is rolled (roll_rule: its table →LootLevel.resolve([table.level_source, rule.level_source], …)→table.roll_drops) and delivered (deliver: an item thatneeds_generation()goes throughItemGenerator.make_instancesat the item level of its drop, any other item throughInstanceUtility.add_item_to_inventory; currency throughadd_currency); then the index is added toloot_rolled. loot_rolledis saved with the holder ("loot_rolled"in the entity and object save data, restored as integers after JSON).
The triggers ​
| Trigger | Called from |
|---|---|
ON_INITIALIZE | Entity.initialize_entity, after the NPC's level is set (so Holder level is known); InteractableObject.initialize_interactable, after the interactions are set up |
ON_DEATH | Entity.entity_death (with the killer, last_damage_source, as receiver) |
ON_DESTROYED | InteractableObject.handle_destruction |
ON_FIRST_ACCESS | ContainerInteraction._attempt_open_container (opener as receiver), LootInteraction.start_interaction (the body is looted), AccessEntityInventoryEffect (the pickpocket rolls the target's rules before it looks whether the inventory is empty) |
An NPC with an unrolled first-access rule gets its LootInteraction when it dies even if its inventory is empty (Entity.has_pending_loot).
Remains of a destroyed object ​
InteractableObject.has_lootable_remains() is true while the object is destroyed (or being destroyed) and its inventory holds something or a first-access rule has not rolled. Then:
process_interactionstill works on a dead object that has remains, andhandle_destructionclears the lock.- A
ContainerInteractionthat is destroyed with remains stays in its closed state (on_object_destroyed), listens to its inventory (_on_remains_updated) and becomesDESTROYEDfor good when it is empty. After a loadrefresh_after_loaddoes the same. - An object with no container gets one made for it (
_make_remains_interaction) when it is destroyed with loot, and again after a load of a destroyed object with a non-empty inventory.
The level, in order ​
LootLevel.resolve(sources, holder_kind, holder, receiver, hub):
- The first of
sources(table, then rule) whose source is not Inherit is the chosen one; its offset and limits shape the result. With none chosen, the default for the holder kind (GameplayConfig.default_npc_loot_level/default_object_loot_level; a reward uses Receiver) is the source, and the first source in the list supplies the offset and limits. - Fixed: its level. Holder:
holder.current_level, falling back to the party. Receiver: itscurrent_level, falling back to the party. Party:NpcLevels.reference_level(party_manager). World:WorldData.item_levelof the current world, falling back to the party. The party falls back toGameplayConfig.fallback_item_level. - The offset is added; the lowest and highest level are applied; the result is at least 1.
Extending ​
- A new trigger: add it to
LootRule.Trigger, callholder.run_loot_rules(trigger, receiver)where it happens, and add it to the choices ofLootRulesFields. Triggers other than Initialize and First Access roll every time they are called. - A new holder: keep a
loot_rolledarray (saved), aget_loot_rules(), and callLootDispatcher.runwith an inventory and aLootLevel.HolderKind. - A new entry kind: add it to
LootEntry.EntryTypeand handle it inLootTable._drop_entry.
Pitfalls ​
- Rules are numbered by their place in the list. Removing a rule renumbers the ones after it: a saved holder that remembers rule 2 as rolled now remembers a different rule.
ItemInstances need a system hub (InstanceUtility.system_hub) to start effects; in the editor preview the roll is a plainDictionary(ItemGenerator.roll,describe_roll).- The older
generate_loot(looter)returns item ids and quantities at item level 1 and ignores currency and level bands: useroll_drops.