Random text in Squiffy

0 votes
57 views
asked Sep 11 in Authoring by anonymous

I can't seem to get my random text generator to work. I am trying to randomly create an attribute in Squiffy using

var randomnumber = function rng(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}

set ("mynumber", randomnumber(1,6));

but it doesn't seem to work as a variable. All I get is null. Help!

1 Answer

0 votes
answered Sep 15 by Alex (486 points)

Where are you defining your randomnumber function? If it's not in the same place as your set script then it won't be defined. You should be able to see the error in your browser console.

If you want to include additional global JavaScript functions, then you can put them in a separate .js file and include it using @import - see http://docs.textadventures.co.uk/squiffy/import.html.

Alternatively you could add them to JavaScript's global window object in a master section. Add it using this:

window.randomnumber = ...

instead of

var randomnumber = ...

Adding it to a master section ensures that the function will always be defined even when the game is reloaded. (If you only added it to the first section of a game, if the player reloads the browser tab, the initial section's JavaScript won't be re-run so you won't have the function defined globally any more).

This site is now closed.
As of 1st November 2015, this site is a read-only archive. For more information see the intfiction forum post

Welcome to IF Answers, a site for questions and answers about Interactive Fiction.

Technical questions about interactive fiction development tools such as Inform, Twine, Quest, QuestKit, Squiffy, Adrift, TADS etc. are all on-topic here.

Non-technical questions about interactive fiction are also on-topic. These questions could be about general IF design, specific games, entering the IF Comp etc.
...