⚡ Part 5 · Advanced Module

Adding AI to Your
MUGEN Characters

Make your characters fight on their own from simple button-mashing to smart attack patterns.

⏱ ~60 minutes 📋 6 steps 🎯 Intermediate 🛠 Fighter Factory 3 + Notepad
0 XP
Level 1
🔥0
Your Progress 0 / 6 steps
1
🤖
What is MUGEN AI?
Understand how CPU characters think in MUGEN
Active
🎯
Goal for this step

Learn what AI is in MUGEN and where the AI code lives inside your character files.

When a player is not controlling a character, MUGEN takes over and runs it as a CPU opponent. The rules for what that CPU does are called the AI Artificial Intelligence.

Without AI code, a CPU character will literally do nothing. It just stands there. Your job is to write simple rules that tell it when to attack, jump, block, or throw a special move.

All the AI code lives in one place your character's .CMD file. It's a plain text file you can open in Notepad or Fighter Factory 3.

FileWhat it controls
.CMDInputs, commands, and AI logic
.CNSStates, physics, damage values
.SFFSprites (images)
.AIRAnimations
.SNDSounds
💡
How a CPU round works: MUGEN reads your AI rules top to bottom every single game tick (there are 60 ticks per second). The first rule that passes gets activated then MUGEN stops checking and acts on it.
🔍
Before You Start: Open your character's folder and find the .CMD file. Open it in Notepad. Scroll all the way to the bottom that's where we'll add all our AI code.
✏️
Fill in the Blanks
+15 XP
All AI code lives inside the file. MUGEN checks AI rules times per second.
🧠
Knowledge Check
+15 XP
What happens if a MUGEN character has no AI code?
AIt uses a random built-in fighting style
BIt copies the opponent's moves
CIt just stands there and does nothing
2
📋
The [AILevel] Header
Add the foundation that tells MUGEN where your AI code starts
Locked
🎯
Goal for this step

Add the [AILevel] header and [Statedef -3] block to the bottom of your .CMD file.

Before you write any moves, you need to tell MUGEN that everything below is AI code. You do this with a special header block.

Scroll to the very bottom of your .CMD file and add this:

; ══════════════════════════ ; AI SECTION add at the bottom of your .CMD file ; ══════════════════════════ [AILevel] value = 1 ; Minimum AI difficulty level to use these rules

Everything you write after this block is AI code. Everything before it is normal player input commands they stay exactly as they are.

⚠️
Important: The [AILevel] header tells MUGEN "only use these rules when the character is CPU-controlled." Without it, your AI triggers might fire during player-controlled rounds too!

Next, you'll write a Statedef block. Every set of AI actions lives inside a Statedef. Start with state -3 this is a special "always active" state that runs no matter what the character is doing:

[Statedef -3] ; -3 = runs in ALL states, including during attacks
💡
State -3 is your best friend for AI. It keeps running even when the character is mid-attack or knocked down. Always use -3 for your AI block.
True or False
+15 XP
The [AILevel] header should go at the bottom of the .CMD file.
Statedef -3 runs in ALL states, even during attacks.
Without [AILevel], AI triggers only fire during CPU rounds.
🧠
Knowledge Check
+15 XP
Why do we use Statedef -3 for AI code instead of another state number?
AIt makes the AI stronger than other statedefs
BIt runs in ALL states, even during attacks and knockdowns
CIt is the only statedef that supports the ChangeState type
3
Writing AI Triggers
Write your first ChangeState action to make the AI attack
Locked
🎯
Goal for this step

Write your first AI trigger block that makes the CPU throw a punch when the opponent is close.

Each AI action is a State Controller that uses a ChangeState type. You tell MUGEN: "If these conditions are true, switch to this state (do this move)."

Here's the basic template for one AI action:

[State -3, MyAIMove] type = ChangeState value = 200 ; the state number of the move to perform trigger1 = AILevel > 0 ; only run as CPU trigger1 = Random < 200 ; 200/1000 chance = 20% trigger1 = ctrl ; character must be free to act
LineWhat it means
value = 200Go to state 200 that's your punch, kick, fireball, etc.
AILevel > 0Only trigger as CPU never for the human player
Random < 200Random number 0-999. Less than 200 = 20% chance per tick
ctrlShort for "control = 1" character isn't in the middle of another move

All triggers on the same number (trigger1) must ALL be true for the action to fire. It's like saying "AND" between each condition.

