Howdy, Stranger!

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

[SugarCube] Problem with programmatically changing the start page

For my project, I need to be able to direct the reader to any passage from the initial URL, so I have implemented a URL query that sets the startPassage config setting, redirecting to the chosen passage. E.g., "index.html?nav=My%20Passage" would trigger a change in the

This seems to work fine, until you also disable history controls so that the reader cannot use the browser's back/forward buttons to navigate through the story history. At that point, if you open a second instance of the story in the same window using a different startpoint, then press the browser button to go back to the first instance, SugarCube/Twine can't cope, triggering an error an error on Object.History, e.g.:

[quote]Error: Uncaught TypeError: Cannot read property 'title' of undefined.

Stack Trace:
TypeError: Cannot read property 'title' of undefined
    at Object.History.restore (file://localhost/Twine/Tests/Test.html:82:7806)
    at Object.History.init (file://localhost/Twine/Tests/Test.html:82:2963)
    at HTMLDocument.<anonymous> (file://localhost/Twine/Tests/Test.html:81:17642)
    at j (file://localhost/Twine/Tests/Test.html:26:26676)
    at Object.k.fireWith [as resolveWith] (file://localhost/Twine/Tests/Test.html:26:27489)
    at Function.o.extend.ready (file://localhost/Twine/Tests/Test.html:26:29283)
    at HTMLDocument.I (file://localhost/Twine/Tests/Test.html:26:29453)

(For example, you might first navigate to Test.html, then add a new location to the URL, e.g. Test.html?nav=Later. If you then hit the browser back button, you will have confounded the application's expectations, generating the error/stacktrace shown above.)

The relevant code:
:: Configuration [script]
config.disableHistoryControls = true;
config.startPassage = "Start";

:: StoryInit
<<script>>
... code to read URL queries into the opts object...

if (opts.hasOwnProperty("nav")) {
SaveSystem.deleteAuto();
config.startPassage = opts["nav"];
}
<</script>>
I'm not sure whether what I'm encountering is a bug in SugarCube--I'm not sure that programmatically changing the config option is really supported. Any ideas on what I can do here? Could I search through the browser window's history and eliminate any reference to my Twine's base URL before the currently running instance--if so, is there a safe way to do that?

Thanks for any help/advice!

EDIT: I have a minimal example and can supply if needed.

Comments

  • 1. Test cases are always welcome.

    2. I think I know what's probably going on.  I'll take a look at it.
  • Thanks! HTML for test case is attached, Twee source (exported from Twine 1.4.2) below.
    :: Start
    This is a test.

    [[Next]]


    :: Next
    Continuing on...

    [[Onward!]]


    :: Onward!
    End of test.


    :: StoryInit
    <<set $urlopts to {}>>
    <<script>>
    state.active.variables.urlopts = parseQueryString( window.location.search.substring(1) );
    var opts = state.active.variables.urlopts;
    for (var key in opts) {
    if ( opts.hasOwnProperty(key) ) {
    state.active.variables.urlopts[key] = decodeURIComponent(opts[key]);
    if (state.active.variables.hasOwnProperty(key)) {
    state.active.variables[key] = state.active.variables.urlopts[key];
    console.log("Setting variable $"+ key + " to " + state.active.variables.urlopts[key]);
    }
    }
    }
    if (opts.hasOwnProperty("nav")) {
    SaveSystem.deleteAuto();
    config.startPassage = opts["nav"];
    }
    <</script>>


    :: Configuration [script]
    config.disableHistoryControls = true;
    config.startPassage = "Start";

    window.parseQueryString = function( queryString ) {
    var params = {}, queries, temp, i, l;

    // Split into key/value pairs
    queries = queryString.split("&amp;");

    // Convert the array of strings into an object
    for ( i = 0, l = queries.length; i < l; i++ ) {
    temp = queries[i].split('=');
    params[temp[0]] = temp[1];
    }

    return params;
    };
  • A PM you have.  Read it you should!
  • Read it I have. Thanks!
Sign In or Register to comment.