Twine: eliminate the back button functionality in Sugarcane

Update: this is now built into Twine 1.4, so this script is no longer necessary. You can enable it in Twine 1.4 by writing "Undo: off" in StorySettings.

If you want to remove the browser's Back button functionality in Sugarcane, to prevent the player from rewinding or undoing moves, here is some script code that can do that.

Obsolete script removed: use Twine 1.4

Version history:

  1. 9/3/13 - Fixed several bugs. The Start passage no longer appears in browser history, Back and Return now behave differently regarding variables, and the Rewind menu items no longer appear in browser history.
  2. 26/2/13 - Initial.

Feel free to report any bugs to @webbedspace.

Comments

snapman's picture

useful!

I had been looking for a way to do this, but my attempts to comment-out everything modifying the url hash were pretty disastrous.

avery's picture

Ahaaaa! Thanks muchly for

Ahaaaa!

Thanks muchly for this, i've been wanting to find out! (btw you should just collect all of these posts into a big pdf or something)

Hey I am having problems

Hey I am having problems getting this to work, in fact I can't get any functions that you have posted to work properly which makes me think I am missing some sort of vital information on how to properly set them up despite looking at several tutorials.

Here is what I have in my start panel;

<<silently>>

<<set $CaveState = 0>>

(function(){
var bs = String.fromCharCode(92);
Wikifier.formatters.unshift({
    name: "continuedLine",
    match: bs+bs+"s",
    handler: function(a) {
        a.nextMatch = a.matchStart+3;
    }
});
}());

History.prototype.display=function(d,b,a){var c=tale.get(d);this.history.unshift({passage:c,variables:clone(this.history[0].variables)});
this.history[0].hash=this.save();var e=c.render();if(a!="offscreen"){removeChildren(document.getElementById("passages"));
document.getElementById("passages").appendChild(e);if(a!="quietly"){fade(e,{fade:"in"});}}if((a=="quietly")||(a=="offscreen")){e.style.visibility="visible";
}if(a!="offscreen"){document.title=tale.title;document.title+=": "+c.title;window.scroll(0,0);
}return e;};

History.prototype.restart=function(){window.location.reload();};macros["return"]=macros.back={handler:function(a,b,e){
var el,d="";var steps=1;if(e[0]){if(e[1]=="steps"){if(isNaN(e[0])){throwError(a,"parameter before 'steps' must be a number.");
return;}else{if(e[0]<state.history.length){d=state.history[e[0]].passage.title;steps=e[0];
}}}else{if(tale.get(e[0]).id==undefined){throwError(a,"The "+e[0]+" passage does not exist");
return;}for(var c=0;c<state.history.length;c++){if(state.history[c].passage.title==e[0]){d=e[0];
steps=c;break;}}}}else{d=state.history[1].passage.title;}if(!d){return;
}else{el=document.createElement("a");el.className="return";el.onclick=function(){if(b=="back"){while(steps>=0){if(state.history.length>1){state.history.shift();
}steps--;}}state.display(d);};el.href="javascript:void(0)";el.innerHTML="<b>«</b> "+b[0].toUpperCase()+b.slice(1);
a.appendChild(el);}}};Interface.buildSnapback=function(){var c=false;removeChildren(document.getElementById("snapbackMenu"));
for(var a=state.history.length-1;a>=0;a--){if(state.history[a].passage&&state.history[a].passage.tags.indexOf("bookmark")!=-1){var b=document.createElement("div");
b.pos=a;b.onclick=function(){var p=this.pos;var n=state.history[p].passage.title;
while(p>=0){if(state.history.length>1){state.history.shift();}p--;}state.display(n);
};b.innerHTML=state.history[a].passage.excerpt();document.getElementById("snapbackMenu").appendChild(b);
c=true;}}if(!c){var b=document.createElement("div");b.innerHTML="<i>No passages available</i>";
document.getElementById("snapbackMenu").appendChild(b);}};

<<endsilently>>

[[Begin|Born]]

[[Skip Ahead|SearchLeft]]

You can see I am using both your 'eliminate back button' code and the one that allows the '\' key to eliminate line breaks, but neither work in my story. (The '\' key actually results in '\s')

Any direction would be greatly appreciated for a brand new Twiner!

SpindleyQ's picture

Hi! Javascript code doesn't

Hi!
Javascript code doesn't generally go in the "Start" panel, it goes into a special passage with "script" in its tags. So if you create a new passage, put "script" for its tags field, and paste everything between "set $cavestate = 0" and "endsilently" into that, you should be set.

(Basing this answer off of the Twine FAQ; I technically haven't actually tried to do any JS in Twine yet.)

Thanks so much, champion!

Thanks so much, champion!