Code Monkey home page Code Monkey logo

yarnmachine's Introduction

YarnMachine

Standalone C++ virtual machine for Yarn Spinner.


Notes:

There may be commands, markup, etc. built into the "real" Yarn interpreter for Unity or other platforms that are not supported / have to be added yourself. This is a generic runtime so any commands that affect UI / audio / visuals are meant to be bring-your-own as custom commands.

The accompanying terminal sample shows basic usage of the API.

Some prototype code showing how you might make a more advanced dialogue runner with an IMGUI widget for a real time graphical application can be found at my gist here: https://gist.github.com/VirtuosoChris/dad8a3e006e6e325964e55507323e41f It's 1) prototype code, so not guaranteed to be optimal in any way 2) lifted right out of my game so you won't be able to just build and run this but it's provided as an example / proof of concept for how you might do this.


Build Instructions :

Meant to be built using cmake; all dependencies should automatically build and add to the project.

If you open the repo using the open in desktop link, it ought to automatically fetch all submodules. If it doesn't or if you use the command line, you may get errors for protobuf missing a CMakeLists.txt, and find the directory is empty. In this event, use

git submodule update --init --recursive

When you first build, the protobuf lib takes a bit to build the first time so be patient.

The cmake should also automatically run protoc on the yarn.proto file to regenerate up to date headers


About:

Yarn Spinner is a neat scripting language for branching narrative that is similar to INK and is intuitive because it kind of just resembles a linear screenplay.

https://yarnspinner.dev/

This project is meant to be a standalone (non Unity, engine agnostic) C++ virtual machine and dialogue player that interprets the compiled Yarn script files.

The Github project for the original Yarn Spinner (not affiliated) can be found here: https://github.com/YarnSpinnerTool/YarnSpinner

and an online sandbox to try the language can be found at: https://try.yarnspinner.dev/

and the Visual Studio Code plugin (which is my preferred means of authoring scripts) can be found at : https://marketplace.visualstudio.com/items?itemName=SecretLab.yarn-spinner

This project doesn't interpret .yarn scripts directly; it depends on 3 input files generated by the Yarn Compiler toolchain. So you'll either need to provide these files with your game or ship the yarn compiler with your game.

  1. compiled .yarnc binary files. The compiled .yarnc files are generated as Google Protobufs https://developers.google.com/protocol-buffers/docs/reference/cpp-generated

So protobuf is a dependency and protoc is used to generate parsing code from the original yarn_spinner.proto file that defines the Yarn Spinner bytecode format.

2 / 3) the two generated csv files containing the line database, and the metadata. I am using the following C++ CSV parser as an additional dependency:

https://github.com/vincentlaucsb/csv-parser

Additional Notes, differences from "real" Yarn Spinner:

  • All the things mentioned in the tutorial at https://docs.yarnspinner.dev/getting-started/writing-in-yarn were implemented, tested, and should work; beyond that we have serialization including deterministic RNG.
  • Since this is C++ / engine agnostic, GUI / user interaction / event loops / threading / etc is bring-your-own.
  • This logically includes markup parsing beyond the built-in attributes, eg. as described here: https://docs.yarnspinner.dev/getting-started/writing-in-yarn/markup
  • See the demo in demo.cpp for an example of how to extend the YarnRunnerBase class with a custom dialogue runner for your game
  • Built in operators and functions have been tested and implemented and should all work.
  • Custom functions and markup callbacks are implemented as std::function callbacks stored in a lookup table
  • The sample command binding functionality is supplied via VirtuosoConsole's QuakeStyleConsole.h https://github.com/VirtuosoChris/VirtuosoConsole which can take an arbitrary c++ function and do template magic to generate type correct iostream parsers without writing any code yourself. But you can do whatever you want with commands, like pass them to a scripting language or whatever else you want. see instructions or demo.cpp. This also uses std::function in the implementation.
  • The VM state is serializable, and uses nlohmann's c++ JSON library as a dependency to do this: https://github.com/nlohmann/json
  • the std::regex markup parsing should probably be replaced since std::regex is apparently not maintained and horribly slow. It's absolutely fine for now though.

