Howdy, Stranger!

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

[Harlowe 1.1.1][Twine 2.0.8] Best method for manipulating multiple strings, Dataset or Array?

Hi I'm a twine newby,

My question: What's the best method to accomplish this a list, an array or dataset?

I'm not married to Harlowe, if this is better implemented in a different story format please suggest one so that I do not overload a function.

Scenario: I want to generate a choice from a list of strings, (*note-I've only included 3 for the example but I have over 50 different strings.)

After the user makes their choice, I want to keep track of that choice so that at the end of the game I can display a list of all the choices they've made as well as remove that choice for future passages so you don't get the same twice.

I know that before I get to the checking and removing part of the code I need to choose the correct data function first and then proceed from there.

I've looked at:
http://www.glorioustrainwrecks.com/node/5034
http://twinery.org/forum/discussion/2146/how-to-add-an-item-to-a-dataset-or-array
http://twinery.org/forum/discussion/comment/11292/#Comment_11292
http://twinery.org/forum/discussion/comment/9504/#Comment_9504

Here's my first stab: Part A:

The first gives the user 3 choices. 1) A random fight choice 2) A random fear choice 3) A random choice of between the previous two.

{
(set: $fightchoices to (either:"Run towards the door","Fight the monster","Use sword and jab"))

1.Will you fight? [$fightchoices]<shout|
(click: ?shout)[ (replace: ?shout)[**Excellent Choice, you bought yourself some extra time**]]
}

{
(set: $fearchoices to (either:"Stand there and do nothing","Scream for help","Beg for mercy","Hide in the corner"))

2.Will your fear protect you?
[$fearchoices]<shout1|
(click: ?shout1)[ (replace: ?shout1)[**Sometimes offense is the best defense**]]
}

{
(set: $choices3 to (either:$fightchoices,$fearchoices))
3. Will you let fate decide?
[$choices3]<shout2|
(click: ?shout2)[ (replace: ?shout2)[**Fate has decided you will live**]]
}

Looking at trying one of these options to do the checking and removing: Part B:

1.array
(set: $fight to (array: "Run towards the door","Fight the monster","Use sword and jab"))
(set: $fear to (array: "Stand there and do nothing","Scream for help","Beg for mercy","Hide in the corner"))

2.dataset
(set: $fight to (dataset: "Run towards the door","Fight the monster","Use sword and jab"))
(set: $fear to (dataset: "Stand there and do nothing","Scream for help","Beg for mercy","Hide in the corner"))

Comments

  • Generally the choice of which collection data type to use is based on what you want to do with the data contained within the collection.

    You generally use an Array if either the order of the elements within the collection is important or if you need to loop through the elements.
    You generally use a Set if all you need to know is if an element exists in the collection or not.
    If you want to access the elements within a collection using an identifier, you can either use an Array if the identifiers are numerical positional indexes, or a Map (Associate Array, Hash Table) if the identifiers are key-words.
  • Thank you! I thought I would use a (Set $var to (array: "thing") type of syntax so that I could make sure that the string wouldn't repeat in a choice. Should I assign the string a number using the datamap function so that later I can keep track of the choices a user has made and display them at the end of the game.
  • Arrays and Maps don't care if the data stored within their elements are unique or not, only the identifiers have to be unique, so both of the following are valid:
    (set: $array to (array: "cat", "cat", "cat", "dog"))
    
    (set: $map to (datamap: "pet1", "cat", "pet2", "cat", "pet3", "cat", "pet4", "dog"))
    
    ... on the other hand the elements stored within a Set must be unique.

    So if you care about the order the user made their choices then you would use an Array to store the list of choices made:
    (set: $chosen to (array:))
    
    /% In a different passage... %/ 
    (set: $chosen to it + (array: "User's First Choice"))
    
    /% In another different passage... %/ 
    (set: $chosen to it + (array: "User's Second Choice"))
    ... etc
    
    .. if on the other hand you only want to track if a user selected a particular choice or not, then you could use a Set (or an Array):
    (set: $chosen to (dataset:))
    
    /% In a different passage... %/ 
    (set: $chosen to it + (dataset: "User's First Choice"))
    
    /% In another different passage... %/ 
    (set: $chosen to it + (dataset: "User's Second Choice"))
    ... etc
    
Sign In or Register to comment.