Extension - Object State in Games
Use objects, encapsulation, and class variables to model game characters whose state changes, then save and load it.
- Unit 3: AI Pipelines and State Persistence in Games
- Key Topics
- The AI Pipeline
- Tech Talk
- A. An NPC’s memory. Each object keeps its own state
- Code Runner Challenge
- B. Encapsulation. Why
privateprotects the save file - Code Runner Challenge
- C. Class variables. State shared by the whole class
- Code Runner Challenge
- D. The AI pipeline in code. State makes the same input give a different result
- Code Runner Challenge
- E. Saving and loading. Turning state into text and back
- Popcorn Hack
- MCQ Check
- Homework Hack
Unit 3: AI Pipelines and State Persistence in Games
Store an NPC’s state in private instance variables, protect it with constructors and methods, and save it so it survives a restart.
Reference Guide
Key Topics
| Term | Definition | Example |
|---|---|---|
| Instance variable | Field storing one object’s state. | private int health; |
| Encapsulation | private data, controlled methods. |
private int health; |
| Persistence | Saving state so it survives a restart. | toSaveString() |
The AI Pipeline
Every game tick, each NPC runs this loop. Without instance variables, update state has nowhere to store what the NPC learned.
College Board Requirements
AP CSA Unit 3: Class Creation, about 10-18% of the multiple-choice section (College Board, 2025, p. 18). Topics covered:
- 3.1-3.4 Program design, anatomy of a class, constructors (A, B, E)
- 3.5 Writing methods (B, D, E)
- 3.7 Class variables and methods (C)
- 3.8-3.9 Scope and access,
this(A, B)
Class Design is free-response question 2 (College Board, 2025, p. 176). Inheritance is outside AP CSA (p. 45), so this lesson skips it.
Lesson Plan
Learning Objective: Write a class that keeps an NPC’s state in private instance variables, sets it in a constructor, and changes it only through validated methods.
Success Criteria: You can explain why objects of one class hold different state, protect it with private, and turn it into a save string and back.
Tech Talk
An NPC is a save file with legs. The AI reads its instance variables to decide and writes new state when the world changes, so a guard attacks you after you steal even though you have not moved. Only the class should change its own state, and every object must start valid.
Code Examples
A. An NPC’s memory. Each object keeps its own state
In UML, - means private and + means public. this.name = name; assigns the parameter to the instance variable.
Run it, then make the guard take much more damage and predict the output
public class NPC {
private String name;
private int health;
private String state;
public NPC(String name) {
this.name = name;
this.health = 100;
this.state = "WANDER";
}
public void takeDamage(int amount) {
health -= amount;
}
public void describe() {
System.out.println(name + ": " + health + " HP, state=" + state);
}
public static void main(String[] args) {
NPC guard = new NPC("Guard Bob");
NPC villager = new NPC("Villager Sam");
guard.takeDamage(40);
guard.describe();
villager.describe();
}
}
Code Runner Challenge
Run it, then make the guard take much more damage and predict the output
View IPYNB Source
// CODE_RUNNER: Run it, then make the guard take much more damage and predict the output
public class NPC {
private String name;
private int health;
private String state;
public NPC(String name) {
this.name = name;
this.health = 100;
this.state = "WANDER";
}
public void takeDamage(int amount) {
health -= amount;
}
public void describe() {
System.out.println(name + ": " + health + " HP, state=" + state);
}
public static void main(String[] args) {
NPC guard = new NPC("Guard Bob");
NPC villager = new NPC("Villager Sam");
guard.takeDamage(40);
guard.describe();
villager.describe();
}
}
NPC.main(null);
Notice: damaging the guard does not touch the villager.
B. Encapsulation. Why private protects the save file
Java protects your state from impossible values. In any other class, boss.health = -50; will not compile, so the only legal way to change health is a method you control, like the clamp in takeDamage.
Run it, then add a heal(int amount) method that never goes above 100
public class SafeNPC {
private int health;
public SafeNPC() {
this.health = 100;
}
public void takeDamage(int amount) {
health -= amount;
if (health < 0) {
health = 0; // clamp: no negative HP, ever
}
}
public int getHealth() {
return health;
}
public static void main(String[] args) {
SafeNPC boss = new SafeNPC();
boss.takeDamage(30);
boss.takeDamage(9999);
System.out.println("Health: " + boss.getHealth());
// In ANOTHER class, boss.health = -50; would be a COMPILE ERROR
}
}
Code Runner Challenge
Run it, then add a heal(int amount) method that never goes above 100
View IPYNB Source
// CODE_RUNNER: Run it, then add a heal(int amount) method that never goes above 100
public class SafeNPC {
private int health;
public SafeNPC() {
this.health = 100;
}
public void takeDamage(int amount) {
health -= amount;
if (health < 0) {
health = 0; // clamp: no negative HP, ever
}
}
public int getHealth() {
return health;
}
public static void main(String[] args) {
SafeNPC boss = new SafeNPC();
boss.takeDamage(30);
boss.takeDamage(9999);
System.out.println("Health: " + boss.getHealth());
// In ANOTHER class, boss.health = -50; would be a COMPILE ERROR
}
}
SafeNPC.main(null);
C. Class variables. State shared by the whole class
npcCount is a class variable (one copy total) and id is an instance variable (one per object).
Run it, then create one more TrackedNPC and predict the new count
public class TrackedNPC {
private static int npcCount = 0; // shared by the class
private int id; // one per object
public TrackedNPC() {
npcCount++;
id = npcCount;
}
public static int getNpcCount() {
return npcCount;
}
public static void main(String[] args) {
new TrackedNPC();
new TrackedNPC();
new TrackedNPC();
System.out.println("NPCs: " + TrackedNPC.getNpcCount());
}
}
Code Runner Challenge
Run it, then create one more TrackedNPC and predict the new count
View IPYNB Source
// CODE_RUNNER: Run it, then create one more TrackedNPC and predict the new count
public class TrackedNPC {
private static int npcCount = 0; // shared by the class
private int id; // one per object
public TrackedNPC() {
npcCount++;
id = npcCount;
}
public static int getNpcCount() {
return npcCount;
}
public static void main(String[] args) {
new TrackedNPC();
new TrackedNPC();
new TrackedNPC();
System.out.println("NPCs: " + TrackedNPC.getNpcCount());
}
}
TrackedNPC.main(null);
D. The AI pipeline in code. State makes the same input give a different result
Same distances, different behavior after the theft. Only grudge changed.
Run it, then change the witnessTheft() amount to 3 and predict what happens
public class GrudgeNPC {
private int grudge; // memory of the player
private String state;
public GrudgeNPC() {
this.grudge = 0;
this.state = "WANDER";
}
public void witnessTheft() {
grudge += 5;
}
public void tick(int distance) {
if (grudge < 5) {
state = "WANDER";
} else if (distance < 4) {
state = "ATTACK";
} else {
state = "SEARCH";
}
System.out.println("grudge=" + grudge + ", distance " + distance + " -> " + state);
}
public static void main(String[] args) {
GrudgeNPC bob = new GrudgeNPC();
bob.tick(2);
bob.witnessTheft();
System.out.println("--- player stole something ---");
bob.tick(8);
bob.tick(2);
}
}
Code Runner Challenge
Run it, then change the witnessTheft() amount to 3 and predict what happens
View IPYNB Source
// CODE_RUNNER: Run it, then change the witnessTheft() amount to 3 and predict what happens
public class GrudgeNPC {
private int grudge; // memory of the player
private String state;
public GrudgeNPC() {
this.grudge = 0;
this.state = "WANDER";
}
public void witnessTheft() {
grudge += 5;
}
public void tick(int distance) {
if (grudge < 5) {
state = "WANDER";
} else if (distance < 4) {
state = "ATTACK";
} else {
state = "SEARCH";
}
System.out.println("grudge=" + grudge + ", distance " + distance + " -> " + state);
}
public static void main(String[] args) {
GrudgeNPC bob = new GrudgeNPC();
bob.tick(2);
bob.witnessTheft();
System.out.println("--- player stole something ---");
bob.tick(8);
bob.tick(2);
}
}
GrudgeNPC.main(null);
E. Saving and loading. Turning state into text and back
flowchart LR
A["NPC object"] -->|toSaveString| B[("Save file")]
B -->|read values back| C["New NPC object"]
A real game writes this text to a file, later parses it back into values, and builds a new object. The constructor validates every value it receives, so a corrupted save cannot create an impossible NPC. (split and Integer.parseInt are standard Java but not on the exam.)
public class SavableNPC {
private int health;
private String state;
public SavableNPC(int health, String state) {
if (health > 100) {
health = 100; // validate: cap a corrupted value
}
this.health = health;
this.state = state;
}
// SAVE: turn the object's state into text
public String toSaveString() {
return state + "," + health;
}
// LOAD: build a brand-new object from saved text
public static SavableNPC fromSaveString(String line) {
String[] parts = line.split(",");
return new SavableNPC(Integer.parseInt(parts[1]), parts[0]);
}
}
Try it on paper:
new SavableNPC(62, "ATTACK").toSaveString()returnsATTACK,62SavableNPC.fromSaveString("ATTACK,62")returns a new NPC with health 62 and stateATTACKSavableNPC.fromSaveString("ATTACK,9999")returns a new NPC with health 100, because the constructor validated it
Hacks & Practice Tasks
Popcorn Hack
2-minute challenge: design a Companion with a name, a loyalty from 0 to 10, and whether it is following you. Make them private, set them in a constructor, and use your own values. Compare with this sample, then write yours in your homework notebook as a // CODE_RUNNER: cell ending with Companion.main(null);.
// Practice #1 - Try it yourself first!
public class Companion {
// Sample answer:
private String name;
private int loyalty;
private boolean isFollowing;
public Companion(String name) {
this.name = name;
this.loyalty = 5;
this.isFollowing = true;
}
public void describe() {
System.out.println(name + ", loyalty=" + loyalty + ", following=" + isFollowing);
}
public static void main(String[] args) {
new Companion("Pixel").describe();
}
}
MCQ Check
4 questions, one at a time. Copy the score line into your notes.
Homework Hack
Task: Build a Shopkeeper with private name, gold, and mood, starting at 100 gold and mood 5, with mood kept between 0 and 10, and a save string.
Skeleton: copy it into a // CODE_RUNNER: cell in your homework notebook, fill in the blanks, and end the cell with Shopkeeper.main(null);.
public class Shopkeeper {
// 1. Three PRIVATE variables: name, gold, mood
// 2. Constructor: gold starts at 100, mood at 5
public Shopkeeper(String name) {
}
// 3. Add to mood, but keep it between 0 and 10
public void changeMood(int amount) {
}
public int getMood() {
return 0;
}
// 4. Return name,gold,mood (example: Mira,100,5)
public String toSaveString() {
return "";
}
public static void main(String[] args) {
Shopkeeper mira = new Shopkeeper("Mira");
mira.changeMood(20);
System.out.println(mira.getMood());
mira.changeMood(-99);
System.out.println(mira.getMood());
System.out.println(mira.toSaveString());
}
}
References
College Board. (2025). AP Computer Science A course and exam description [Effective fall 2025]. https://apcentral.collegeboard.org/media/pdf/ap-computer-science-a-course-and-exam-description-effective-fall-2025.pdf
Kaczmarczyk, L. C., Petrick, E. R., East, J. P., & Herman, G. L. (2010). Identifying student misconceptions of programming. In SIGCSE ‘10 (pp. 107-111). ACM.