NPC - Non-player character. It’s one of the most important aspects of a game. It makes the world feel alive. Getting NPCs right is key to games success. But how do you make one?
NPC consists of many parts, such as visuals, movement or personality. The brain however carries the most weight. What makes the NPC go there, choose this or say that. Is it all just conditions like in Undertale? Does the NPC have a schedule that drives him? What about the relationships between NPCs and player?
These questions have many different answers and solutions. I wanted my game to feel dynamic and alive. No hardcoded schedule that will make the NPCs feel rigid and “locked“. I did some research and i found a thing called Utility AI system.
Unlike schedule systems or pure conditions it doesn’t control or make the NPCs do anything. It’s just a choice marking stimulation.
The way it works is quite simple. You have a series of utilities, under which are many values, i call them consideration values. Its a value 0 to 1 computing a “state“ of the NPC. Hunger, fatigue, time of day, essentially anything that can be turned to 0→1. These values then get added/multiplied/divided/subtracted to compute one singular value for the utility. The utility with the highest value gets chosen. That sends a signal carrying ID of the action to do and some args to the NPC.
This system is easily expandable. For my game i have a structure like this:
Agent
—State
——Utility
———Aggregation
————Consideration Value
*Agent - the controller that chooses the utility and state
*State - state of the npc, such as combat, idle or flee
*Utility - the action to perform. Utilities can be stacked.
*Aggregation - just a wrapper for all values. You have a choice which mathematical function you want to use to compute the final value.
*Consideration value - a simple function that “normalizes“ a certain value to range from 0 to 1. With the addition of curve to modify the value even more.