Advice for video game NPC

I’m new to the AI world but am looking for advice on how to use OpenAI APIs for an NPC in a video game world. The NPC is actually a fictional AI itself in the game, hence why I want to make it really feel like one.

  • Is there a way I can give it a backstory so it knows all the lore of the game (would be several pages of text)?
  • Can I set it to avoid discussing topics such as real world politics for example?
  • Can I update these things on the backend without needing to update the client game code?
1 Like

If anyone could point me in the right direction that would be greatly appreciated!

Yeah, Fortnite actually did something similar with Darth Vader. They basically used the system prompt + user prompt approach:

  • System prompt: This is where you load in the NPC’s backstory + rules. For example, “You are Darth Vader, a Sith Lord who knows the lore of the Empire. You must never break character and you don’t discuss real-world topics.”

  • User prompt: This is where you feed the player’s interactions/questions. The model will respond in character, guided by the system rules you already set.

That way, you can keep the NPC aligned with your game’s lore and prevent it from wandering into things like real-world politics. Plus, if you manage the prompts on your backend, you can update or tweak the backstory anytime without pushing a game client update

1 Like

I was lead dev on several releases of Global Agenda and Tribes Ascend… Been doing ML/AI ever since so maybe I can help if this question is still outstanding.

Minion NPCs might be too expensive to power using cloud models but the unit economics probably work out fine for bosses, when players have invested enough to get to elite levels. I would use the game engine’s systems to fully build out the boss, giving it some optional server driven behaviors that AI could provide. If the AI is not there, it works like the rest of the bosses. If the AI is found, it gets a ton more fun.

In the server side code, think of a melee as a conversation in the chat completions API. I would set it up something like this:

  1. system prompt: tell GPT it is acting as a bot in a game and the user is a coach advising it and giving a play by play of the battle. Give it the lore of the game, the specifics of the map its on, all the attacks it has, the personality you want it to take on for the specific boss. Tell it about the human players in the map… names, experience level, stats about each one. Importantly, tell it you want it to generate specific responses at each turn: which way to move, which attack to launch, what it wants to say, gestures and emotes to trigger, any other actions it has like spawning minions or calling for help. You can give it tools representing those actions so you will be notified by function calls or you can just tell it to provide structured outputs using a pydantic record definition or even just specify an XML record containing each output you need on every turn. Give it strategy instructions (when health is below 20% retreat unless one human remains and they are below 30% etc.). this will get quite long and you will need to evolve it.

  2. each “user” turn should describe the boss’ inputs in natural language. What does it hear? what does it see? what attack has just occurred? damage? remaining time, health level, did the attack it just did work? shields? you can use natural language for all of that, sort of like a “play by play” announcer. Leave out information it should not have (position of stealth attackers, conditions elsewhere on the map, etc) or give it more info if you want to power it up. You could put a camera on the boss’ head and render a POV of what it actually sees, loading that into a multi-modal model.

  3. on each model turn, parse out the models’ actions (from tool calls or structured outputs or XML) and push those into your NCP’s next tick or planning sequence. rinse and repeat.

Something like

system: (your code/config generates this text) you are a massive boss protecting a treasure and there’s a horde of attackers. Respond with ATTACK or RUN between XML “action” tags, then provide an optional “weapon” tag if attacking, or “direction“ if running. Use the“taunt” tag to speak to the attackers. You have a range attack LIGHTNING that does -4 and recharges in 3 seconds. You have a melee STAB attack that does -10 in close combat.

user: (the following text is generated programatically from map physics, game stats, etc. it does not need to be super interesting or well formatted. just dump facts about the game state in natural language that the model will understand) player named bob is a level 2 paladin coming toward you at 1 click with a broad sword. bob will reach you in 3 seconds. bob health is 100%. Your health is 90%. You hear noise in the distance. LIGHTNING is fully charged. You have 3 health potions remaining.

assistant: (the model will generate this) <taunt>Prepare to suffer, bob!</taunt><action>ATTACK</action><weapon>LIGHTNING</weapon>

tick tick tick (game engine ticks the characters / map)

user: (your code generates this based on events since the last turn) bob took -3 from lightning attack. Bob is doing -1 DPS on you with broadsword attack on your left. your health is 88%. LIGHTNING is 50% charged and not usable yet. player alice is 5 seconds away.

assistant: <taunt>bob, that tickles!</taunt><action>RUN</action><direction>RIGHT</direciton>

etc.

did the one guy just shamelessly edit his online casino into his message after the fact or have I just not had enough coffee yet?

I updated my prior reply to include the correct XML tags that had been filtered out because I did not format them as code. sorry if there’s some confusion about casinos… its just example prompts and replies. Thanks!!

1 Like

oh that’s a legitimate link… sorry i was a little nervous about clicking on it ^.^

Yes, this is absolutely doable with OpenAI APIs, and it’s a fairly common pattern. You can give the NPC a full backstory and lore by injecting it into the system prompt or by using a RAG setup where the lore is stored externally (e.g. in a vector database) and retrieved as needed. Topic restrictions like avoiding real-world politics can be enforced via system instructions and moderation rules. Importantly, all of this can live on your backend: you can update prompts, lore documents, and filters without touching the game client at all, as long as the client just talks to your server.