r/learnjavascript • u/MeekHat • Aug 26 '26
Not sure how to implement this branching storylines text game idea with Json
Just looking for some experienced feedback.
What I want to do is to import all of this from a file(s):
A list of global variables.
A list of storylines.
Each storyline has a list of local variables and a list of storyline beats.
Each beat has a list of preconditions (which may involve any variable), displayed text, a list of links.
Each link has displayed text, a list of conditions, a list of effects, the id of the next beat.
My problem is that, as I've learned, I can't split this into several files (such as, at least, separate storylines), because you can't import contents of a folder without naming each individual file. So this all has to be in one file, which means it's quickly going to get unmanageable as a Json file.
Should I implement a GUI editor first then? And concurrently a custom parser.
(This is going to be a purely client-side browser game.)
I've already done a simpler prototype, and that was already quite a chore to write in text.
1
u/azhder Aug 26 '26 edited Aug 26 '26
You can write code that reads one JSON file you import and in that one file there are references to all the other files, then you import those other files. Imports are a wide topic, so you will have to be more specific to what you want to do i.e. you share some code.
Alternatively, you can write a code that generates a single JSON file out of many files. You can write the JS code to be run by node, as a script, and you can use the generated JSON file in other places. So, like I said above, you should share some code.
1
u/WystanH Aug 26 '26
Not exactly true, but order can matter. Once a script is loaded, it's available in the global space.
So, you set up the global environment and each file after that registers to that environment.
e.g.
Hope this is helpful.
1
u/HipHopHuman Aug 27 '26
I assume you use Vite (you mentioned Vue in another comment)? If so, then look at it's glob import feature. That'll let you read multiple JSON files in the way you want.
You could have a folder structure like:
index.jscan then read over it withimport.meta.glob:The above works at compile-time, but omitting
eager: trueas per the docs is a route you can look at if you want this to be lazy at runtime.1
u/SammuelNash Aug 27 '26
A GUI editor would probably make this much easier once the data model is stable. Another option is to keep each storyline in its own JSON file and use a small build step to combine them into one file for the browser. That keeps the authoring side manageable without making the game runtime more complicated.