Howdy, Stranger!

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

[Harlowe] Using hook values in string comparisons

Last last I noticed that if you have a hook whose value is "creature":
You see a [creature]<thing|

You can ask for the value of ?thing to be outputted to the screen like this:
?thing


Output: creature

Which might make you think it is a string that can be compared with other strings. But then if you try this:
(if: ?thing is "creature")[It's a creature!](else:)[It's no creature.]

then the two do not match. It seems like ?thing !== "creature" The only way I got the two to match was by creating a new string from the hook and then comparing that string with my string.

I'm not sure if this is a technical issue or if "is" only returns true when the two objects are the same type.

Comments

  • note: the following explanation has been been simplified, it is not 100% technically correct.

    1. When you write code like [creature]<thing| it creates a tw-hook HTML element with an identifier of thing and that element is appended to the output that will eventually be display as the current page.

    2. The content of the named hook's associated hook is processed and the result becomes the content of the new tw-hook element. This content is not the same as a String.

    3. When you write code like ?thing (known as a hook reference) you are asking the engine to search the HTML structure of the current page for an element with a ID of thing, once it finds one what is done with the tw-hook element depends on which macro is also being used in combination to the hook reference.

    4. When you write code like ?hookName or $variable in a passage without an associated macro you are using a short-cut that is the equivalent of (print: ?hookName) or (print: $variable)

    5. If the parameter passed to the (print:) macro is not a String value then the macro first needs to convert the parameter to a String value. This is what is happening in your example, the tw-hook element represented by the hook reference is being converted into a String value.

    The process is basically extracting the contents of the tw-hook element and appends it to the page.

    6. The Javascript !== operator like the === operator compares data type as well as value of the two things being compared. In your example one side is a reference to a tw-hook element and the other a String, so the data types are not the same.
  • OK. Thanks for the detailed under-the-hood explanation.
Sign In or Register to comment.