When a random option is not avalable, pick another

Hey guys, I have been trying to get inform 7 to run a series of random scripts where none of the options are originally available, but are added later on as dictated by my story, with sometimes one or two options being chosen. The snag is I can’t work out how to do this. I thought about adding them to a table, but I can’t do this as they all involve a Say command and a short script. Also, I’m not sure how to pick random table options. Has anyone any suggestions on how to achieve this? At the moment I’m doing some of it using nested Say commands, but this is very jerky and not a very good option for most of what I want to do.

I’m not quite sure this is what you’re looking for; it’d a bit easier to visualize what you want without some code or pseudocode, but you’re more likely than you think to be able to do with a table, though. You could fill the table with texts that have substitutions that run code, like this:

[code][in the table]
“[ring the bell]The bell rings.”

[elsewhere]
To ring the bell: now every guard is alert.[/code]

Or you could fill a table with rules that get triggered when the row is selected.

To pick a table option complete at random you could say “Choose a random row in the Table of Foo.”

I did something similar for an AI system (which has a collection of scripts, selected from lists of available scripts by various criteria). I used object rulebooks, something like this:[code]A script is a kind of thing.

Execute is an object based rulebook.

Available Scripts is a list of objects that varies.[/code]Now we can define scripts as objects with “execute” rules, like this:Foobar is a script. Execute foobar: say "Foobar!"; move the player to the Hall of Mirrors.…and add or remove them from the Available Scripts list at will. To select a random script from the list, run it, and remove it from the list, we might do:let active script be entry (a random number from 1 to the number of entries in Available Scripts) in Available Scripts; follow the execute rules for the active script; remove the active script from Available Scripts.Rulebooks are magic. :slight_smile:

That’s a really cool idea prevtenet, but I pasted it in as-is to test it, but when it ran I got this error:

I don’t think the script is setting Foobar as ‘active’, which makes me think it would probably break if it ran out of options as well. I think I need to have a blank script that only executes when none of the others do, just to be sure, but I have no Idea how to do that.

Well, before you call the “let active script be” rule you could check whether Available Scripts is empty, and if it isn’t then you could run the blank script instead. (It’s often a good idea to include a sanity check like this when you’re trying to choose randomly from a list that might be empty!)

Yeah, the code above is incomplete; you also need logic to manage what’s on the Available Scripts list, and logic to deal with the list being empty, both of which depend on your specific application. Something like: After taking the lantern: add foobar to Available Scripts. and:Every turn: if the number of entries in Available Scripts is not 0: let active script be entry (a random number from 1 to the number of entries in Available Scripts) in Available Scripts; follow the execute rules for the active script; remove the active script from Available Scripts; otherwise: say "List is empty!". [or whatever]

Wow, thanks that really works! You’ve taught me quite a bit about Inform. Maybe you should turn that into an extension?

By the way, is there any way to do this at all:

To ResetScripts: add every script to Available Scripts;