Howdy, Stranger!

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

Dynamic passage name? (sugarcube)

edited August 2016 in Help! with 2.0
So I'm trying to make it so a link goes to a specific passage. The player is on passage 8 and they want to go to passage 9, but in this circumstance I need to write 9 in relation to the current passage.

So I tried using:
[[Up|passage()+1]]

and it sends me to passage 81 instead of 9.

I tried it with a minus code:
[[Down|passage()-1]]

and it worked fine (took me to passage 7), the same with divide and multiply but not plus.

Comments

  • edited August 2016
    Your issue is because passage names are inherently strings—i.e. text. Even if you name your passage "9", it's still a string—one holding only numerals, but a string none the less. Ergo, both of your examples are taking a string and combining it with a number.

    The reason the first doesn't work as you wanted, while the second does is because the plus sign (+) is both the arithmetic addition operator and the string concatenation operator.

    Your first example actually looks like the following to the system:
    STRING  (CONCATENATE OR ADD)  NUMBER
    
    Since the first operand is a string, the system decides that plus sign should be the string concatenation operator:
    STRING  CONCATENATE  NUMBER
    
    Which results in the number being converted to a string:
    STRING  CONCATENATE  STRING(NUMBER)
    

    Your second example goes something like the following:
    STRING  SUBTRACT  NUMBER
    
    Since the minus sign may only be the arithmetic subtraction operator in this instance, the system decides to convert the string into a number:
    NUMBER(STRING)  SUBTRACT  NUMBER
    

    TL;DR: Passage names are strings. Don't combine strings and numbers unless you are aware of the consequences.


    If you simply have to do arithmetic with passage names, then you'll need to forcibly convert the passage names to numbers. You may do so like the following:
    [[Up|Number(passage()) + 1]]
    [[Down|Number(passage()) - 1]]
    
    You could also do something like the following, but it's a bit wordier:
    [[Up|parseInt(passage(), 10) + 1]]
    [[Down|parseInt(passage(), 10) - 1]]
    
    Obviously, you should only do either of those for passage names which consist solely of numerals.
  • Thanks, that worked perfectly. I know I probably shouldn't do things this way but it works for what I need it for.
Sign In or Register to comment.