0 votes
by (220 points)
I'm looking for a bit of best practice advice on how to choose a random event from a filtered list.

Basically, in a passage, I have a long list of random events that I'd like the game to choose one from and display on the page. But I'd to restrict the choice or display to only options for which the player qualifies.

For example, if the player is level 4, the game should only choose from events listed between levels 1 and 4, any events listed as level 5 or higher should not show up on the page. If possible I'd like to filter against several variables, rather than just one.

Should I run through the events, then store qualified ones in an array, and choose from that array? Or should I do a loop, choose, then test level - if yes display, if no re-run loop. Neither of these seem very quick or clean though. Does anyone have any advice on a better way to handle this? Specifically using Twine2 and Sugarcube if it helps.

Thank you!

1 Answer

+2 votes
by (63.1k points)
selected by
Best answer

I would first create a data structure with the possible events:  

<<set setup.events to {
    generic : [
        "always available events", 
        "help an old lady",
        "found some junk"
    level : {
       oneToFour : [
            "attacked by a bear", 
            "found some gold"
        overFour : [
    other : {

After that, I'd then test for events in PassageReady (or similar), so that the list of potential events is always available. 

<<set _events to clone(setup.events.generic)>>
<<if $level lte 4>>
    <<set _events to _events.concat(setup.events.level.oneToFour)>>
     <<set _events to _events.concat(setup.events.level.overFour)>>
<<if $someOtherCondition>>
     <<set _events to _events.concat(setup.events.other.whatever)>>

This way, you can write up all the event control code one time, and adding new events later is as simple as updating the setup.events object. 

Since the events are rolled every passage thanks to PassageReady, all you need to do to select one is fetch it from the array: 

<<set _randomEvent to _events.pluck()>>

You don't have to use something exactly like this, but think about the problem this way: instead of building a system that relies on you keeping track of everything, build infrastructure you can use to build the thing you want. 

by (220 points)
This is exactly the help I needed, thank you very much!