Help with text in set: command

I was wondering if I could use the (set:) to add text to a variable? How can it work? Will it work? I’ve tried (set: $text += hello) but it doesn’t work! Any help? Thanks, HouseonaTree! P.S.I am using Harlowe

You need to use quotes. Without them, it thinks that hello is a variable name, so it’ll either throw an error or add undefined to your text or something like that.

(set: $text += "hello") 

Since you seem to be new to this, I’ll point out some stuff that will probably trip you up with using strings.

You can either write "hello" or 'hello'. Both work the same. Why have two options? Take a look at these examples while remembering that it’s looking for matching quotes or apostrophes.

Phrase:
1) Bob says, "Hi."
2) This is Meg's cat.

Bad code:
1) $text += "Bob says," Hi.""  <-- string is "Bob says," and the rest errors
2) $text += 'This is Meg's cat.' <-- string is "This is Meg" and the rest errors

Good code:
1) $text += 'Bob says, "Hi."' <-- quotes safely within matching apostrophes 
2) $text += "This is Meg's cat." <-- apostrophe safely within matching quotes

You can also escape (force the script to ignore matching) quotes and apostrophes within strings by using a \. It works but it’s ugly. It’s generally recommended as the fallback only if the previous methods aren’t working for you. For example if you have to mix and match quotes and apostrophes.

1) $text += 'Bob says, "It\'s a nice day out."' <-- would fail after "It" without a \

2) $text += "The collar on Meg's cat says \"Fluffy\"." <-- would fail at quotes around "Fluffy" without escapes

The \ is never displayed in the outputted text, so \" is displayed as " and \' as '. If you ever wanted to display the actual \ character in your text, you have to escape that as well. So \\ is displayed as \.

Hopefully that wasn’t overkill of an answer and was easy to follow along.

2 Likes

tayruh, I would quit coding if it wasn´t for you :blush:! Once again, thanks for saving me and my (future) projects! :slight_smile: HouseonaTree :house: :deciduous_tree:

2 Likes

P.S. I am seeing a slight problem in the code you gave me (I´m not complaining!). When I put $text in a place before the script activates it reads 0. Any idea on how to make the text invisible before the script is run? Thanks, HouseonaTree!

1 Like

I’m surprised it’d be 0. I’d expect it to be undefined. :thinking: But I don’t use Harlowe much, so maybe that’s how it handles it.

Anyway, the best way would be to define it before you use it, even if it’s to give it an empty value. If you want to be blank, do something like this:

(set: $text to "")

This will set it to an “empty string”. It’ll be printed, but it prints nothing.

"abc" + "" + "def" = "abcdef"

Harlowe automatically defaults a variable to zero, if that variable is referenced before it has been defined (assigned a value by the Author).
ex.
1: The following will print “Hello, my name is 0” if $name hasn’t been assigned a value by the Author yet.

Hello, my name is $name

2: The following will throw a the number 0 isn’t the same type of data as the string “hello” error if the $text variable wasn’t previously assigned a String value by the Author.

(set: $text += "hello")
2 Likes

I understand that, the problem I was having was the fact that I put the $text variable in the same passage as the (set: $text to " ") script. I had to put them in different passages, with (set: $text to “”) going first, in order to make it invisible. Once again, thank you to all who contributed! - HouseonaTree!