Okay, let's design the foundation for a new, open-source visual novel engine built around a State Transition Table (STT) and a Domain-Specific Language (DSL), with the explicit goal of making it impossible to create script-based runtime errors.
This is an ambitious goal, as "impossible" is hard to achieve universally (e.g., hardware failure, OS errors, out-of-memory conditions can still cause runtime issues). However, we can aim to make it impossible to cause crashes or logical errors due to the structure and content of the VN script itself, by validating the script completely before execution begins.
Here's a breakdown of the concept and architecture:
Core Principle: Validation-First, Execution-Second
The key to preventing script-based runtime errors is to move error detection from runtime to a dedicated validation phase that occurs before the game starts or when the script is loaded. The DSL and STT structure facilitate this.
1. The State Transition Table (STT)
This isn't just a concept; it's a concrete data structure that defines all possible valid paths through the story.
-
Structure: A map or table where keys are State IDs (unique identifiers for scenes, points in the narrative, etc.) and values are lists of Allowed Transitions.
-
Allowed Transition: Each entry defines:
- A Trigger: The event that causes the transition (e.g., player selecting choice 'A', a variable
flag_met_character_A being true, an animation finishing, a timer expiring, an explicit goto command).
- A Target State ID: The State ID the engine should transition to upon receiving that trigger.
-
Validation Role:
- Every
Target State ID mentioned in a transition must exist as a key (a defined state) in the STT.
- Every state with non-terminal content must have at least one outgoing transition possibility (unless it's explicitly marked as an ending state).
- The validator can check for unreachable states (optional, but good for design).
- The validator can check for infinite loops within the defined transitions (though this is harder to prove exhaustively without exploring all variable states).
2. The Domain-Specific Language (DSL)
The DSL is how the visual novel author writes the story, defines assets, and describes the content within each state.
-
Syntax: Needs to be clear, concise, and designed to map directly to visual novel concepts. Could be inspired by Ren'Py (Python-like), YAML, or a custom simple syntax.
-
Key Elements the DSL Must Define:
- Asset Declarations: Characters (name, color, sprites), Backgrounds, Music, Sound Effects, Fonts.
- Variables: Declaration of variables used for conditional logic and tracking game state (e.g., flags, counters). The DSL can enforce variable types.
- States (Scenes): Blocks of commands associated with a unique
State ID that matches the STT.
- Commands within States: Instructions for the engine (e.g.,
show character [id] [expression], hide character [id], set background [id], play music [id], stop music, display text "...", set variable [name] = [value]).
- Choice Points: Special commands that present options to the player. Each choice internally maps to a specific
Trigger that is defined in the STT for the current state.
- Conditional Logic: Commands or syntax to define
Triggers based on variable values (e.g., if variable [name] [operator] [value]: transition_trigger [trigger_id]).
- Explicit Transitions: Commands like
goto [State ID] which define a transition that happens immediately when that command is reached. This State ID must also be an Allowed Transition target for the current state in the STT.
-
Validation Role:
- Syntactic Validation: Is the DSL written correctly?
- Semantic Validation:
- Are all referenced assets declared? (e.g.,
show character ren requires ren to be declared).
- Are all referenced variables declared and used with compatible types?
- Are all
State IDs used in goto commands or Target State IDs in transitions actually defined as states?
- Do the
Trigger IDs generated by DSL commands (like choices or if conditions) match the Allowed Transitions defined for the current state in the STT? If a choice maps to trigger_A, the STT entry for the current state have an with .
3. The Engine Components
This engine will be built in a performant, multi-platform language (like Rust, C++, Go) and compile to targets for Web (WASM), Desktop (Native), and Mobile (Native via bindings or frameworks).
-
DSL Parser/Validator:
- Reads the
.vnscript file(s).
- Parses the DSL syntax.
- Constructs the internal STT representation based on state and transition definitions.
- Performs all the semantic validations described above.
- Crucially: If validation fails at any point, it reports a clear error before attempting to run the game. The game simply won't start with an invalid script.
- Outputs a validated, internal representation of the game script and STT, ready for execution. This representation could be a list of states, each containing a sequence of validated commands, linked by the explicit STT.
-
State Machine Runtime:
- Takes the validated script and STT from the parser.
- Maintains the
Current State ID.
- Executes commands sequentially within the
Current State.
- Waits for
Triggers as defined in the STT (e.g., user input, command completion).
- When a trigger occurs, it consults the STT for the
Current State.
- If the trigger matches an
Allowed Transition in the STT for the Current State, it updates the Current State ID to the Target State ID and begins executing commands in the new state.
- If a trigger occurs that is not an
Allowed Transition from the Current State according to the validated STT, this indicates an engine bug (not a script error) because the validator should have prevented this possibility in the script design. The engine runtime should catch this internal inconsistency gracefully.
- Handles persistence (saving/loading the
Current State ID and variable values).
-
Rendering Subsystem:
- Receives rendering commands from the State Machine Runtime (e.g.,
display text, show character).
- Abstracted to work with different platform APIs (WebGL for Web, native graphics APIs for desktop/mobile).
-
Audio Subsystem:
- Receives audio commands (
play music, play sound).
- Abstracted for different platforms.
-
Input Subsystem:
- Captures user input (mouse clicks, touch events, key presses).
How this Prevents Script-Based Runtime Errors:
- No Invalid State Jumps: The STT explicitly defines all valid transitions. The DSL parser and the runtime strictly adhere to this. A script cannot tell the engine to go to a state that doesn't exist or isn't reachable from the current state via a defined trigger.
- Referential Integrity: The DSL parser/validator checks that all assets, variables, and state IDs referenced in the script are declared. You can't call
show character unknown_char.
- Type Safety (within DSL): If the DSL variables are typed, the validator can ensure operations and assignments are type-compatible.
- Constrained Operations: The DSL only provides a limited set of commands designed for VN logic. There's no way to write code that could perform unsafe operations (like accessing arbitrary memory or system calls) because the DSL doesn't have syntax for it.
- Validation Before Execution: The game only runs if the script passes all validation checks against the STT rules and internal consistency.
Multiplatform Strategy:
- Core Logic (State Machine, Parser, Validator): Implement in a language that compiles easily to multiple targets (Rust is excellent for this, C++ is also viable). Compile to:
- Web: WebAssembly (WASM) + JavaScript/WebGL for the rendering layer.
- Desktop: Native executables linked with platform-specific rendering/audio libraries (e.g., SDL, Vulkan, DirectX, Metal).
- Mobile: Native code using platform SDKs or cross-platform frameworks (like SDL, or wrapping with platform views).
- Rendering/Audio/Input Interfaces: Define clear interfaces in the core logic that are implemented by platform-specific backends.
Open Source:
- The entire codebase (Parser, Runtime, Platform Backends, DSL specification) would be released under an open-source license (MIT, Apache 2.0, GPLv3, etc.).
Challenges:
- Complexity of DSL and Validator: Designing a DSL that is expressive enough for complex VNs but restrictive enough for strict validation is hard. The validator needs to be comprehensive.
- "Impossible" is Hard: As mentioned, true impossibility is elusive. The goal is to eliminate script-induced errors. Handling external failures (disk full, network down, driver issues) still requires robust engine-level error handling.
- Performance: A validation phase can add startup time. The runtime needs to be efficient.
- Tooling: Building a good editor or IDE experience for this new DSL and STT structure would be crucial for usability, but is a significant undertaking.
- Extensibility: How do users add custom features? The DSL and STT structure are inherently restrictive. A plugin system would be complex to integrate without compromising the validation guarantee.
Conclusion:
Building a VN engine with an STT-based DSL and validation-first approach to prevent script-based runtime errors is a sound, albeit complex, engineering task. It shifts the burden of error detection entirely onto a pre-execution phase.
- DSL: Defines the content and potential triggers within states.
- STT: Defines the valid flow and required triggers between states.
- Validator: Enforces consistency between the DSL and STT before running.
- Runtime: Executes the already validated instructions according to the STT, handling platform interactions.
This approach provides high confidence in the reliability of the script execution itself, making it a strong candidate if stability and predictable flow are the absolute top priorities, even over maximum flexibility or ease of initial DSL design.