; A simple jab fires often when close [State -3, AI_Jab] type = ChangeState value = 200 trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = p2bodydist X < 80 ; within 80 pixels horizontally trigger1 = Random < 300
💻
Fill in the Code
+20 XP
Complete this AI trigger to make the CPU throw a kick (state 210) with a 30% chance when in control:
[State -3, AI_Kick] type = value = trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = Random <
💡 Hint: ChangeState switches to a new state. 30% of 1000 = 300.
🧠
Knowledge Check
+15 XP
If all triggers are on trigger1, how do they relate to each other?
AThey ALL must be true (AND logic)
BAny one of them can be true (OR logic)
COnly the first one matters, the rest are ignored
4
🎯
Attack Priorities & Special Moves
Layer your AI rules so supers, specials, and normals fire in the right order
Locked
🎯
Goal for this step

Build a layered attack priority system: supers first, specials second, normals last.

A good AI mixes different attacks normals when close, special moves at range, supers when conditions are right. The key is order put the most important rules first, because MUGEN stops reading as soon as one fires.

; TIER 1: SUPER (rare, only when life is low or has enough power) [State -3, AI_Super] type = ChangeState value = 3000 ; your super move state trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = power >= 3000 ; needs full power bar trigger1 = life < 200 ; only when health is low trigger1 = p2bodydist X < 120 trigger1 = Random < 500 ; ── TIER 2: SPECIAL (medium range, medium chance) [State -3, AI_Fireball] type = ChangeState value = 1000 ; fireball state trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = p2bodydist X > 80 ; far away = use projectile trigger1 = Random < 150 ; ── TIER 3: NORMAL (close range, high chance) [State -3, AI_Jab] type = ChangeState value = 200 trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = p2bodydist X < 80 trigger1 = Random < 300
TriggerWhat it checksExample
p2bodydist XHorizontal distance to opponent< 80 = within punching range
p2bodydist YVertical distance to opponent< -40 = opponent is above you
lifeYour current life points< 200 = dangerously low
p2lifeOpponent's current life< 100 = they're nearly dead
powerYour power/super meter>= 1000 = one bar full
p2statetypeOpponent's stance= A = they're airborne
💡
Rule of thumb: Start supers high, specials middle, normals last. If supers are at the bottom, they'll rarely fire because a normal move will trigger first.
🔢
Put It In Order
+15 XP
Click these AI rule types in the correct priority order (first to last in the .CMD file):
Normal attacks (jab, kick) — close range, high chance
Super moves — rare, needs full power bar
Special moves (fireball) — medium range, medium chance
🧠
Knowledge Check
+15 XP
Why should super moves be placed before normal attacks in your AI code?
ASuper moves do more damage so they need to load first
BMUGEN stops at the first rule that fires, so normals would trigger before supers
CThe game crashes if supers are placed after normals
5
🛡️
Defensive AI — Jumping & Blocking
Make your AI react defensively and move around the stage
Locked
🎯
Goal for this step

Add movement and blocking rules so the AI can dodge, approach, and defend.

A character that only attacks is easy to beat. Real AI also knows when to jump away, back off, or activate blocking. Let's add some defensive behaviour.

Making the AI jump and walk

; Jump backward when health is very low [State -3, AI_JumpBack] type = ChangeState value = 40 ; state 40 = jump trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = life < 150 trigger1 = p2bodydist X < 60 ; opponent very close trigger1 = Random < 80 ; Walk forward when far from opponent [State -3, AI_WalkIn] type = ChangeState value = 20 ; state 20 = walk forward trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = p2bodydist X > 150 ; opponent far away trigger1 = Random < 250

Making the AI block

; Stand block when opponent attacks and is close [State -3, AI_Block] type = ChangeState value = 130 ; state 130 = standing guard trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = p2stateno = 200 ; opponent threw a punch (state 200) trigger2 = p2stateno = 210 ; or a kick (state 210) trigger2 = OR trigger1 = p2bodydist X < 100 trigger1 = Random < 400
⚠️
Trigger1 vs Trigger2: All conditions on the same trigger number must ALL be true (AND). Different trigger numbers are checked separately (OR). So trigger1 = A + trigger1 = B means "A AND B". But trigger2 = C adds another path "OR C".
🔮
Predict What Happens
+15 XP
Your AI has a jump rule with trigger1 = life < 150 and trigger1 = p2bodydist X < 60. The character has 300 life and the opponent is 40 pixels away. What happens?
AThe AI jumps because the opponent is close enough
BThe AI jumps because at least one trigger is true
CThe AI does NOT jump because life is not below 150 (both triggers must be true)
🧠
Knowledge Check
+15 XP
What does trigger2 do differently from trigger1?
AIt fires with double the priority of trigger1
BIt replaces trigger1 if trigger1 fails
CIt adds a separate OR path — an alternative set of conditions
6
🧪
Testing & Tuning Your AI
Launch MUGEN, watch your AI fight, and fine-tune the Random values
Locked
🎯
Goal for this step

