A Quick and Easy Explanation of 'or's' and 'and's' in *if statements

So, one of the things I didn’t understand (as someone who never coded anything before doing my game) until very recently was how to make sure I was coding ‘or’s’ and ‘and’s’ in my *if statements correctly. Then, I taught myself how to read the statements. Then I realized a really easy shortcut to making sure the parenthesis added up correctly. I’ll use one of my upcoming Text Box Investigation sequences as an example.

*if (((((currentclue = "Pipe") or (currentclue = "pipe")) or ((currentclue = "Water Pipe"))) or (((currentclue = "water pipe")))) or ((((currentclue = "Water pipe")))))

To give some context, the variable currentclue is being typed in by the PC via a text box. So if they type in apple then currentclue is set to apple. This may be a lot to look at for someone new to coding, so let me break this into bite sized pieces. First, let’s go through a statement that’s not comparing anything. A simple statement for a single variable with different outcomes like the one above.

First thing I want you to do is count the amount of variables that are included in this long string of ‘or’ statements. So essentially, how many times does currentclue pop up? Five times. Congrats, first step done! Seriously, once you know that, the rest is really easy!

Second thing I want you to do; look at each end of the total statement, where there’s the most parenthesis stacked on top of each other like an American fast food burger. The very left, right after *if and at the very right. Now you’re going to count the number of parenthesis on each side. Do they match the amount of currentclue variables in the statement? As in, do the left and right sides each have 5 parenthesis each? There ya go! That’s step two! Make sure the amount of parenthesis on the far ends of the full statement match the amount of variables in your statement.

It will look like this:

*if (((((currentclue = "Pipe" or currentclue = "pipe" or currentclue = "Water Pipe" or currentclue = "water pipe" or currentclue = "Water pipe")))))

Third thing I want you to do is enclose every one of those statements in just a regular set of parenthesis, but don’t add anymore onto the ends. So at this point, it will look like this:

*if (((((currentclue = "Pipe") or (currentclue = "pipe") or (currentclue = "Water Pipe") or (currentclue = "water pipe") or (currentclue = "Water pipe")))))

Now you’ve determined where your individual statements are. Easy, right? Basically what is starting to take shape is that, if the player types in Pipe pipe Water Pipe water pipe or Water pipe into the text box it will take them to the same place. That is the goal of this exercise.

Okay, fourth step. Start on the VERY LEFT of the statement. Those 5 parenthesis after the *if. I want you to focus on going to the end of each INDIVIDUAL STATEMENT. The parenthesis on the right of each individual statement, are going to be whatever numbered statement it is in the sequence. So, "Pipe" is the first statement in the sequence. Therefore, it has 1 parenthesis at the end of it. "pipe" is the second statement in the sequence, so it’s going to have 2 parenthesis at the end of it. Keep doing that until the end, where the fifth and final statement "Water pipe" already has 5 parenthesis at the end of it.

So now, it’s going to look like this:

*if (((((currentclue = "Pipe") or (currentclue = "pipe")) or (currentclue = "Water Pipe"))) or (currentclue = "water pipe")))) or (currentclue = "Water pipe")))))

Finally, for every individual statement in the sequence except the first one, count how many parenthesis are at the end of it, then subtract 1. For instance, going from right to left, the very last statement has 5 parenthesis at the end of it. So, do 5 - 1 = 4. You will put 4 parenthesis in the beginning of that individual statement. The end result is the correct sequence of code!

*if (((((currentclue = "Pipe") or (currentclue = "pipe")) or ((currentclue = "Water Pipe"))) or (((currentclue = "water pipe")))) or ((((currentclue = "Water pipe")))))

That sequence makes it so if a player types in any of those variations of Pipe or Water Pipe that it will take them to the same text passage. You can add as many sequences as you want to a line of code like this, as far as I know! So if you’re coding the introduction to your game, and you don’t want to allow the player to pick a variety of names, it could look something like this!

*label entername

*if (((((((((((((name = "Elizabeth") or (name = "Liz")) or ((name = "Art"))) or (((name = "Arthur")))) or ((((name = "Kris"))))) or (((((name = "Kristina")))))) or ((((((name = "Chris"))))))) or (((((((name = "Christina")))))))) or ((((((((name = "Sam"))))))))) or (((((((((name = "Samuel")))))))))) or ((((((((((name = "Sarah"))))))))))) or (((((((((((name = "Carter")))))))))))) or ((((((((((((name = "Harper")))))))))))))

*goto nametaken

*label nametaken
This is the name of a prominent character in the story, apologies! Please pick another name!

*goto entername

If you can understand this concept, you can compare multitudes of variables as well! Just remember, choicescript only allows two total sequences of comparison. So again, going with the basics. Let’s use our last line of finished code we just did.

*if (((((currentclue = "Pipe") or (currentclue = "pipe")) or ((currentclue = "Water Pipe"))) or (((currentclue = "water pipe")))) or ((((currentclue = "Water pipe")))))

