Howdy, Stranger!

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

Is there a way to take a value from a specific datamap in a series based upon another variable?

edited March 2016 in Help! with 2.0
Like, this:

(set: $food1 to (datamap: "name", "beef", "calories", "300"))
(set: $food2 to (datamap: "name", "sausage", "calories", "400"))

(set: $foodid to (either: 1, 2))

(print: ("$food"+$foodid')s name)

Which would result in a 50% chance to show beef and a 50% chance for sausage.
My question lies in the concatenation of the 'string' $food with the actual value of the variable $foodid, turning the result into the correct variable $food1 or $food2.

If it's not possible, a workaround would be nice.

The only alternative I see right now is working with huge lists of "if" statements ... not pretty!

Thank you!!

Comments

  • You can concatenate a string with a number if you use the (text:) macro to first convert the number to a story like so:
    (set: $varS to "string")
    (set: $varN to 10)
    (print: $varS + (text: $varN))
    
    ... but this wont allow you to convert the resulting string into a $variable reference.

    You could use the (either:) macro to select a datamap instead of a number like so:
    (set: $item to (either: $food1, $food2))
    
    (print: $item's name)
    
  • Hi there greyelf.

    Thank you so much for your input; the idea of using an aggregate variable (like $currentItem) was absolutely mindblowing!

    If you don't mind a follow up question; any idea how to go back from the aggregate to the original variable?

    To give a little more context; I'm working on a screen with a table with a list of items which are all datamaps, like above. Clicking on one item would go to a detail screen where you can change the item's properties.

    I could make a specific passage per item, but that would quickly get unmanageable.
    So basically, with the click: command, I can go to a single screen and show the relevant properties of the chosen item.

    But if I change some properties in this new screen; how do I get back to the original screen?

    So, how do I do the following:

    (set: $food1 to $currentItem)

    ... considering that this new screen doesn't really know which original item was placed into the currentItem variable. (so after leaving the screen, how does the code know that it has to update the changes made into $currentItem, into $food1 or $food2 or $foodx)

    Thank you again!
  • You could use a variable to store the name of the variable original variable (without the $ sign) in the table screen, and then use a (print:) macro in the detail screen to dynamically create a (set:) macro to modify the original variable's value:

    The following example consists of three passages (startup tagged, Table, Detail)

    a. Assign a value to $original, this needs to be done either before the Table passage or in such a way that viewing the Table passage a second time does not reset the $original variable. I am going to use a startup tagged passage to do this.
    (set: $original to "the original value")
    
    b. Table passage, which in this example is the story's first passage.
    The (print: (passage:)'s name) screen
    
    original: $original
    
    (link: "Detail Screen")[
    	(set: $varName to "original")
    	(goto: "Detail")
    ]
    
    c. Detail passage:
    The (print: (passage:)'s name) screen
    
    (set: $aggregate to "the new value")
    
    old original: $original
    
    (print: "(set: $" + $varName + " to $aggregate)")
    
    new original: $original
    
    (set: $last to (history:)'s last)
    (link-goto: "Return to $last screen", $last)
    
Sign In or Register to comment.