Working with different variable types

Twine Version: Harlowe 2.11

It’s been quite a while since I’ve done any programming in, frankly, anything but I remember one classical issue with anything that ends up using RPG elements and such is the fact that String, Integer, and Float variables don’t always play nice with each other. I know, from testing, that Twine very much does not like it if you try to compare a numerical variable with a string variable, is there any way to, if I’m comparing two variables, make sure they’re being compared a Strings? Like, if Y is 9 and X is “9” is there some phrasing that would allow the programming to check if they’re the same value?

And, in a similar vein, is there an easy method to drop/round the decimal place off a Float to produce an Integer value to compare/assign to another variable? Or am I going to have to create a sequence of math steps to figure that out “manually”, such as it is?

If you were using a more JavaScript friendly Story Format like SugarCube then you could use its ability to coerce a value’s data-type when it’s being compared with a 2nd value of a different type.
eg. In JavaScript and thus SugarCube the following comparisons have different outcomes depending on the number of equals signs used…

9 === 9     => true, the values are the same and have the same data-type.
9 === "9"   => false, because the values have different data-types.
9 == "9"    => true, because the number 9 is coerce to string "9" before the compare.

However Harlowe doesn’t support JavaScript’s data-type coercion, so you will need to manually do the type conversation.
eg. if one variable contains a number, and the other variable contains a String representation of that number…

(set: _number9 to 9, _string9 to "9")

(if: (str: _number9) is _string9)[...the numerical values is the equivalent of the string value...]

Regarding converting a floating point number into an integer, there are a couple options you can use: the (trunc:) macro; the (floor:) macro; and the (ceil:) macro. Which you use defined on the specific use-case, because there are subtle differences between them.

1 Like

Honestly (str: ) covers basically everything I needed for what I was working with in regards to that part of the question, I just hadn’t found it in the documentation, and I knew Harlowe could be less coding robust than some of the others (as you pointed out) and so I was worried it just… Didn’t exist. Must have just missed it in the documentation.

Similarly, thank you for directing me towards the correcting macros for the other portion as well. Did spend a lot of time scrolling documentation and just hadn’t found anything like them where I expected to, so much appreciated all around.

Unlike some other story formats like SugarCube or Snowman, Harlowe isn’t a thin abstraction layer over JavaScript.

Harlowe:

  • includes its own custom implementations of data-types like Array & Data-map.
  • doesn’t support automatic data-type conversion.
  • uses position (1) instead of offset (zero) based Array indexing.
  • doesn’t support Bracket or Dot based Notation when accessing Array elements or Object properties.

So it’s less a case of “being less coding robust”, and more a case of some people expecting to be able to write JavaScript like code in a language that isn’t JavaScript. It’s a little like someone complaining that their JavaScript like code doesn’t work in a Python based project. :slight_smile: