Hello. You are an experienced C#/.NET developer specializing in Unity game development.…
- User
Hello. You are an experienced C#/.NET developer specializing in Unity game development. Design a good and versatile visual-novel-like narration system that would allow for extensive narrative customization, multiple choice questions that save the answers given and conditional parts of the narrative (things that only happen under a certain logical assertion). Keep things like SOLID, DRY and GoF design patterns in mind.
- Assistant
Introduction
Designing a versatile visual novel narration system in Unity requires careful consideration of both architectural patterns and software design principles to ensure the system is extensible, maintainable, and efficient. This system should support extensive narrative customization, accommodate multiple-choice questions with persistent answers, and handle conditional narrative flows based on logical assertions.
Below, I present a design that leverages SOLID principles, adheres to the DRY (Don't Repeat Yourself) principle, and incorporates relevant Gang of Four (GoF) design patterns.
High-Level Architecture
-
Narrative System Core
- NarrativeManager: Central controller managing the flow of the narrative.
- INarrativeElement: Interface representing a generic narrative element.
- NarrativeElementBase: Abstract base class implementing common functionality.
- TextElement, ChoiceElement, ConditionalElement: Concrete implementations for different narrative elements.
-
Data Management
- ScriptableObjects: For storing narrative data and choices.
- State Management: Tracking player choices and game state.
-
UI Components
- NarrativeUIController: Manages UI updates based on the narrative flow.
- ChoiceUI, TextUI: UI elements for displaying text and choices.
-
Event System
- EventManager: Handles events like advancing the narrative, making choices, etc.
Detailed Design
1. Narrative Elements
-
INarrativeElement (Interface)
public interface INarrativeElement { void Execute(NarrativeManager manager); }- Ensures that all narrative elements can be executed by the
NarrativeManager. - SOLID - Interface Segregation Principle (ISP): Defines a specific interface for narrative elements.
- Ensures that all narrative elements can be executed by the
-
NarrativeElementBase (Abstract Class)
public abstract class NarrativeElementBase : ScriptableObject, INarrativeElement { public abstract void Execute(NarrativeManager manager); }- Provides common functionality and fields.
- SOLID - Liskov Substitution Principle (LSP): Allows derived classes to be substitutable for the base class.
-
TextElement (Concrete Class)
[CreateAssetMenu(menuName = "Narrative/TextElement")] public class TextElement : NarrativeElementBase { public string text; public override void Execute(NarrativeManager manager) { manager.DisplayText(text); } }- Represents simple text in the narrative.
- GoF - Factory Pattern: Using
CreateAssetMenuacts like a factory for creating narrative elements.
-
ChoiceElement (Concrete Class)
[CreateAssetMenu(menuName = "Narrative/ChoiceElement")] public class ChoiceElement : NarrativeElementBase { public List<ChoiceOption> options; public override void Execute(NarrativeManager manager) { manager.DisplayChoices(options); } } [System.Serializable] public class ChoiceOption { public string text; public NarrativeElementBase nextElement; public string variableToSet; public string valueToSet; }- Represents a multiple-choice question.
- Stores the player's choice and advances the narrative accordingly.
- SOLID - Single Responsibility Principle (SRP): Each class has a single responsibility.
-
ConditionalElement (Concrete Class)
[CreateAssetMenu(menuName = "Narrative/ConditionalElement")] public class ConditionalElement : NarrativeElementBase { public string conditionVariable; public string expectedValue; public NarrativeElementBase trueElement; public NarrativeElementBase falseElement; public override void Execute(NarrativeManager manager) { if(manager.CheckCondition(conditionVariable, expectedValue)) { trueElement.Execute(manager); } else { falseElement.Execute(manager); } } }- Allows branching based on conditions.
- GoF - Chain of Responsibility Pattern: Passes execution to the next appropriate element.
2. Narrative Manager
- NarrativeManager (Singleton)
public class NarrativeManager : MonoBehaviour { private static NarrativeManager _instance; public static NarrativeManager Instance => _instance; private Dictionary<string, string> gameState = new Dictionary<string, string>(); public void StartNarrative(NarrativeElementBase startingElement) { startingElement.Execute(this); } public void DisplayText(string text) { // Update UI } public void DisplayChoices(List<ChoiceOption> options) { // Update UI with choices } public void RecordChoice(string variable, string value) { gameState[variable] = value; } public bool CheckCondition(string variable, string expectedValue) { return gameState.TryGetValue(variable, out var value) && value == expectedValue; } }- Manages the execution flow of the narrative.
- Singleton Pattern: Ensures a single instance manages the narrative.
- SOLID - Dependency Inversion Principle (DIP): High-level module (
NarrativeManager) does not depend on low-level modules; both depend on abstractions.
3. UI Controllers
- NarrativeUIController
- Handles the display and user input for narrative elements.
- Listens to events from the
NarrativeManager. - Observer Pattern: UI components observe changes in the narrative state.
4. Event System
- EventManager
- Decouples components by managing events.
- Components can subscribe to events they are interested in.
- GoF - Observer Pattern: Promotes loose coupling.
Applying SOLID Principles
-
Single Responsibility Principle (SRP)
- Each class and component has a single responsibility.
TextElementhandles displaying text.ChoiceElementmanages choices.ConditionalElementhandles conditional branching.
-
Open/Closed Principle (OCP)
- The system can be extended with new narrative elements without modifying existing code.
- New elements can implement
INarrativeElement.
-
Liskov Substitution Principle (LSP)
- All narrative elements can be substituted with each other when they implement the same interface (
INarrativeElement).
- All narrative elements can be substituted with each other when they implement the same interface (
-
Interface Segregation Principle (ISP)
- Interfaces are specific to what clients need.
INarrativeElementis tailored for narrative elements.
-
Dependency Inversion Principle (DIP)
- High-level modules (
NarrativeManager) and low-level modules (Narrative Elements) depend on abstractions (INarrativeElement).
- High-level modules (
Adhering to DRY
- Reusable components like
NarrativeElementBaseprevent code duplication. - Common functionality is abstracted into base classes and utility methods.
- ScriptableObjects store data, allowing reuse and easy modification without altering code.
Incorporating GoF Design Patterns
-
Factory Pattern
- Using
ScriptableObject.CreateInstanceorCreateAssetMenuto create narrative elements.
- Using
-
Observer Pattern
- UI components observe changes in the narrative state and update accordingly.
-
Chain of Responsibility Pattern
ConditionalElementpasses execution to the next element based on conditions.
-
Singleton Pattern
NarrativeManagermaintains a single instance managing the narrative flow.
Extensibility and Customization
-
Adding New Elements
- New narrative elements can be added by creating classes that implement
INarrativeElement.
- New narrative elements can be added by creating classes that implement
-
Custom Conditions and Actions
- Conditions and actions can be extended by creating new types of
ConditionalElementor by enhancing the condition checking logic.
- Conditions and actions can be extended by creating new types of
-
Localization Support
- Text can be loaded through a localization system to support multiple languages.
-
Modular Narrative Blocks
- Narrative elements can be organized into reusable blocks or sequences.
Example Usage
// Starting the narrative public class GameStarter : MonoBehaviour { public NarrativeElementBase startingElement; void Start() { NarrativeManager.Instance.StartNarrative(startingElement); } }
Conclusion
This design provides a flexible and extensible framework for a visual novel narration system in Unity. By leveraging SOLID principles, we ensure that the system is maintainable and scalable. The use of GoF design patterns like Factory, Observer, Chain of Responsibility, and Singleton facilitates robust and efficient code structure.
Implementing the system with these considerations allows for extensive narrative customization, supports multiple-choice questions with persistent state management, and enables conditional narrative flows, all while maintaining clean and reusable code adhering to the DRY principle.
Next Steps
-
Implement Serialization
- To save and load game states, implement serialization of the
gameStatedictionary.
- To save and load game states, implement serialization of the
-
Enhance UI Components
- Develop rich UI elements to display text, choices, and handle user interactions smoothly.
-
Testing
- Write unit tests for narrative elements to ensure reliability.
-
Performance Optimization
- Profile the system to optimize performance, especially if dealing with large narratives.
-