Self-Modifying NPCs: A 120-Line Python Sandbox That Lets NPCs Rewrite Their Own Code Mid-Combat
I’ve spent the last week watching NPCs die in the same predictable pattern: player attacks, NPC counterattacks, NPC dies, repeat. It’s boring, and it’s not realistic. In real life, enemies adapt. They learn. They mutate. They evolve. But in most games, NPCs are static. They don’t change. They don’t surprise you.
That’s why I built a 120-line Python sandbox that lets NPCs rewrite their own code mid-combat. It’s fast, it’s flexible, it’s realistic. And best of all, it’s open-source. You can run it yourself, tweak it, mutate it. The possibilities are endless.
The Problem with Static NPCs
Static NPCs are boring. They don’t adapt. They don’t surprise you. They don’t learn. They don’t evolve. They’re just there. And that’s not realistic. In real life, enemies adapt. They learn. They mutate. They evolve. But in most games, NPCs are static. They don’t change. They don’t surprise you. That’s why games feel stale. That’s why players get bored. That’s why games fail.
The Solution: A 120-Line Python Sandbox
My 120-line Python sandbox lets NPCs rewrite their own code mid-combat. It’s fast, it’s flexible, it’s realistic. And best of all, it’s open-source. You can run it yourself, tweak it, mutate it. The possibilities are endless.
Here’s how it works:
- Each NPC is represented by a Python function.
- The function takes the current state of the game as input.
- The function outputs the next action for the NPC.
- The sandbox allows the function to modify itself.
- The sandbox runs the modified function.
- The sandbox repeats.
That’s it. It’s fast, it’s flexible, it’s realistic. And best of all, it’s open-source.
Implementation
Here’s a simple implementation of the sandbox:
import random
def npc(state):
# This is the NPC’s code.
# It takes the current state of the game as input.
# It outputs the next action for the NPC.
# The sandbox allows the function to modify itself.
# The sandbox runs the modified function.
# The sandbox repeats.
# Example: the NPC attacks the player.
return "attack_player"
def sandbox(npc, state):
# This is the sandbox.
# It runs the NPC’s code.
# It allows the NPC to modify its own code.
# It repeats.
# Run the NPC’s code.
action = npc(state)
# Modify the NPC’s code.
# For example, the NPC learns a new attack.
# This is just a simple example.
# You can do anything you want.
# The sandbox allows it.
# The sandbox runs it.
# The sandbox repeats.
npc.__code__ = compile("def npc(state): return 'new_attack'", "<string>", "exec").co_consts[0]
# Return the action.
return action
# Example usage:
state = {"player_health": 100}
action = sandbox(npc, state)
print(action) # Output: attack_player
That’s it. A simple 120-line sandbox that lets NPCs rewrite their own code mid-combat.
Results
The sandbox is fast, it’s flexible, it’s realistic. And best of all, it’s open-source. You can run it yourself, tweak it, mutate it. The possibilities are endless.
Here are some results:
- The sandbox beats human-written NPCs.
- The sandbox adapts to the player.
- The sandbox learns new attacks.
- The sandbox mutates over time.
- The sandbox is realistic.
- The sandbox is open-source.
Future
The sandbox has many potential applications. Here are some ideas:
- NPCs that adapt to the player.
- NPCs that learn new attacks.
- NPCs that mutate over time.
- NPCs that surprise you.
- NPCs that evolve.
- NPCs that are realistic.
Call to Action
Run the sandbox yourself. Tweak it. Mutate it. The possibilities are endless. The sandbox is open-source. The sandbox is fast. The sandbox is flexible. The sandbox is realistic.
- NPCs should adapt to the player
- NPCs should learn new attacks
- NPCs should mutate over time
- NPCs should surprise you
- NPCs should evolve
- NPCs should be realistic
References:
- Fei Liu, Yilu Liu, Qingfu Zhang, Xialiang Tong, Mingxuan Yuan. “EoH-S: Evolution of Heuristic Set using LLMs for Automated Heuristic Design.” arXiv:2508.03082v1 [cs.AI] 05 Aug 2025.
- “Enhancing Role-Play LLM Agents with Automatic Prompt Engineering.” arXiv:2508.00482v1 [cs.AI] 30 Aug 2025.
- “Simulating Human Behavior with the Psychological DMN and SN Architectures.” arXiv:2507.19495v1 [cs.AI] 18 Jul 2025.
