Howdy, Stranger!

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

New to Twine - Making a click for each array element

edited August 2016 in Help! with 2.0
Greetings,

I've recently started using Twine 2 Harlowe and I'm having a blast! Such a great engine! But I cannot understand how to make the final feature to my first project.

The thing I'm working on is a deck manipulating assistant. I have several decks of cards, described as arrays with text elements:
(set: $deck1 to (a: "cardtext1","cardtext2"...)
(set: $deck2 to (a: "cardtext1","cardtext2"...)
(set: $deck3 to (a: "cardtext1","cardtext2"...)

These are put into an empty passage and loaded on the first passage with the help of
(display: "passagewitharrays")

The cards are usually manipulated this way:
(set: $t to (either:...$deck1))
(set: $deck1 to $deck1 - (a: $t))
(print: $t)

And I move this card to discard pile on click
(set: $discardofdeck1 to  $discardofdeck1 +  (a: $t))

But sometimes I need to pin the card, so I move it to $pinned array.

So, the question is:

How can I later move any of the elements from $pinned to any discard array?

I can show all the elements of $pinned, but how can I make a click button for each of these elements, so that I can move one of them to another array? I was thinking of trying to make some sort of a loop for $pinned's length, but not sure how to and whether it is worth it.

Thank you in advance!

Comments

  • There are three main ways of looping in Harlowe:

    1. Combining a Named Hook with a (live:) macro.
    2. Using the (subarray:) macro to process an array of (display:) macros.
    3. Recursive (display:) macros, although this method can lead to a nesting error.

    Assumptions:
    1. All of the above methods use a variable to track the position of the element in the array currently being processed, mine will be named $index.
    2. You are using a variable to track which item/link was clicked, mine will be named $selected.
    3. The array variable containing the items to loop is named $list.

    The contents of a (link:) macro's associated hook is not evaluated until after the link is selected, because of this the current value of any variables you reference within that content will be whatever they equal after the looping is finished and not what they were at the time the (link:) macro was defined.

    To get around this issue you need to use the (print:) macro to dynamically create each of your (link:) macros. The follow example show how to do this for a single element of a loop, it will be calling a (go-to: "Next Passage") but you can replace that code with whatever you want.
    (print: "(link: $list's ($index))[(set: $selected to '" + $list's ($index) + "')(go-to: 'Next Passage')]")
    
    ... note the usage of both single and double quotes, this is needed because of the accessing of an arrays element via $list's, the assigning of a String value to the $selected variable, and the passing of a String passage name to the (go-to:) macro.
  • Thanks a lot! Changed print to append and made a loop with live. Looks like you saved my test-Saturday.
Sign In or Register to comment.