Howdy, Stranger!

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

Making a variable equal to other added variables?

Let me explain, I'm using harlowe and I thought it would be possible to do (set: $items to $laptop + $pens + $revol) (with those respective variable set also) to allow me to do an if statement later that would allow me to use $items as the variable. On other pages I've set each of the other variables to +1 (so like (set: $laptop to $laptop + 1) etc.) and that much has worked, but not so much for the $items setting. Even when the laptop, pens, and revol variable are each 1, when I do (print: $items) it always comes out to 0. I even tried an if statement with (if: $items is 3)[blah blah blah] when laptop, pens, and revol were each 1 and it just didn't work.

Also please be gentle with me when you explain because I have an extremely small knowledge of coding in general. Thanks!

Comments

  • edited June 2015
    The problem you are having is one of "math" vs "concatenation." In math we say 1 and 1 makes 2, where you are trying to get it to be 11 (because the two 1's are side by side). Make sense? That's concatenating. The same thing works in Microsoft Excel, for example.

    Instead... do this:
    (set: $inv to (a: 'Dagger', 'Shield', 'Potion'))
    (if: $inv.length is 0)[You have nothing.]
    (else:)[Your inventory contains:
    (print: $inv.join("\n"))
    ]
    
  • Well.... I may be wrong. Even though I should be right, from conventional standards.

    here is what I mean:
    (set: $laptop to "test")
     (set: $pens to "again")
     (set: $revol to "over")
     (set: $items to $laptop + $pens + $revol)
     $items
    

    Actually displayed:
    testagainover
    

    That should not have worked (I believe). Though... maybe it still helps you with your original goal.
  • Ohhhh! Now I get why what I was trying to do wasn't working. I tried out
    (set: $inv to (a: 'Dagger', 'Shield', 'Potion'))
    (if: $inv.length is 0)[You have nothing.]
    (else:)[Your inventory contains:
    (print: $inv.join("\n"))
    ]
    
    though and it doesn't do exactly what I wanted for my story. I was attempting to make an inventory that could only hold three items, so once you chose three, you would no longer be able to choose others. Also later in the story the tasks you would be able to accomplish could only be done if you had picked up certain things. Is that possible? Or is that very convoluted?
  • Not convoluted at all. I can help if you need it.
  • edited June 2015
    .
  • Yes please I would definitely appreciate your help on that!
  • @sage overloading + so it performs concatenation if called with non-numeric parameters is pretty common - lots of languages do it, including sugercane and java. You may even find that 2 + 2 + "-" + 2 will print "4-2" - it'll work numerically for the first pair, then switch to string concatenation once it gets to the "-" sign. Logically "22-2" would be an equally valid output - only way to tell is to try it and see how it works. Parentheses should be able to make the expression unambiguous - 2 + ( 2 + "-" + 4) should always be "22-4" because the string operation has to happen first.
  • edited June 2015
    Yeah, @Mykael but correct me if I'm wrong: it's a travesty!
  • The quest for simplicity creates traps for the unwary.
Sign In or Register to comment.