Howdy, Stranger!

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

Harlowe: Variable Access to Data Structures

Hello Twine forums!

I'm currently trying to include in a story a passage of text that gets progressively and randomly more scrambled as time goes on. Effectively, I'm trying to randomly swap letters with each iteration. This sort of string manipulation would be trivial using Javascript (or whatever), and feels like it should be relatively easy to implement in Twine. Ordinarily, I'd just resort to Javascript (or whatever), but I'm a university professor teaching English majors how to use Twine for their final projects, and for purposes of pedagogy, I'm trying to figure out how I can implement it in Harlowe/TwineScript.

Enough preamble. Here's my dilemma: for the life of me, I can't figure out how to get reliable variable access to elements either in an array or a datamap.

Array-wise, I can pull two random characters from the string, by using calls of the sort (set: $char to $string[$index]) [although this only sometimes works and I haven't yet been able to figure out what does and does not break it. That's not my question here.] I cannot, however, then change the array in the reverse manner: (set: $string[$index] to $char) breaks, throwing "I can't give undefined a new value." I understand that arrays in Harlowe are explicitly not indexed in this manner (0-indexed bracket notation), but instead use the possessive form: (print: $array's 4th). I haven't been able to figure out how to hack a way to use the possessive syntax with a variable: e.g. (print: $array's $var) does not work. If you have suggestions to this end, I'd love to hear them, although what I've been able to Google, it sounds like this particular avenue is probably a dead end.

I thought perhaps I might be able to hack a datamap for these purposes, but have discovered that datamaps' indices must be hard-coded in much the same way as arrays'. Take the following example:

[1] (set: $index to 1)
[2] (set: $stringindex to (text: $index))
[3] (set: $data to (datamap: $index, "one"))
[4] (print: $data's 1)
[5] (print: $data's $index)
[6] (print: $data's $stringindex)

The first four lines of code work perfectly, while the last two lines throw errors about unexpected string literals ("Unexpected string literal 's $index"). Which is to say, I can put data *into* a datamap variably, but can't get it out variably, which is the exact opposite problem I'm having with the array.

I suppose this makes some kind of sense? Since you put data into a datamap with (set: $data to (datamap: "index", "value")), but get it out with (set: $var to #$data's index). The latter isn't actually a string.

If any of you Twine folks have figured out how to implement something like this in Harlowe/TwineScript without having to resort to kludges like hacking Javascript calls into Twine passages or other tomfoolery, or for that matter, moving to other story formats, I'd appreciate it greatly if you might be able to describe how.

Thanks so much!

Scott

Comments

  • The Harlowe project overview lists a number of potential changes in the upcoming Harlowe 2 that are related to accessing array elements and using variable based indices.

    You can download and test a beta version of Harlowe here, though I am not sure which version of the Twine 2 application is in that download.
Sign In or Register to comment.