Login
Remember
IF Answers
Ask a Question

# 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!

- squiffy

## 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).

## Related questions

- How to add a random number generator in Squiffy?
- Why does this Javascript not work in squiffy but will in a js editor
- How to compare two variables in Squiffy?
- How to deal with large numbers in Squiffy?
- Can I add dud links and tooltips in Squiffy with Javascript?

...