Performance: State machines for AI behavior
MEDIUM IMPACT
This affects the responsiveness and smoothness of AI character actions during gameplay, impacting frame rate and input responsiveness.
enum AIState { Idle, Chasing, Attacking }
AIState currentState = AIState.Idle;
void Update() {
switch (currentState) {
case AIState.Idle:
CheckForPlayer();
break;
case AIState.Chasing:
MoveTowardsPlayer();
break;
case AIState.Attacking:
AttackPlayer();
break;
}
}
void ChangeState(AIState newState) {
currentState = newState;
}void Update() {
if (isIdle) {
CheckForPlayer();
} else if (isChasing) {
MoveTowardsPlayer();
} else if (isAttacking) {
AttackPlayer();
}
// Multiple boolean flags checked every frame
}| Pattern | CPU Checks per Frame | State Transition Cost | Frame Impact | Verdict |
|---|---|---|---|---|
| Multiple boolean flags | Multiple checks | High (inconsistent) | Can cause frame drops | [X] Bad |
| Enum-based state machine | Single check | Low (clear transitions) | Smooth frame rate | [OK] Good |