Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

[Twine 2.1.1][SugarCube 2.14][JS] Reading external JSON files and file names

Hey there,

I'm researching some way of reading files in the folder where my twine story is. I'm interested in reading my /images and /templates folders. Since my project will probably grow pretty large in time and I have no idea how to organize all the data (images of potraits, landscapes, characters, maps, etc.) that I'll be using in my game inside the Twine editor, I want to be able to extend my game through external files.

I would want to put images of my items, portraits of characters and landscapes into my /images folder and then .json templates into my /templates folder. Both folders would have their own subfolders.

I would like to specify the data such as properties of my game objects inside the .json file and then assign random image paths from the corresponding images folder to the object, then use 'new' and prototype inheritance to generate game objects when my twine game starts.

Example:
/images/Enemies/Goblins/Portraits holds the following files: GoblinPortrait1.jpg, GoblinPortrait2.jpg, GoblinPortrait3.jpg
/images/Enemies/Goblins/Characters holds the following files: GoblinStand1.jpg, GoblinStand2.jpg
/templates/Enemies/Goblins holds Goblin.json, GoblinSoldier.json, GoblinMaster.json, GoblinWorker.json

I would build a list of GoblinPortraits and another list for the GoblinStand images and then randomly assign the paths to these images to my Goblin enemy game objects. The properties of the Goblin enemy game object would be read from the .json file and then when needed instantiated using the new operator.

I'm very new to web development, but I'm very eager to learn and experiment.
My question is, is this possible locally on the users computer or do I have to host these files somewhere on the web? What are the possible solutions to this problem considering that I use Twine 2, SugarCube 2.14 and JavaScript?

I read that, I'd need JQuery to deserialize the templates of my game data, but I find no clear solution to reading filenames of a folder using JavaScript. It seems I'd need to setup Node.js or something to be able to read the file names, but that would turn my game into a server client... eh, it seems too much compared to the benefits that I'd gain from going through setting it up let alone getting it to run.

Comments

  • edited April 2017
    Still no answer.. i guess the question is a really bad one.

    I looked more into this problem and now I can tell, that JS does not access the filesystem. You nees to setup a server of somekind to do that and there's no way of starting that server using JS and Twine. Hmmm, I might need to think about better organizing my objects and images and keep a list of my images inside the game project.
  • JavaScript instances running in browsers have extremely limited access to the local filesystem. You're not going to be able to read directories to get a list of their contents.

    I'm unsure why you want to attempt to initialize objects via deserialized JSON when you can simply use object literals and/or constructors. JSON is useful if you have data which can be represented as a simplified object graph—a property bag—which you need to send/receive or store/retrieve as plain text, elsewise not so much. I also feel the need to point out that JSON and object literals are not the same thing and should not be confused as such—I see that mistake a lot, though I'm unsure if you're making it here.

    As far as the image list(s). Since you're using Twine 2 as your compiler, your best bet would probably be to simply keep the list(s) of images within the project. You'd have more options using one of the command-line compilers, but there are trade-offs as well, so I'd only look into that if you find the Twine 2 IDE burdensome.
  • I'm unsure why you want to attempt to initialize objects via deserialized JSON when you can simply use object literals and/or constructors. JSON is useful if you have data which can be represented as a simplified object graph—a property bag—which you need to send/receive or store/retrieve as plain text, elsewise not so much. I also feel the need to point out that JSON and object literals are not the same thing and should not be confused as such—I see that mistake a lot, though I'm unsure if you're making it here.

    Hey there TME,

    The real reason why I want to use deserialized JSON, is because I want to make an editor for the items, characters, dialogs, missions, etc. So, this way I don't have to run the game in a test mode to verify if my object was created and designed in a valid way, that is compatible with my game. Other than that I'm not a huge fan of copy-pasting and editing, also I want to let other people add gameObjects to my game later in an easy way.

    About the instantiation using a constructor. Do you know any fast way of converting an Object into a constructor function? If not, do you just type in everything by hand?

    So far, I've been cloning my objects using Object.assign({}, gameObject) and accessing their prototype to add new variables and functions to them and also change previous ones when needed using 'this'.

    I originally wanted to just read my images and templates folder's files (JSON and picture files:gifs, pngs, jpegs) and subfolder names. Then deserialize the JSON, call the cloner function on them, assign a picture, gameObject ID, name, some difficulty based randomly generated values for some other parameters and add them to the list of useable objects in the game (shop inventories, enemy spawners, location constructors, dialog system, etc)

    Is there some way of at least partially achieving something like this using Twine 2?
Sign In or Register to comment.