Howdy, Stranger!

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

"If" variables not working?

I have a bunch of things that change in my story if you've died one or more times and are replaying from checkpoints.

I've successfully got it working where it's adding a +1 to the $death_count variable on a loss, but for some reason the If conditional to show certain text isn't working.

For example:

(if: $death_count > 1)[Text here] doesn't produce anything regardless of whether or not there's a death, but weirdly if I set it to the opposite (so if $death_count is less than 1) the text does appear. I'm not quite sure if I'm doing something wrong, or if there's a bug.

Comments

  • You should assign a value to a variable before you use it in a conditional statement like an IF.

    The following code works:

    Assign a default value to the variable: `$death_count` is {
    (set: $death_count to 1)
    $death_count
    }

    Increment the variable by 1: `$death_count` is now {
    (set: $death_count to it + 1)
    $death_count
    }

    Check if the `$death_count` variable is greater than 1:
    (if: $death_count > 1)[The `$death_count` variable WAS greater than 1](else:)[Something went wrong]
  • Are the 'or equal' conditions implemented yet? The code: (if: 3 > 2)[ayo] works fine, but not if I replace the '>' with a '>='. Or am I being silly here?
  • piato wrote:

    Are the 'or equal' conditions implemented yet? The code: (if: 3 > 2)[ayo] works fine, but not if I replace the '>' with a '>='. Or am I being silly here?


    Both the following work for me:

    (set: $death_count to 2)
    (if: $death_count >= 2)[The `$death_count` variable WAS greater than 1](else:)[Something went wrong]

    (if: 3 >= 2)[Three is greater than Two](else:)[Warning Will Robertson]
  • greyelf wrote:

    You should assign a value to a variable before you use it in a conditional statement like an IF.

    The following code works:

    Assign a default value to the variable: `$death_count` is {
    (set: $death_count to 1)
    $death_count
    }

    Increment the variable by 1: `$death_count` is now {
    (set: $death_count to it + 1)
    $death_count
    }

    Check if the `$death_count` variable is greater than 1:
    (if: $death_count > 1)[The `$death_count` variable WAS greater than 1](else:)[Something went wrong]


    It doesn't work for me. The problem with assigning it an arbitrary value ahead of time is that I have a tally showing how many times a player has died (and this is presented at the checkpoints and also the beginning of the game), so if I assign it a value of 1 then it's just going to show one death inaccurately.
  • Further if I assign it a value at the start, it's going to reset the variable that should trigger whether or not the extra flavour text appears.

    In Twine 1.4x I only incremented $death_count each time the player died and this worked both with the ongoing tally and determining whether or not the additional content appeared. I can't get the same to work in Twine 2.0.
  • Like 1.4.x, variables are defaulted to 0 whenever they are undefined. (I plan to add some kind of switch that disables this as an opt-in, which is something I hadn't gotten around to in 1.4.) "$not_a_variable" prints 0, for instance.

    So, I'm not sure what the problem is. How exactly do these checkpoints work?
  • mixvio wrote:

    The problem with assigning it an arbitrary value ahead of time is that I have a tally showing how many times a player has died (and this is presented at the checkpoints and also the beginning of the game), so if I assign it a value of 1 then it's just going to show one death inaccurately.

    You can assign it whatever value you like, I only used 1 in the example because it allowed me to do what you described in your original post. eg. Increment a variable by 1 and then check if the current value of the variable was greater than one.

    Many years of programming in many different languages has taught me to not rely on the "the variable will have a good default value if you have not defined one" concept as it can change without notice, so I suggest  always assign one yourself then you know what its default  value will be as some story formats assign default values to undefined variable and some don't.
  • L wrote:

    Like 1.4.x, variables are defaulted to 0 whenever they are undefined. (I plan to add some kind of switch that disables this as an opt-in, which is something I hadn't gotten around to in 1.4.) "$not_a_variable" prints 0, for instance.

    So, I'm not sure what the problem is. How exactly do these checkpoints work?


    On a passage where the player dies, I have

    (set: $death_count + 1)

    At various points in the game I have text that appears if the player has died at least once but which should be invisible otherwise. It works correctly in not showing the text when the player hasn't died yet (or if I change the If conditional to be less than one death instead of greater than) but it doesn't appear when the player has.

    Even cutting and pasting the code from greyelf didn't work.
  • Ignore me, I stupidly had the greater-than value at one, so it wouldn't show up obviously until the player had died twice.

    Never code on a hangover, kids.
  • mixvio wrote:

    On a passage where the player dies, I have

    (set: $death_count + 1)

    I think you have mistyped the above because that would result in a "(set:)'s 1st value is the number 1, but should be an assignment operation." error message.

    I think you meant to type one of the following:

    Assignment is either
    (set: $death_count = 1)
    or
    (set: $death_count to 1)

    Incrementation is either
    (set: $death_count = it + 1)
    or
    (set: $death_count to it + 1)
  • (if: 3 >= 2)[Three is greater than Two](else:)[Warning Will Robertson]

    This works flawlessly inside a passage. However, when I 'display' it remotely from another passage, I get a syntax error, which does not occur with simple GT.

  • This is a problem with the (display:) macro, there are known issues with (display:) not working as expected and Leon has stated that there should be a fix soon.
Sign In or Register to comment.