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
There's a problem though. Whichever link you click on, $curItem will be set to the last value.
If this is your location passage
And the displayItem passage is
No matter what you click on it'll say, "You take rotten tomatoes"
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.
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.
Use this for itemDisplay: 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.