Now say we want to take them to that text sequence if they type in any of that, but only if they have enough Intelligence. It would then simply look like this:

*if (((((currentclue = "Pipe") or (currentclue = "pipe")) or ((currentclue = "Water Pipe"))) or (((currentclue = "water pipe")))) or ((((currentclue = "Water pipe"))))) and (Intelligence >= 25)

Checking the intelligence is the second variable out of 2 total. That’s simple! But what if you have more than two variables? Well, you check them all inside of a single sequence. You use the same exact thing you did in the first sequence, but instead, start it fresh from the second!

So if you wanted to check for Intelligence and Awareness and if they found the Water Line earlier as a previous clue, it would look like this:

*if (((((currentclue = "Pipe") or (currentclue = "pipe")) or ((currentclue = "Water Pipe"))) or (((currentclue = "water pipe")))) or ((((currentclue = "Water pipe"))))) and (((Intelligence >= 25) and (Awareness >= 20)) and ((foundwaterline = "True")))

So now it’s checking if the player typed in any of the phrases they needed to in order to look at the clue, and it’s checking if the stats are high enough, and it’s checking if the variable for finding the Water Line equals True or not. This full sequence means:

If they type in Pipe, pipe, Water Pipe, water pipe or Water pipe, and if their Intelligence is >= 25, and if their Awareness is >= 20, and if the variable foundwaterline = "True", only then will the player be taken to the appropriate text.

This is a key feature for how I do text box investigations in The Bureau, and I hope by following this step by step tutorial, more people will be willing to add fun little text box mini-games into their games as well! Happy writing/coding, and remember, everyone starts somewhere!

P.S. If any of this is wrong, someone more knowledgeable than me, please let me know below and I will be sure to fix it. This should all be correct though, as I tested it in my game and it did work.

8 Likes

I know this is just a demo to show how to use brackets and statements, but can I make a suggestion on how to streamline this (as someone who uses text entry for riddles in a few of my games.)

It’s often a good idea to reset the player’s answer to a single one. Example here:

image

(I know this is in 3 separate lines instead of one long one like yours. Either is ok, I just found this format easier to make sure I covered all the bases for each riddle by not having too many in each line for this particular code for the riddle game.)

Anyways, similar to yours where you know the player might have entered the word with or without a capital, just reset the one with a capital to a word without it, and then when you’re checking it throughout the game you only need to check one variable, not multiple. The simpler you go, the less brackets you need to mess about with which has the potential to misplace them and cause errors.

ie

*if (((currentclue = "Pipe") or (currentclue = "water pipe")) or (currentclue = "Water pipe"))
   *set currentclue "pipe"

I also recommend notepad ++ if having problems understanding where to put brackets which when you click on the bracket, it shows its pair.

5 Likes

You guys can do:

*if (("$!!{answer1}" = "COMB") or ("$!!{answer1}" = "COMBS"))
   *set answer1 "comb"
5 Likes

Oh yeah, most definitely. If you’re keeping what the answer is as a permanent stat that will be checked later on, setting it as one answer no matter what is the way to go. In my game, for the investigations, the answers are essentially a means to an end.


So if the players enter any of the answers, and that variable isn’t true, they see that description. The arrow points to the variable in question, which gets ticked as “True” once they get that description. After that, any necessary stand-out clue from the description goes in a list in the stats menu, and if they enter any of those answers again, it takes them to a screen that says they’ve already looked at that place and should look somewhere else.

But regardless, at the end of it, the player is taken back to the same text box so the variable is overridden once they search someplace else.

Interesting, I’ll be honest I don’t really fiddle around with the capitalization commands with variables. Does that cover all 6 possible input variations of ‘combs/comb’?

Ah, I thought there was something like that mentioned somewhere. Was wondering if I’d imagined it but yes that was the way to take out the capitalisation issue.

1 Like

Yes, this was my first thought. Simply eliminate the different variations on capitalization, by making the text input all upper-case.

In situations where I have to string together more than three comparisons, I usually simplify by dumping them in pairs (or threes) into *temp variables. Giving the temps significant names has the bonus of making the code easier to scan and quickly understand. The main benefit, of course, is avoiding The Devil’s PEMDAS.

Quick example:

*temp rose_jealous (rose_romance and (rose_approval > 50))
*temp lily_jealous (lily_romance and (lily_approval > 60))
*temp rose_lily_meet (rose_present and lily_present)

*if (rose_lily_meet and (rose_jealous or lily_jealous))
    *goto jealousy_encounter

The end goal is for the statement in the *if command to read more or less like a simple English sentence: “If Rose and Lily meet, and one or the other of them is romanced, they will have a jealous encounter.” Just like what you would write in your outline.

1 Like

It covers all possible variations of “comb”, even “cOmB”.