Toggle Boolean Variables

Discussion in 'Suggestions' started by Nachtlicht, Jun 7, 2023.

  1. Nachtlicht

    Nachtlicht Virgin

    Greetings!

    As it is now (correct me if I'm wrong, I'm new here), when adding changes to the score to a chapter, you can change a Boolean variable to either "true" or "false".

    I propose to add a third option, with which you can toggle it - if it was set to "true" the moment the reader chose that particular chapter, it is now set to "false", and vice versa - without you, the author, having to know beforehand which one it is.

    This would give Boolean variables an advantage over the other variable types it doesn't really have right now (as far as I can tell); I could just as well use a numerical variable and set it to 1 for "true" and 0 for "false" to emulate a Boolean variable.

    More importantly, I can think of a way or two to use this function. What I have in mind specifically is a basic random number generator - for (a simplified) example, I may have two different Boolean variables, and my story branches at several points (but links back together later). Some branches may toggle the first variable, some toggle the other one. After several of these branches, there are four different ways these variables could be set, depending on what choices the reader made.

    This wouldn't really work as well if all I can do is set those variables to either "true" or "false", as the last choice the reader makes will overwrite the ones before it. Choose chapter x, and you will always receive result y, regardless of what you did before.

    I hope I managed to get my point across; otherwise feel free to ask. If there is already a way to emulate what I'm trying to do with the system that's already in place, please let me know!
     
  2. insertnamehere

    insertnamehere Really Really Experienced

    This would be a valuable and fairly simple improvement.

    For reference, it's already possible to achieve this with two chapters:
    A - B1 - C
    - B2 - (link to C)
    where B1 has the access condition (variable is False) and sets variable to True, B2 has the access condition (variable is True) and sets it to False, B1 and B2 have identical text and names, and the children of B2 each link to a different child of B1, with matching names. It's unwieldy, but if you have unavailable options set to invisible, the reader will hardly notice the difference.

    This actually won't work.

    Suppose you have chapter pairs A1 and A2, B1 and B2, C1 and C2 etc. such that both B1 and B2 can be accessed from either A1 or A2, and both C1 and C2 can be accessed from either B1 or B2, etc. where the X1 chapters toggle Boolean variable y and the X2 chapters toggle Boolean variable z. Assume that y and z are initially False (it doesn't matter). I'll denote the combination yz as either 00, 01, 10 or 11.

    Initially, yz = 00.
    After A, yz = 01 or 10.
    After B, yz = 11 or 00 (from 01) or 00 or 11 (from 10)
    After C, yz = 01, 10, 01, 10, 01, 10, ....

    In other words, 00 and 11 after one bit-flip are only ever 01 or 10, and 01 and 10 only ever become 00 or 11. Therefore, if you do this process an even number of times, you will only ever get 00 or 11, and if you do it an odd number of times, you will only ever get 01 or 10. There is a way to prove this with Boolean algebra - each chapter depth can be considered an operation of either XORing with 01 or with 10, which either adds or removes a 1 from the number, which means that an odd number of 1's becomes an even number and vice versa. At two bits, this leaves you with just two choices.

    A more effective strategy to generate a pseudo-random number in this manner can be achieved with a regular number and constant additions of powers of 2. Suppose that now, you start with a variable n = 0. A1 does nothing, while A2 adds 1 to n. B1 does nothing, while B2 adds 2 to n. C1 does nothing, while C2 adds 4 to n. D1 does nothing, while D2 adds 8 to n.

    Initially, n = 0.
    After A, n = 0 or 1.
    After B, n = 0 or 2 (from n = 0) or 1 or 3 (from n = 1).
    After C, n = 0 or 4 (from n = 0) or 2 or 6 (from n = 2) or 1 or 5 (from n = 1) or 3 or 7 (from n = 3).

    This gives you a number result that represents a unique path through all of these choices. You can then split this number however you like - if you want a chapter to appear 50% of the time, and n is between 0 and 15, take a range like 4 <= n <= 11 (to avoid it being determined by just the last choice). This solution is also scalable if you want more than two choices per layer by fiddling with the constants you add.
     
    gene.sis and TheLowKing like this.
  3. Nachtlicht

    Nachtlicht Virgin

    I should mention that my example is extremely simplified; what I have in mind is rather complex and needlessly ambitious. I'm not going to go into too much detail without having anything to show for it (I am still in the planning phase, and might be in over my head), but in short, the reader will have the choice to visit chapters multiple times and/or skip chapters. At chapter x, when the "random number generator" comes into play, I, as the author, don't necessarily know which and how many chapters the reader has accessed.

    This would also make your binary-based RNG difficult to implement... although I will definitely keep that in mind, I hadn't thought of that.

    This would work with what I have in mind; however, I will probably need more than two Boolean variables, which means three or more copies of every chapter, and with the scope I'm planning, unwieldy doesn't even begin to describe it. Still, I will keep this in mind, as well.

    Thank you for your reply!
     
  4. insertnamehere

    insertnamehere Really Really Experienced

    The toggling issue would indeed be fixed if the number of chapters visited may be either odd or even, but you would need to be careful that it's not something the reader would be motivated to visit a certain number of times (e.g. twice, to verify that the content doesn't change on each visit). With the current CHYOA system, I think it would be easier to just set the variables outright - perhaps it could be dependent on the last chapter visited out of two which are equally likely to be so, with a different such pair for each Boolean.
     
  5. TheLowKing

    TheLowKing Really Really Experienced

    All things worth doing are. ;)
     
    Nachtlicht and insertnamehere like this.
  6. gene.sis

    gene.sis CHYOA Guru

    I don't understand why you would need to use Boolean variables.

    If the problem with insertnamehere's suggestion is that you don't know where the cutoff number is (between 7 and 8 with 4 decisions), you could just "randomly" add and subtract numbers for each chapter.

    E.g. you start at 0.
    Chapter 1A +1, chapter 1B -1
    Chapter 2A -1, chapter 2B +1
    Chapter 3A +1, chapter 3B -1
    Chapter 4A -1, chapter 4B +1
    Chapter nA ...

    In the end, you can decide which of the two possible outcomes it will be by checking
    Score < 1
    and
    Score > 0

    Depending on the circumstances, it might also make sense to weigh some decisions with higher numbers.
    If you don't want to check for 0, you could use +/-1 for a choice that will only happen once and then use even numbers for the rest of the decisions.
    (It might also be possible to use 2 variables to achieve 4 different results.)
     
  7. insertnamehere

    insertnamehere Really Really Experienced

    Like the Boolean toggling example, this doesn't create an even distribution.

    Initially, x = 0
    After 1, x = +1 or -1
    After 2, x = +2, 0, 0, or -2
    After 3, x = +3, +1, +1, -1, +1, -1, -1, or -3
    After 4, x = +4, +2, +2, 0, +2, 0, 0, -2, +2, 0, 0, -2, 0, -2, -2, or -4

    in other words, there are just 5 possible results: +4, +2, 0, -2, and -4. The extreme values (+4 and -4) are the least likely, with only 1/16 chance of occurring each; 0 is the most likely, with a probability of 6/16. Values become more likely the nearer they are to 0, so this method actually follows a binomial distribution. This makes sense, because it's always deciding between two equally weighted options.

    This is all in addition to the similar problem of Boolean toggling, where the result must always be even or odd. Because this is a number, it does grow it range, but the likely range grows very slowly (after 4 iterations we have just 3 probable outcomes). This technique might be useful if you want this result to determine some outcome of one specific event, where certain outcomes are significantly less likely than others, but at that point you might as well make it actually tied to the choices the reader makes.

    As mentioned, it can also be used for a coin flip (checking x > 0 and x < 1 skews it significantly with an even number of choices, but with an odd number, x > 0 and x < 0 works anyway as 0 is impossible). However, all of the methods so far can be used for a coin flip. This might be one of the best ways for CHYOA as Boolean toggling is currently cumbersome.
     
    gene.sis likes this.
  8. gene.sis

    gene.sis CHYOA Guru

    When using a Boolean variable, there are only 2 outcomes anyway. So it is not a requirement that each possible number will be hit with the same probability.
    That means it would be accurate enough to have two number ranges that will be hit with the same probability, i.e. <0 and >0 if 0 can be ignored.

    Due to the unknown number of chapters, we don't know whether it's an even or an odd number.
    So if there is an even number of chapters, one possible outcome will be 0 and we don't know whether it is "+0" or "-0".
    The probability of that is 37.5% after four chapters and will slowly decline with more chapters. With a "random" number of chapters, you might halve the probability as every other chapter won't have 0s.
    After 12 decisions, the probability of two consecutive decisions would be around 10%, skewing the result to 55/45.

    To avoid that, you can do +1/-1 for a single decision and +2/-2 for all other decisions, so you get an actual coin flip. (Due to the fact that the result is determined by the decisions it's not really random.)

    There might also be the possibility of more than two options per decision that will change the probabilities for each option.
     
    Last edited: Jun 18, 2023
    insertnamehere likes this.
  9. gene.sis

    gene.sis CHYOA Guru

    While 4 to 11 considers 2^3, it ignores the difference created by the settings of 2^0 and 2^1, making the first two decisions pointless.
    2^3 and 2^2 might result in 0, 4, 8, or 12.
    4 and 8 will result in 4, 5, 6, 7, 8, 9, 10, 11 -> same range as 4 and 8
    0 and 12 will result in 0, 1, 2, 3, 12, 13, 14, 15 -> same range as 0 and 12
     
    insertnamehere likes this.
  10. insertnamehere

    insertnamehere Really Really Experienced

    That's true, and a consequence of starting at a perfect power of two. You can take more uneven ranges (or combinations of range, if you feel like it) to get better pseudorandomness. That said, true pseudorandomness would occasionally not be dependent on every decision.
     
    gene.sis likes this.