yarnmachine's People

Contributors

virtuosochris avatar

Stargazers

MurilinhoPs avatar  avatar  avatar Edu Garcia avatar awardell avatar Ulysses avatar

Watchers

James Cloos avatar  avatar  avatar  avatar

yarnmachine's Issues

Missing step while building the project (minor thing)

Hello :-)

Not really an issue, but I had to do one tiny extra step and thought I'd write it here, in case anyone else runs into the problem in the future.

In order to successfully generate the project, I had to first manually fetch the protobuf submodule:

git submodule update --init --recursive

Otherwise the folder "./depends/protobuf" is empty and CMake fails with:

CMake Error at CMakeLists.txt:30 (add_subdirectory):
The source directory
....../YarnMachine/depends/protobuf
does not contain a CMakeLists.txt file.

Everything else generated and built just fine. ๐Ÿ‘

I would be happy if it was made into the instructions somehow, if this is something that happens for everyone.
This is a first time I'm making an issue, and maybe I could just make a pull request, tell me what you think.


PS: Just want to say I'm very glad this exists.
I'm making my own tiny game engine for learning purposes and really want to use YarnSpinner in it.
I understand YS was made for unity, but I definitely think it would benefit the community to have a engine agnostic version.
If nothing else your project showed me that it can be done.

Thank you!
Jan

By default, include ProtoBuf header/source with the rest of the source code.

Thanks for creating this. I just read over the YarnSpinner docs, and it seems like exactly the thing I was looking for, in order to create branching dialog for my toy game engine.

If I can make a suggestion: I think it would be a good idea to store the generated ProtoBuf files, along with the code. That way, people interested in this project can just download the code and build it, without having to download extra dependencies. Those ProtoBuf header/source files only need overwriting when the .proto file changes.

Error: Command <command name> unknown

Hello :-)

So far everything works just fine, but I ran into a single problem, which I'm not yet sure the cause of.
It's related to commands and manifests into the YarnTest (demo.cpp) console app like this:

[error]: Command fade_up unknown
[error]: Command 1.0 unknown

which happens when the script gets to this line

<<fade_up 1.0>>

As I'm just learning about YS it's by no means affecting me in any way, but I still thought I'd post it here.

How to reproduce

I compiled the test dialog at https://try.yarnspinner.dev/ using both the ysc.exe in this repo (version 2.3.0), as well as the compiler built from the source (version 2.4.0), but the problem persists.

I'm attaching the compiler output + the source script here YarnScript.zip

To reproduce the problem I copied the compiler output to the YarnTest.exe directory and ran it:

.....\build\Debug>YarnTest.exe
Type name of module to run :
YarnScript
Loading lines from : YarnScript-Lines.csv
Line database total lines : 37
Line database size (bytes) : 14104
Time spent in parsing (ms) : 2
Yarn Spinner is a language for writing conversations in games!
You can use it to write branching dialogue, and use that in a game engine!
For example, here's a choice between some options!
        1) Wow, some options!
        2) Can I put text inside options?
1
You got it, pal!
You can also write 'commands', which represent things that happen in the game!
In this editor, they'll appear as text:
> fade_up 1.0
[error]: Command fade_up unknown

[error]: Command 1.0 unknown

        1) Nice!
        2) But it didn't actually fade!

It's the only problem I ran into so far, and it doesn't affect me, but I will be poking around a bit more.

Thanks again,
Jan

yarnc / csv -> .json script

The additional large dependencies for protobuf and csv are kind of silly / offensive to me for simple dialogue trees - I'd like to have a script pipeline that compiles .yarn files, then opens them up and serializes the output into a json file using nlohmann, since my engine runtime already uses that.

I might put a bounty on this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.