Howdy, Stranger!

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

Setting Twee Variables from Javascript (Solved)

edited February 2014 in Help! with 1.x
I'm having some issues getting the value of a Twee variable to show up after setting it from Javascript. I have this:
state.history[0].variables[c[0]] = c[random].split(',,')[1];
c[0] and c[random].foo are set from elsewhere in the script. You can see the whole thing under below if it is helpful. I am also using SugarCube, in case that matters. If I use Javascript again to access this variable, everything is fine, but if I try to use <<print $c[0]>> it comes back empty (not actually "c[0]" but what that resolves to like <<print $whatever>> if c[0] = whatever).

-------------------------
Full code:
Passage:

<<nobr>>
<<test "whatever" "<span style='color:#ff0000'>test</span>,,1" "<span style='color:green'>test2</span>,,2" "<span style='color:yellow'>test</span>,,3" "random">>
<</nobr>>

<<click "test">><<replace "#testthis">><<print $whatever>><</replace>><</click>>

<<id "testthis">>test text<</id>>
Macro:

version.extensions.testMacro = {
major: 1,
minor: 0,
revision: 0
};
macros["test"] = {
handler: function (a,b,c) {
var l = document.createElement("a");
a.appendChild(l);
l.className = "testClass";
l.id = c[0];
l.innerHTML = c[1].split(',,')[0];
l.onclick = function () {
for(var i=1;i<c.length;i++) {
if(c[c.length-1] == "random") {
var randomLength = c.length-2;
var random = 0;
for(var j=0;j>-1;j++) {
random = Math.floor(Math.random() * randomLength+1);
if(l.innerHTML != c[random].split(',,')[0].replace(/'/g, '"')) {
break;
}
}
l.innerHTML = l.innerHTML.replace(l.innerHTML,c[random].split(',,')[0]);
state.history[0].variables[c[0]] = c[random].split(',,')[1];
break;
}
if(c[i+2] == "end") {
var thisl = document.getElementById(c[0]);
var end = document.createElement("span");
end.innerHTML = c[i+1].split(',,')[0];
thisl.parentNode.replaceChild(end, thisl);
break;
}
if(c[i+1] == "out") {
var thisl = document.getElementById(c[0]);
thisl.parentNode.removeChild(thisl);
break;
}
if(l.innerHTML == c[i].split(',,')[0].replace(/'/g, '"') &amp;&amp; c[i+1] != undefined &amp;&amp; c[i+1] != "end" &amp;&amp; c[i+1] != "out") {
l.innerHTML = l.innerHTML.replace(l.innerHTML,c[i+1].split(',,')[0]);
break;
}
if(c[i+1] == undefined &amp;&amp; c[i+1] != "end" &amp;&amp; c[i+1] != "out") {
l.innerHTML = l.innerHTML.replace(l.innerHTML,c[1].split(',,')[0]);
break;
}

}
}
}
}

Comments

  • The SugarCube docs say you should use state.active instead of state.history[0]. I don't know if that could cause the issue you're seeing though.
  • mth wrote:

    The SugarCube docs say you should use state.active instead of state.history[0]. I don't know if that could cause the issue you're seeing though.


    Aha! Argh! I missed that (very new to Twine and SugarCube both)! That was the answer! Thanks!
Sign In or Register to comment.