Twine Version: Harlowe
Hi there,
I am passing variables between different Twine files via my website with the Javascript speaking to the Harlowe code and this is working successfully on the opening page of the Twine file. For example, the states of adrenaline, dopamine etc, code for that below:
(set: $adrenaline to 0)\
\
(set: $dopamine to 0)\
\
(set: $seratonin to 0)\
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const c = urlParams.get('c');
So the URL includes the letters with these states added on, and this changes depending on thee states from other websites brought through. But for a neutral opening, it would have the add on ?a=0&b=0&c=0 at the end.
However, when I try to add values onto say $adreanline within the Twine story, from the second passage, it works when play testing via Twine but it seems to be having issues adding the number on in an online state with the add on to the URL noted above.
I’m writing it as follows:
(set: $adrenaline to $adrenaline -1)
. I’m unsure whether it’s an issue with the Harlowe code or speaking to the Javascript. In the online version when a=0 in the URL, it currently states:
“The string “0” isn’t the same type of data as the number 2
You might want to convert one side to a number using (num:), or to a string using (str:)”
Any advice would be greatly appreciated! Thank you so much in advance!
It sounds like the data types are mismatched. When pulling a variable from the URL, they are typically understood as strings, but any math requires numbers so you can use the (num:)
macro on the variables pulled from the URL. A string value of “0”, might as well be “Soup Can” because the data types are the same.
→ https://twine2.neocities.org/#macro_num
JavaScript is more forgiving with data types and sometimes automatically converts things behind the scenes, but I don’t believe Harlowe’s API does that, thus it suggesting you use the (num:)
macro.
Hope I didn’t misunderstand something. Let us know if that helps.
Hi there, thanks so much for this advice.
I think you are probably right but am unsure about how to implement this after several attempts.
Here is the code I am using to get the value from the URL:
(set: $adreanaline to 0)\
<script>const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const a = urlParams.get('a');
$adrenaline = a;
</script>\
How might I convert this to numbers using the (num:)
macro?
Right so you can only use macros outside of <script>
tags.
After your code, you should be able to use:
(num: $adrenaline)
If that only outputs a valid number (and doesn’t change the variable) then set $adrenaline
to a number version of itself:
(set: $adrenaline to (num: $adrenaline))
However, Harlowe might understand JavaScript data types so you might try altering the last line of your <script>
code to look like…
$adrenaline = parseInt(a);
…instead of using the (num:)
macro.
Not to worry, what you are asking for can be done. I just don’t have time to test all the scenarios, but I can throw them at you quickly.
Let us know is any of those tricks work for you.
1 Like
The first option worked beautifully, thanks so much!
1 Like