Howdy, Stranger!

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

Find empty value inside array

Let's say I have the item:
(set: $knife to (datamap:
"Name","Knife",
"Description","a simple knife",
"equip", false)
)
(set: $iknife to "(link-reveal: 'knife')[(set: $myeq to it + (a: $knife))]")

And an array to store my items:
(set: $myeq to (a:))

It will be stored inside my array, like:
(set: $myeq to (a: $knife, $otheritem, $etc)

But if my array have an empty value, like:
(set: $myeq to (a: $knife), "", $otheritem, $etc, "", "")

How could I make the item be stored in one of this spaces? Do Harlowe has any sort of foreach or any other loop?

Comments

  • edited June 2016
    I will take it as "it's impossible, give up" ;)
  • edited June 2016
    The subject that your question touches upon is more complex that you may think, but here is a short answer.

    1. Harlowe does not have a looping macro but you can emulate one using either

    a. The (live:) macro as shown in this post.

    b. Using Subarrays as shown in this topic.

    c. Using Javascript as shown in this post.

    Although each of those methods come with their own side-effects, like the (live:) macro interfering with the Reader's ability to interact with the screen.

    2. Your $myeq array does not actually have empty elements, it has elements that are equal to an empty String literal. So you could use code (either TwineScript or Javascript) to loop through the array until you come to the first empty String literal and replace it.

    3. Each time the Reader navigates between Passages all the known variables are copied and that copy is what is made available to the new Passage being shown. This means that the object contained with your $knife variable and the object contained in the knife element of the $myeq array will no longer be the same object, they will now both contain two new objects that have the "same value" as the original object.

    This effect can be demonstrated using two passages:
    a. First
    [[Second]]
    
    ''Setup variable referecing an object and an array element refercing the same object.''
    {
    (set: $knife to (datamap: "Name", "Knife", "Description", "a simple knife", "equip", false))
    
    (set: $myeq to (a:))
    (set: $myeq to it + (a: $knife))
    }
    
    ''Test the object references''
    before change:
    knife.equip: (print: $knife's equip)
    myeq(knife).equip: (print: $myeq's (1)'s equip)
    
    change knife.equip to true (set: $knife's equip to true)
    
    after change:
    knife.equip: (print: $knife's equip)
    myeq(knife).equip: (print: $myeq's (1)'s equip)
    
    ... the two after change values will be the same.

    b.Second
    ''Test the object references''
    before change:
    knife.equip: (print: $knife's equip)
    myeq(knife).equip: (print: $myeq's (1)'s equip)
    
    change knife.equip to false (set: $knife's equip to false)
    
    after change:
    knife.equip: (print: $knife's equip)
    myeq(knife).equip: (print: $myeq's (1)'s equip)
    
    ... the two after change values will no longer be the same.

    note: There are a number of ways to solve this issue depending on what exactly you want to be able to do with those object references.
  • Thank you, I'll read carefully and see what I can do. Anyway, maybe I'll try another way: store items arrays inside a datamap, each item slot holding one, like:

    datamap:
    1, "",
    2,"",
    etc...
Sign In or Register to comment.