Test your AI in Watch mode and tune the Random values until your character fights well.

Now that you have AI code written, it's time to load it into MUGEN and see it in action. Here's the best workflow:

StepWhat to do
1. Save your .CMDMake sure you've saved the file in Notepad or FF3.
2. Launch MUGENOpen MUGEN normally it reloads character files each session.
3. Watch ModeIn MUGEN, go to Watch or set both P1 and P2 to CPU. This lets you observe the AI without playing.
4. Debug ModePress Ctrl+D in a MUGEN match to see real-time debug info state numbers, variables, and triggers firing.
5. Adjust Random valuesIf the AI attacks too much, lower the Random cap. Too passive? Raise it.

Common problems and fixes:

; PROBLEM: AI won't do anything ; FIX: Make sure [AILevel] header is there and Statedef is -3 ; Also check ctrl = 1 character might be stuck in an animation ; PROBLEM: AI does the same move over and over ; FIX: Lower the Random cap on that move, or add distance conditions ; PROBLEM: AI fires moves during player-controlled rounds ; FIX: Make sure every trigger has: trigger1 = AILevel > 0 ; PROBLEM: AI never uses super ; FIX: Check the power condition try printing power in debug ; Also make sure the super block is BEFORE normal attack blocks
💡
Quick tuning guide: Start with every move at Random < 200. After testing, raise the values for moves you want more often, and lower them for moves that feel spammy. A good AI feels unpredictable but fair.
⚠️
AILevel scaling: You can make AI harder on higher difficulty by using AILevel in your Random calculation: trigger1 = Random < (AILevel * 50). At difficulty 1 this is 50, at difficulty 8 it's 400.
; FULL EXAMPLE a simple but functional AI block [AILevel] value = 1 [Statedef -3] ; Walk forward when far [State -3, AI_Walk] type = ChangeState value = 20 trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = p2bodydist X > 120 trigger1 = Random < (AILevel * 40) ; Fireball at range [State -3, AI_Special] type = ChangeState value = 1000 trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = p2bodydist X > 80 trigger1 = Random < (AILevel * 30) ; Jab when close [State -3, AI_Jab] type = ChangeState value = 200 trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = p2bodydist X < 80 trigger1 = Random < (AILevel * 50) ; Jump escape when low health [State -3, AI_Jump] type = ChangeState value = 40 trigger1 = AILevel > 0 trigger1 = ctrl trigger1 = life < 200 trigger1 = p2bodydist X < 60 trigger1 = Random < 60
True or False
+15 XP
You need to restart your computer to reload character files in MUGEN.
Press Ctrl+D during a match to see debug info.
If the AI does the same move over and over, you should lower that move's Random cap.
🧠
Final Knowledge Check
+15 XP
Your AI attacks too aggressively and spams the same move. What should you do?
ALower the Random cap on that move and add distance conditions
BDelete the [AILevel] header
CChange Statedef -3 to Statedef -1
🎉🤖⚡🏆🎮🔥
Workshop Complete!
Your MUGEN character now has working AI! It can attack, defend, and move around the stage on its own. You've learned how to write triggers, layer priorities, and tune behaviour.
0
Total XP
1
Level
0
Best Streak
0%
Accuracy
← Back to MUGEN Workshop ⭐ View My Progress & Certificates

This workshop was free and took many hours to build. If it helped you learn something new, consider supporting the project.

☕ Support on Ko-fi

Grab the Resources

Everything you need to follow along with this workshop.

📄
AI Cheat Sheet
Quick reference for the most common AI triggers distances, life checks, power, and state types. Print and keep it next to you while coding.
🤖
Starter AI Template
A ready-to-paste AI block with walk, jab, special move, and jump escape all with EDIT ME comments. Drop it into any .CMD file.
🎯
Project Brief
A structured task sheet to guide students through adding and tuning AI to their character with achievement targets at each stage.
🔙
Back to Workshop
Return to the main MUGEN Workshop page to explore character creation, Fighter Factory 3, stage building, and more.
Go to Workshop

🛠️ Free In-House Dev Tools

Make It Yours

Use these free browser tools alongside this workshop to create custom sprites, sounds, levels and colour schemes for your game. No installs. Free forever.

👤
Character Creator
Design & customise game characters
🎨
Pixel Studio
Draw sprites & animations
🎨
Colour Palette
Build a game-ready colour scheme
📖
Story Editor
Write branching dialogue
🛠️ See All 20 Free Tools →