iTranslated by AI
Writing Visual Novel Scenarios with JSX
This is my first post on Zenn. Nice to meet you.
Recently, I played an adventure game that was so interesting it made me want to try making one myself, so I created something like this.
The screen looks a bit like RPG Maker.

Usage
function App() {
const choice = useChoice();
return (
<Game>
<SenarioRenderer>
<Text>Text</Text>
<Choice choices={["Choice 1", "Choice 2"]} />
<Branch if={choice.answer == "Choice 1"}>
<Text>Choice 1 was selected</Text>
</Branch>
<Branch if={choice.answer == "Choice 2"}>
<Text>Choice 2 was selected</Text>
</Branch>
</SenarioRenderer>
<ScreenRenderer>
<MessageWindow />
</ScreenRenderer>
</Game>
);
}
Basic Components
-
Game- The top-level wrapper component.
- Internally, it just arranges
Context.Providers.
-
SenarioRenderer- A component for writing scenarios.
- A custom renderer converts the passed
childreninto JSON.
-
ScreenRenderer- A component for placing elements on the screen.
- Internally, it mostly just places the
Stagefrom@inlet/react-pixi.
For other details, please check App.tsx in the test directory or the core implementation.
Why I Made It
I think there are already many tools, such as RPG Maker or TyranoScript, designed to make games with a somewhat fixed format as easily as possible.
Some of these tools allow for extensions using JavaScript and other languages. However, I feel that the more elaborate you try to be, the higher the ratio of such extensions becomes, making the implementation harder to manage as the extensions and the original syntax get mixed together.
So, I thought it would be good to have something that can be written entirely in JavaScript, and that's why I created this.
What I Did
Most of it relies on libraries and isn't particularly difficult, but I'll write down some of the things I researched and considered.
Implementing a Custom React Renderer (react-reconciler)
To write scenarios in JSX, I created a dedicated renderer.
To create a custom renderer in React, you need to pass an object called HostConfig, which implements various methods, to react-reconciler. The methods implemented in HostConfig are called during React's rendering process to handle tree generation, diffing, and applying updates.
Although it's not the latest information, the following article was very helpful:
You can see actual implementations of HostConfig in repositories like these:
Component Design for Scenario Description
To keep the custom renderer implementation simple, I only used the following two rendering components (equivalent to DOM elements in react-dom):
-
Text- Writing
<Text>Text</Text>creates the text to be rendered on the screen.
- Writing
-
Action- Executes the passed function and moves to the next element.
In other words, the final rendering result looks like this:
[
{ type: "Text", message: "Text" },
{ type: "Action", action: ... },
{ type: "Text", message: "Text" },
{ type: "Text", message: "Text" }
]
These are displayed or executed one by one in response to actions like clicking on the message window.
Therefore, scenario components other than Text are implemented by wrapping Action.
For example, the Choice component for displaying options looks like this:
function Choice({ choices }) {
const choice = useChoice();
return <Action action={() => choice.setChoices(choices)} />;
}
Problem: Context Cannot be Shared Between Different Renderers
Currently, when executed, the following warning is displayed in the console:
Warning: Detected multiple renderers concurrently rendering the same context provider. This is currently unsupported.
This is caused by trying to share context between my custom renderer and the screen rendering renderer (@inlet/react-pixi).
Originally, it seems that sharing context between different renderers is allowed by setting isPrimaryRenderer to false in HostConfig.
However, in this case, there are three: react-dom, the custom renderer, and the screen rendering renderer. Sharing between react-dom and each of the others doesn't trigger a warning, but a warning appears because the latter two are trying to use the same context.
- react-dom
- @inlet/react-pixi renderer
- Custom renderer
Since there seems to be no solution at the moment, I'm leaving it as is until a major problem occurs.
Conclusion
I'm satisfied because I was able to achieve most of what I originally envisioned. By working with the Reconciler, I feel like my understanding of React has deepened as well.
Since the implementation is quite rough, if you think you can do it better, please go ahead and make one. And let me use it.
Discussion