Howdy, Stranger!

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

click the number to countdown, recursivity and nesting

Hello,

I am on last 2.0 and Harlowe.

For a passage, I would like the user to be able to decrease a number by clicking it (or clicking some other word), and do something when it reaches 0.

Say, the user has 10 chips and I want him/her to eat them, one by one, and show the remaining chips every time the user eats one.

So I was trying something like:

(set: $chips to 10)
There are still (link: "$chips")[(set: $chips to it -1)] chips left
... so when the number is clicked, the variable decreases by one. The problem is that I want the clicked number to show chips left after each bite. And so, idiotic recursive nesting problem arises, i.e:


(set: $chips to 10)

There are still (link: "$chips")[(set: $chips to it -1)(link: "$chips")[(set: $chips to it -1)]] chips left
... with that code, the user can click the "10", then a "9" appears in its place. But to be able to reach 0, I should nest that 9 more times within itself! crazy

Is there any other way you would reccomend this to be done?

I assume I could (link:) another word, use it to decrease the $chips variable, then use a (live:) somehow to constantly check, and display the updated variable ?

Any help is greatly appreciated


Comments

  • I just realized I am trying to achieve some sort of loop, so searched for loops in the forum, but did not really find anything something that I can understand how to apply here.

    Thanks again.
  • NOTE: Even though the following solution works, I am not sure if it is a good way to do it!

    You can do what you want with two variables, a hook, and a (live:) macro:

    (set: $chips to 10)
    (set: $selected to false)

    There are still [(link: "$chips")[(set: $chips to it -1)(set: $selected to true)]]<amount| chips left.

    {(live: 20ms)[(if: $chips is 0)[(replace: ?amount)[0](stop:)](elseif: $selected)[(replace: ?amount)[(link: "$chips")[(set: $chips to it -1)(set: $selected to true)]](set: $selected to false)]]}
    Code breakdown:

    1. The amount hook:
    We use a hook so we can replace its contents with a new copy of the (link:) macro each time the link is clicked.

    [
    (link: "$chips")[
    (set: $chips to it -1)
    (set: $selected to true)
    ]
    ]<amount|
    2. The (live:) macro:
    We check every 1/50th of a second to first see if all the chips are gone, if they are we display a zero and stop the (live:) macro, otherwise we check the $selected variable to see if the links was clicked. If it was clicked we reset the hooks contents so it contains the (link:) macro again and reset the $selected variable.

    {
    (live: 20ms)[
    (if: $chips is 0)[
    (replace: ?amount)[0]
    (stop:)
    ]
    (elseif: $selected)[
    (replace: ?amount)[
    (link: "$chips")[(set: $chips to it -1)(set: $selected to true)]
    ]
    (set: $selected to false)
    ]
    ]
    }
    You may want to change how often the (live:) macro runs by making the 20ms a large value.
Sign In or Register to comment.