# Conversation Engine Design for Unity 6
This document outlines the design of a modular conversation engine for Unity 6, using C# and GameObjects. The system is designed for flexibility, scalability, and ease of use by game designers, allowing them to add only the necessary features for each conversation. It supports branching conversations, multiple-choice answers, and NPC memory, with plans for future extensions like inventory checks and conditional branching.
## System Design Overview
### Core Objectives
- **Modular**: Each feature (e.g., branching, memory, inventory checks) is a separate script (Component) that can be added to conversation nodes as needed.
- **Hierarchical**: Uses Unity’s GameObject hierarchy to organize NPCs, conversation managers, conversations, and nodes.
- **Flexible**: Supports branching conversations with multiple-choice answers, start/end nodes, and future extensions like memory or inventory checks.
- **Persistent**: NPCs track past interactions and shared information using a lightweight memory system.
### GameObject Hierarchy
NPC (GameObject)
├── ConversationManager (GameObject)
│ ├── Conversation_1 (GameObject)
│ │ ├── StartNode (GameObject)
│ │ ├── DialogueNode (GameObject)
│ │ ├── ChoiceNode (GameObject)
│ │ └── EndNode (GameObject)
│ ├── Conversation_2 (GameObject)
│ │ ├── StartNode (GameObject)
│ │ └── ... (other nodes)
└── Mesh (GameObject, visual component)
- **NPC**: Root GameObject representing the character.
- **ConversationManager**: GameObject with a `ConversationManager` script, managing all conversations and tracking interaction history.
- **Conversation**: GameObject representing a single conversation, with a `Conversation` script to manage nodes and metadata (e.g., completion status).
- **Nodes**: GameObjects under a Conversation, each with a specific node script (e.g., `StartNode`, `DialogueNode`, `ChoiceNode`, `EndNode`).
### Core Components
- **ConversationManager (Script)**:
- Manages all conversations for the NPC.
- Tracks started/completed conversations.
- Stores NPC memory (e.g., key-value pairs for remembered facts).
- Provides an interface to start conversations and retrieve current nodes.
- **Conversation (Script)**:
- Manages a single conversation’s state (e.g., current node, completion status).
- References the start node to begin the conversation.
- Validates the hierarchy (ensures a start node and at least one end node).
- **Node Types (Scripts)**:
- `StartNode`: Marks the conversation’s entry point, links to the next node.
- `DialogueNode`: Displays dialogue text and links to the next node.
- `ChoiceNode`: Presents multiple-choice options, each linking to a different node.
- `EndNode`: Ends the conversation and optionally triggers actions (e.g., updates NPC memory).
- **Node Base Class**:
- A base class (`BaseNode`) that all node types inherit from, defining:
- Linking to next node(s).
- Processing node logic (e.g., displaying text, evaluating conditions).
- Triggering UI updates or game events.
### Conversation Flow
- Player interacts with the NPC, triggering `ConversationManager` to select a conversation (e.g., based on conditions like “first meeting”).
- `Conversation` script starts at the `StartNode`.
- Each node processes its logic:
- `DialogueNode`: Displays text, moves to the next node automatically.
- `ChoiceNode`: Shows options, waits for player input, then moves to the chosen node.
- `EndNode`: Concludes the conversation, updates `ConversationManager` memory, or triggers actions.
- UI (separate from hierarchy) displays dialogue and choices, communicating with the current node via events or a controller script.
### Modularity and Extensibility
- **Node Modifiers (Scripts)**:
- Each feature (e.g., memory, inventory checks) is a MonoBehaviour script attached to a node.
- Examples:
- `MemoryModifier`: Stores a key-value pair in `ConversationManager`’s memory node is reached (e.g., “told_player = true”).
- `InventoryCheckModifier`: Checks for a specific item before allowing a branch.
- `TimeCheckModifier`: Checks in-game time or weather for branching.
- `RandomBranchModifier`: Randomly selects the next node.
- Modifiers implement `INodeModifier` with a method like `EvaluateProcessNode(BaseNode)` to modify behavior for a node.
- **Attaching Modifiers**:
- Designers add modifier scripts to node GameObjects in the Unity Editor.
- `BaseNode` checks for modifiers and executes them before default logic.
- Example: A `ChoiceNode` with `anInventoryCheckModifier` disables a choice if the player lacks an item.
- **Why Modifiers?**:
- Keeps core node types simple and focused.
- Allows mixing and matching features without duplicating node types.
- Enables easy addition of new features.
### NPC Memory System
- Stored in `ConversationManager` as a `Dictionary<string, object>` for flexibility.
- Example entries:
- `"first_meeting_done"`: true
- `"told_lore_secret"`: true
- `"last_conversation_id"`: "quest_conversation_1"
`
- Updated by `MemoryModifier` scripts when nodes are processed.
- Queried by `MemoryCheckModifier` to lock/unlock branches (e.g., skip dialogue if `first_meeting_done == true`).
- Persistence: Serialize to file or `PlayerPrefs` for session reloading.
### UI Integration
- `DialogueUIManager` (Script separate from hierarchy):
- Renders dialogue text (`DialogueNode`) and choice buttons (`ChoiceNode`).
- Subscribes to `ConversationManager` events to display current node content.
- Sends player input (e.g., choice selection) to advance conversation.
### Designer Workflow
- **In Unity’s Hierarchy**:
- Drag-and-drop GameObjects to create conversations and nodes.
- Attach node scripts (e.g., DialogueNode`, ChoiceNode)`).
- Configure in Inspector:
- `DialogueNode`: Set dialogue text, next node.
- `ChoiceNode`: Add choices with text, and node references.
- Add modifiers (e.g., `InventoryCheckModifier`) and configure settings.
- Use prefabs for reusable conversations or node templates.
- **Validation**:
- `Conversation` script validates node graph in Editor (e.g., ensures start node and end node exist).
- Visual feedback (e.g., Gizmos, custom Editor tools) to highlight missing connections.
## Future Extensions
### Planned Modules (Modifiers)
1. **MemoryModifier**:
- Adds key-value pair to memory node is reached (e.g., `key = "gave_quest_item", value = true`).
2. **MemoryCheckModifier**:
- Checks memory key and enables/disables a node or choice on value.
- e.g., Allow “What’s next?” only if `gave_quest_item == true`.
3. **InventoryCheck**Modifier:
- Queries player inventory for an item.
- e.g., Example: Allow choice if player has “Golden Key”.
4. **TimeCheck**Modifier**:
- Checks in-game time or weather.
- e.g., Example: Branch if `time >= 18:00` or `weather == rainy`.
5. **RandomBranchModifier**:
- Randomly selects next node from weighted list.
- e.g., `70% neutral response, 30% rare hint.
### Additional Features
- **Localization**: Support localized dialogue via string table or file.
- **Animation/Sound**: Trigger animations or sounds with `AnimationTriggerModifier` or `SoundModifier`.
- **Quest Integration**: Use `QuestModifier` to update quest states.
- **Dynamic Nodes**: Allow runtime node creation for procedural NPCs.
## Implementation Notes
- **Why GameObjects?**
- Leverages Unity’s hierarchy and Inspector for intuitive design.
- Visualizes conversation flow in Scene view.
- Simplifies attaching components.
- **Why Modifiers?**
- Follows Single Responsibility Principle: Nodes handle flow, modifiers handle logic.
- Reduces complexity: No monolithic node script.
- Enables drag-and-drop customization.
- **Performance**:
- Keep node processing lightweight (e.g., avoid heavy `Update` logic).
- Cache references in `Awake` or `OnEnable`.
- Pool UI elements to reduce instantiation.
- **Error Handling**:
- Validate node connections in Editor.
- Fallback behavior for errors (e.g., missing next node uses `EndNode`).
- **Testing**:
- Debug UI for simulating conversations in Editor - Play Mode for testing branching and modifiers.
- Unit tests for modifier scripts.
## Next Steps
1. **Base Classes**:
- Create `IBaseNode` interface and `BaseNode` abstract class.
- Implement `Conversation` and `Conversation` scripts.
2. **Core Nodes**:
- Implement `StartNode`, `DialogueNode`, `ChoiceNode`, `EndNode`.
- Set up node linking and flow logic.
3. **UI Integration**:
- Create `DialogueUIManager` for text and choice display.
- Connect to `ConversationManager` via events.
4. **Modifiers**:
- Start with `MemoryModifier` and `MemoryCheckModifier` for memory.
- Add `InventoryCheckModifier` once inventory system defined.
5. **Editor Tools**:
- Add validation in `Conversation` for Editor feedback.
- Create custom Inspector UI for nodes for configuration.
This design provides a robust foundation for a conversation engine, with clear paths for modular expansion. Let me know if deeper exploration of any section is needed or when ready for Unity C# C# code to begin implementation!