Howdy, Stranger!

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

Booleans as numeric values

Hi all,
in twine, are the numeric values of "true" and "false" guaranteed to be 1 and 0 respectively? I know that in some other languages, you only know that "true" is nonzero.

The reason I'm asking is that I'm trying to do something like this:
<<set $kindness to $walked_dog + 2*$fed_horse + 5*$rescued_kitten>>
and I would like to be sure that the result will be 8 if those three things are true, no matter what computer this game is played on.

The alternative would be to do
<<set $kindness to 0>>
<<if $walked_dog>><<set $kindness += 1>><</if>>
and so on, but that's horrendous and I'd rather not have to.

I am using Twine 2.0.11 and Sugarcube 2.14 if that matters.

Comments

  • Why is that horrendous?
  • jhocking wrote: »
    Why is that horrendous?

    Why do something in multiple lines if I can do it elegantly in one line? Not to mention having to wrap it in <<silently>> to avoid lots of unwanted white space, and that would be even more clutter.
  • Preface. In JavaScript, the boolean values, true and false, are an actual boolean type—i.e. they are not numbers, though they can be coerced into numbers. Some languages do not have a boolean type and use numbers, generally the integers 1 and 0, as stand-ins.

    Now, to what you really want to know. When boolean values are coerced into numbers, true should yield the value 1 and false the value 0. Thus, yes, what you're attempting to do there should work.

    Coercing in the opposite direction is not so clear cut, as while the number 0—and NaN—does coerce to false, all non-zero numbers coerce to true.
  • Excellent, thank you. That's exactly what I needed to know. I will not be coercing in the other direction, so that issue will not come up.
Sign In or Register to comment.