0 votes
by (140 points)
I am building a sports simulator. I want to add team stats, so when a variable is declared with the team name, it can be used with the .csv to fetch values such as offense, defense, etc.

I am using Harlowe 2.0.1 and am planning to run the HTML file locally. I would prefer to retain the csv format.

How can I go about this?

1 Answer

+1 vote
by (159k points)

A potential solution to your request would need to solve three main issues:

1. Accessing an external CSV file.
2. Parsing the contents of the file.
3. Updating a Harlowe story variable via Javascript. (i'm assuming you don't just want to display the content.)

I will comment on each of the above in the reverse order.

3. The Harlowe engine and Javascript.

Harlowe has been deliberately designed by its developer to restrict access to the internals of its Javascript engine and the story format has no documented Javascript API, this means there is no official method for an Author to use Javascript to access the story's variables.

Some author's (like furkleindustries) have discovered it is possible to use a Scope Escalation Hack to gain access to some of the restricted sections of the engine, and one of these sections is the story variables. This comment on the How can I get live data from JavaScript into Twine/Harlowe variables? question contains a warning about the possible issues associated with relying on such a hack.

2. Parsing CSV data using Javascript.

You can use a Javascript library like jquery-csv (I haven't used this library before) to parse the CSV content once you have retrieved it from the external file(s).
The simplest method to add a Javascript library like that one to your story is to open its main JS file(s) (jquery.csv.min.js in the case of that particular library) in a text editor and then cut-n-paste the file's contents into your story's Story Javascript area.

1. Opening an external CSV file from a HTML page being viewed in a web-browser.

There is a new(ish) Web/Javascript specification known as the File API which is designed to be used to open an external file from a web-browser, unfortunately that specification is still a Working Draft and is not fully supported by all brands (nor versions) of common web-browsers (caniuse).

notes on point 1:
a. This is the main reason why I asked (in your previous question about this subject) if you were planning on hosting the released version of your story HTML file on a web-server, because it's easier to retrieve CSV data from a web-server that it is from a local file.

b. The Client-Side File Handling example found on the jquery-csv library's website contains Javascript code showing how to test if the File API is supported by the current Reader's web-browser, and how to use it and that library to read/parse CSV data.

by (6.2k points)
what is csv?
by (63.1k points)

what is csv?

Comma-separated values, which is exactly what it sounds like. It's sort of like a less standardized and less fine-tuned JSON, from my understanding. I imagine you're better off using JSON in most cases, if you can. 

...