Hey,
I'm using the latest SugarCube 2 and Twine 2 versions.
I ran into a read-only error when passing my object from SugarCube to JavaScript and try to write it's property.
Here's what I did:
Start Passage:
JavaScript:
So, what did I want to accomplish? I wanted to code a work around for my project to detect whether I am in development or published mode and specify where my GIF and JPG files are located accordingly, so Twine can find them when I'm using relative paths in HTML.
I've sort of accomplished this since I could get access the cwd and do my checks. Then I wondered if I could pass SugarCube2 objects per reference and whether I could store the gained information in a property (member).
Now, unfortunately this fails. It appears that SugarCube creates read-only properties for its objects.
Then I tried something like "mynumber" : 1 and increment it in JavaScript. That for some reason worked.
After that I tried adding writable : true to $mydir in SugarCube but the code still failed to write the property of the object.
I finally found a way to write the value in JavaScript by invoking a SugarCube macro:
I remember escaping a couple of quotations. But the code worked.
This begs the question:
What is going on? How, can I set my object property inside a macro without telling SugarCube to do it? Why does this work on numbers but fails on strings? Do I need to dig through SugarCube's macro implementation when problems like these occur?
Thank you for your explanation and help in advance
Br,
Disane
I'm using the latest SugarCube 2 and Twine 2 versions.
I ran into a read-only error when passing my object from SugarCube to JavaScript and try to write it's property.
Here's what I did:
Start Passage:
<<set $mydir = { "mydirectory" : "Get info from JavaScript and store current work directory here." }>>> <<findcwd $mydir>>
JavaScript:
macro.findcwd = { handler : function (place, macroName, params, parser) { var mydir = params[0]; // I snagged this from stackoverflow to get the cwd var loc = window.location.pathname; var dir = loc.substring(0, loc.lastIndexOf('/')); // Twine starts to complain here about // "mydirectory of "Get info from JavaScript and store current work directory here."" // being a read-only property mydir.mydirectory = dir; } }
So, what did I want to accomplish? I wanted to code a work around for my project to detect whether I am in development or published mode and specify where my GIF and JPG files are located accordingly, so Twine can find them when I'm using relative paths in HTML.
I've sort of accomplished this since I could get access the cwd and do my checks. Then I wondered if I could pass SugarCube2 objects per reference and whether I could store the gained information in a property (member).
Now, unfortunately this fails. It appears that SugarCube creates read-only properties for its objects.
Then I tried something like "mynumber" : 1 and increment it in JavaScript. That for some reason worked.
After that I tried adding writable : true to $mydir in SugarCube but the code still failed to write the property of the object.
I finally found a way to write the value in JavaScript by invoking a SugarCube macro:
new Wikifier(place, "CWD is indeed: <<set $mydir.mydirectory is \"" + dir + "\">>
I remember escaping a couple of quotations. But the code worked.
This begs the question:
What is going on? How, can I set my object property inside a macro without telling SugarCube to do it? Why does this work on numbers but fails on strings? Do I need to dig through SugarCube's macro implementation when problems like these occur?
Thank you for your explanation and help in advance
Br,
Disane