{"schemaVersion":1,"record":{"key":"lesson/npc-action-selection","type":"lesson","id":"npc-action-selection","lang":"en","title":"How an NPC selects an action","summary":"Separate eligibility, priority and execution so a character makes explainable decisions without rapidly switching between them.","updatedAt":"2026-10-20","status":"active","url":"https://gamemechanics.pro/en/lessons/npc-action-selection/","jsonUrl":"https://gamemechanics.pro/api/v1/knowledge/en/lesson/npc-action-selection.json","markdownUrl":"https://gamemechanics.pro/content/knowledge/en/lesson/npc-action-selection.md","aliases":[],"tags":["npc-ai","decision-making","debugging"],"sourceIds":["source-25e079ce502c5d9b","source-8ad8c2778e5aab6e"],"sources":[{"id":"source-25e079ce502c5d9b","title":"Dawe et al.: Behavior Selection Algorithms, An Overview","url":"https://www.gameaipro.com/GameAIPro/GameAIPro_Chapter04_Behavior_Selection_Algorithms.pdf","verifiedAt":null},{"id":"source-8ad8c2778e5aab6e","title":"David Rez Graham: An Introduction to Utility Theory","url":"https://www.gameaipro.com/GameAIPro/GameAIPro_Chapter09_An_Introduction_to_Utility_Theory.pdf","verifiedAt":null}],"sections":[{"id":"prerequisites","title":"Prerequisites","body":"A character may need charging, repairs and helping the player simultaneously. Write the selection rule explicitly before adding behavioral architecture."},{"id":"principle","title":"The principle","body":"First filter eligible actions, then select one, then execute it. Do not let each subsystem independently control movement. The Game AI Pro overview compares state machines, behavior trees and other selection methods; there is no universal winner.\n\nA state machine suits a few distinct modes. A behavior tree suits sequences of conditions and actions. Comparing situational value can use a common scoring scale, as described in the utility-theory chapter. Scoring does not replace prohibitions or eligibility checks.\n\n<span id=\"example-a-maintenance-robot\"></span>"},{"id":"example","title":"Practical example","body":"### A maintenance robot\n\nIn this teaching model, a robot can charge, repair a pump or help the player open a heavy door. Battery is normalized from 0 to 1. Below 0.1, a mandatory rule sends it to an available charger. If no route exists, it waits safely and reports why.\n\nOtherwise, compare eligible candidates:\n\n| Action | Score in this model | Eligibility                             |\n| ------ | ------------------- | --------------------------------------- |\n| Charge | `1 - battery`       | Charger reachable                       |\n| Repair | `leakSeverity`      | Parts available and pump reachable      |\n| Help   | `requestUrgency`    | Request still active and door reachable |\n\nAll inputs are bounded to 0..1. With battery 0.6, leak severity 0.8 and urgency 0.5, repair wins. These are authored design scores, not measurements of human desires.\n\nTo prevent close scores causing rapid switching, retain the current action until a competitor scores at least 0.15 higher. Release completed or ineligible actions immediately. Break ties with a stable order. Tune the margin after observation; the mandatory low-battery rule still takes priority."},{"id":"mistakes","title":"Common mistakes","body":"- Comparing distance in meters with urgency on a 0..1 scale without conversion.\n- Restarting repair every frame and resetting its progress.\n- Retaining an action for stability after its target has been destroyed."},{"id":"checks","title":"Verify it","body":"- Log inputs, excluded actions, scores and selection reasons.\n- Check tied scores, lost targets and no eligible candidates.\n- Vary urgency around the margin and check for excessive switching.\n- Separately check mandatory charging and an unreachable charger."},{"id":"agent-task","title":"Prompt for an AI agent","body":"> Implement selection as a pure function of a world snapshot and current action. Keep execution separate. Prepare a boundary-case table and explain every result without random selection. Do not connect a language model to per-frame control.\n\nReview through the [Action](/en/lenses/#schell-031) and [Transparency](/en/lenses/#schell-062) lenses."}],"relations":[{"type":"requires","direction":"outgoing","label":"Before you start","key":"lesson/npc-perception","title":"NPC perception and target memory","url":"https://gamemechanics.pro/en/lessons/npc-perception/","jsonUrl":"https://gamemechanics.pro/api/v1/knowledge/en/lesson/npc-perception.json","markdownUrl":"https://gamemechanics.pro/content/knowledge/en/lesson/npc-perception.md"},{"type":"explains","direction":"outgoing","label":"Apply the principle","key":"mechanic/point-and-click--family-31","title":"NPC, animal and living-system behavior","url":"https://gamemechanics.pro/en/knowledge/mechanic/point-and-click--family-31/","jsonUrl":"https://gamemechanics.pro/api/v1/knowledge/en/mechanic/point-and-click--family-31.json","markdownUrl":"https://gamemechanics.pro/content/knowledge/en/mechanic/point-and-click--family-31.md"},{"type":"practices","direction":"outgoing","label":"Put it into practice","key":"practice/maintenance-patrol","title":"Maintenance patrol","url":"https://gamemechanics.pro/en/knowledge/practice/maintenance-patrol/","jsonUrl":"https://gamemechanics.pro/api/v1/knowledge/en/practice/maintenance-patrol.json","markdownUrl":"https://gamemechanics.pro/content/knowledge/en/practice/maintenance-patrol.md"},{"type":"reviews","direction":"outgoing","label":"Review through a lens","key":"lens/schell-031","title":"Action","url":"https://gamemechanics.pro/en/knowledge/lens/schell-031/","jsonUrl":"https://gamemechanics.pro/api/v1/knowledge/en/lens/schell-031.json","markdownUrl":"https://gamemechanics.pro/content/knowledge/en/lens/schell-031.md"},{"type":"reviews","direction":"outgoing","label":"Review through a lens","key":"lens/schell-062","title":"Transparency","url":"https://gamemechanics.pro/en/knowledge/lens/schell-062/","jsonUrl":"https://gamemechanics.pro/api/v1/knowledge/en/lens/schell-062.json","markdownUrl":"https://gamemechanics.pro/content/knowledge/en/lens/schell-062.md"},{"type":"contains","direction":"incoming","label":"Learning paths","key":"path/npc-behavior","title":"NPC behavior","url":"https://gamemechanics.pro/en/learn/npc-behavior/","jsonUrl":"https://gamemechanics.pro/api/v1/knowledge/en/path/npc-behavior.json","markdownUrl":"https://gamemechanics.pro/content/knowledge/en/path/npc-behavior.md"}],"placements":[],"supersededBy":[],"attributes":{"category":"engineering","level":"intermediate","minutes":6},"endsAt":null,"provenance":"Original gamemechanics.pro material. Check illustrative examples in your own project.","license":"CC-BY-4.0","codeLicense":"MIT","attribution":"gamemechanics.pro contributors"}}