Howdy, Stranger!

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

[Harlowe] Storing variables between different stories

Hello! I've had some trouble with this. There are plenty of people who are doing similar things, but I guess I just have that weird combination of circumstances.

I need to store variables between different stories. I had originally planned on using cookies, which I do think is possible somehow, but there are two issues at the moment:
+ I don't know how to write with Javascript a string into a twine expression (it's reading correctly)
+ I don't know how to write a cookie only when a certain passage is loaded, rather than immediately at the start (it's definitely writing)

I would ordinarily compile the three stories into one twine file - they're 330MB, which I'm guessing is not that big - but each uses a lot of similar passage names, far more than would be feasible to change by hand.

I'm asking because I'm running out of time on when I can submit this for an assignment, but I'll be continuing to look into it. Any live help would be much appreciated.

Here's the code I'm using. Both are just in twine passages on their own:

---LOADING COOKIES INTO VARIABLES---
<div id="startCode"></div>

<script>
function get_cookie (cookie_name)
{
var cookie_string = document.cookie ;
if (cookie_string.length != 0) {
var cookie_value = cookie_string.match ( '[\s]*' + cookie_name + '=([^;]*)' );
return decodeURIComponent ( cookie_value[1] ) ;
}
return '' ;
}

var tendrilMushroomCompleted = get_cookie("tendrilMushroomCompleted");
var tendrilCultistsCompleted = get_cookie("tendrilCultistsCompleted");
var tendrilChamberUnlocked = get_cookie("tendrilChamberUnlocked");

var node1 = document.createTextNode("(set: $tendrilMushroomCompleted to " + tendrilMushroomCompleted.toString() + ")");
var node2 = document.createTextNode("(set: $tendrilCultistsCompleted to " + tendrilCultistsCompleted.toString() + ")");
var node3 = document.createTextNode("(set: $tendrilChamberUnlocked to " + tendrilChamberUnlocked.toString() + ")");

document.getElementById("startCode").appendChild(node1);
document.getElementById("startCode").appendChild(node2);
document.getElementById("startCode").appendChild(node3);
</script>

---WRITING COOKIES FROM VARIABLES---
<script>
function set_cookie (cookie_name, cookie_value, lifespan_in_days, valid_domain)
{
var domain_string = valid_domain ? ("; domain=" + valid_domain) : '' ;
document.cookie = cookie_name + "=" + encodeURIComponent( cookie_value ) +
"; max-age=" + 60 * 60 * 24 * lifespan_in_days +
"; path=/" + domain_string ;
};</script>

(if: $tendrilMushroomCompleted is false)[
<script>
set_cookie("tendrilMushroomCompleted", "false", 30, "dylanford.com");
</script>
](else:)[
<script>
set_cookie("tendrilMushroomCompleted", "true", 30, "dylanford.com");
</script>
]

(if: $tendrilCultistsCompleted is false)[
<script>
set_cookie("tendrilCultistsCompleted", "false", 30, "dylanford.com");
</script>
](else:)[
<script>
set_cookie("tendrilCultistsCompleted", "true", 30, "dylanford.com");
</script>
]

(if: $stoneAcquired is true)[
<script>
set_cookie("tendrilChamberUnlocked", "true", 30, "dylanford.com");
</script>
](else:)[
<script>
set_cookie("tendrilChamberUnlocked", "fakse", 30, "dylanford.com");
</script>
]

</script>
Sign In or Register to comment.