iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📖

Writing Visual Novel Scenarios with JSX

に公開
2024/04/11

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.

https://github.com/mkizka/advx-poc

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 children into JSON.
  • ScreenRenderer
    • A component for placing elements on the screen.
    • Internally, it mostly just places the Stage from @inlet/react-pixi.

For other details, please check App.tsx in the test directory or the core implementation.

https://github.com/mkizka/advx-poc/blob/main/playground/src/App.tsx

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:

https://blog.atulr.com/react-custom-renderer-1/

You can see actual implementations of HostConfig in repositories like these:

https://github.com/facebook/react/blob/main/packages/react-art/src/ReactARTHostConfig.js
https://github.com/inlet/react-pixi/blob/master/src/reconciler/hostconfig.js
https://github.com/jiayihu/react-tiny-dom/blob/master/renderer/tiny-dom.js

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.
  • 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.

https://github.com/facebook/react/issues/17275

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.

GitHubで編集を提案

Discussion