Howdy, Stranger!

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

Using a variable in a (click:) macro in Harlowe

edited December 2015 in Help! with 2.0
Hey all,
I've seen from the Harlowe documentation you can use a string variable as the name of a hook. But I can't figure out how to use a string variable as the name of a hook to use in a click macro:
(set: $clickName to $currItem's name + "link")
A (print:$currItem's name) is here. (hook: $clickName)[Take]

(click: ?$clickName)[You take it. (Plus some other code, obviously, but keeping it simple for the example)]

What I'm trying to do (in case there's another or better way to do it), is that in a "location" passage, there can be several items that the player can click on to take.
In the "location" passage, for each item in the location, I set $currItem to be the item in question, then (display:) a "Display Item" passage, which is the code above. The reason I want to use a variable is that, if my understanding is correct, if I use a hardcoded string then in the list of items, they'll all be using the same string, so when I click the link, it won't work correctly (I'd guess it'd either act as if I clicked all the links at once, or the most recent one but I haven't tested it to see).

So, is it possible to use a variable in the name of a hook like that? It seems like it should be, otherwise what's the point of being able to name hooks from a variable in the first place, but I can't figure out the right way to do it (neither ?$clickName, ?clickName or $clickName seem to work). If it's not possible, what would be a good workaround? Also, I'm not married to Harlowe, so if another Story Format would let me do it, I'd be willing to switch (I have programming experience, and some of the other Story Formats seem more programmer-friendly anyway).
Apologies if I missed something obvious, I'm pretty new to Twine, and I tried to search but couldn't find anything that answered my question.

Comments

  • You can access a hook from a variable using the following:
    (click: "?" + $clickname)[You take it]
    

    There's a problem though. Whichever link you click on, $curItem will be set to the last value.
    If this is your location passage
    (set: $curItem to (datamap: "name", "gold bar"))
    (display: "displayItem")
    (set: $curItem to (datamap: "name", "crown"))
    (display: "displayItem")
    (set: $curItem to (datamap: "name", "rotten tomatoes"))
    (display: "displayItem")
    

    And the displayItem passage is
    {(set: $itemName to $curItem's name)
    (set: $clickName to $curItem's name + "link")
    A (print:$curItem's name) is here. (hook: $clickName)[Take]
    
    (click: "?" +$clickName)[You take $itemName]}
    

    No matter what you click on it'll say, "You take rotten tomatoes"
  • Is there a reason that you are using a hook & (click:) macro combination instead of a just (link:) macro?
  • edited December 2015
    You can access a hook from a variable using the following:
    (click: "?" + $clickname)[You take it]
    
    Ah! That makes sense! Thanks!

    You pretty much nailed what's going on in the location passage. What would be a good way to fix that problem? I tried to reset $currItem to be the right item when hovering over the text, but it seems like mouseover only fires once per hook. I might just have to pull the take link out of the Display Item passage into the location.
    greyelf wrote: »
    Is there a reason that you are using a hook & (click:) macro combination instead of a just (link:) macro?
    Actually, no, there's not. That totally would have fixed my first problem. But doing it that way still runs into the second problem prof_yaffle mentioned.
  • There is a way around the problem, but it's very hacky.
    Use this for itemDisplay:
    {(set: $itemName to $curItem's name)
    (set: $clickName to $curItem's name + "link")
    A (print:$curItem's name) is here. (print: "(link: 'Take')[You take the " + $itemName + "]")}
    
    Basically rather than adding the link macro directly this builds up a string containing the macro. Whenever you need to access the itemName it gets added to the string. This forces Harlowe to read the itemName variable when the link is created, rather than when it's clicked.
  • There is a way around the problem, but it's very hacky.
    Use this for itemDisplay:
    {(set: $itemName to $curItem's name)
    (set: $clickName to $curItem's name + "link")
    A (print:$curItem's name) is here. (print: "(link: 'Take')[You take the " + $itemName + "]")}
    
    Basically rather than adding the link macro directly this builds up a string containing the macro. Whenever you need to access the itemName it gets added to the string. This forces Harlowe to read the itemName variable when the link is created, rather than when it's clicked.
    Interesting. Well, unfortunately that won't work because I do want to run other code when the item is taken too, but thanks for the help!
  • vaughantay wrote: »
    ... Well, unfortunately that won't work because I do want to run other code when the item is taken too...
    The build a string and print it technique can be used to run code as well, the string can be as complex as you want within reason. For example:
    (set: $action to "(link: 'Take')[(set: $var to 'Value')(display: 'Passage Name')You take the " + $itemName + "]")
    (print: $action)
    

Sign In or Register to comment.