Howdy, Stranger!

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

How to have multiple IF conditions?

How can I have multiple conditions in an IF statement?

As a way of example below I want to do something along these lines in one passage but cannot find any examples.

<<if $box1==true and $box2==false and $box3=true>>
Blah blah
<<endif>>

<<if $box1==false and $box3==true>>
Different Blah blah
<<endif>>

Comments

  • http://twinery.org/wiki/expression

    <<set $foo = "true">><<set $voo = "false">>
    <<if $foo eq "true" and $voo eq "false">>A winner is you!<<endif>>
  • Sharpe wrote:

    http://twinery.org/wiki/expression

    <<set $foo = "true">><<set $voo = "false">>
    <<if $foo eq "true" and $voo eq "false">>A winner is you!<<endif>>


    Thanks for that Sharpe.  Is there a limit to how many "and" "or" within a IF statement can be use?

    Also is there a limit to how many IF conditions/statements you can have in one passage?  because i have a few of them and it seems to be only passing the first If statement out of six I have in total. I am checking them with displaying the variables in their current state for debugging purposes and according to those some of the other IF conditions should be displaying the appropriate text but they are not. I am not seeing any error messages either, syntax is correct.

    Surely you can have more than one IF statement in a passage?
  • You can.

    Are you using <<else>>? Sometimes nested if conditional branches can be tricky.

    Upload your story file and let's have a look.
  • Sharpe wrote:

    You can.

    Are you using <<else>>? Sometimes nested if conditional branches can be tricky.

    Upload your story file and let's have a look.


    This is my first go at a Twine story and I am experimenting with it as I go, its first draft as far as the writing goes and I will be polishing that side of it up last.

    Here's my story file:
    http://www.mediafire.com/view/ducd9h780l4rqsw/TheRaid_V1.tws

    The passage im using multiple IF statements where I am having problems with is called "floor1A",
    as stated the first If statement passes and when I play through (power cut was chosen earlier as an option) but the if powercut is true isn't outputting the paragraph when it should be. Neither any of the others if conditions are met.

    These conditions are set in earlier passages of" Remotedet" or  "sabotage1".

    I was hoping to add some kind of basic combat system to make the game more dynamic, but its pretty much scripted so far which I might keep so its easier to track everything.
  • First of all, I re-wrote that CSS and posted a link to it. Attached is a screenshot of what the game looks like at my screen resolution (1600x900). You can just copy and paste the new CSS wholecloth into your game and it will work (and fix the pictured problem). Oh, except the font. I uploaded a new font with the CSS file. So, copy that passage over as well.

    Secondly, I must be missing something because none of the passages you describe are in the game file. So, I think you posted the wrong file. XD

    Take a look and re-up and I'll look it over.

    Like I say, nested if's can be tricky. However, 1.4 has "else if's" that make them a lot easier.

    Try this:
    <<set $foo = 2>>
    <<set $coo = 2>>

    <<if $foo eq 1 and $coo eq 1>>The number is 1.
    <<elseif $foo eq 2 and $coo eq 2>>The number is 2.
    <<elseif $foo eq 3 and $coo eq 3>>The number is 3.
    <<else>>
    Error.
    <<endif>>
  • Sharpe wrote:

    First of all, I re-wrote that CSS and posted a link to it. Attached is a screenshot of what the game looks like at my screen resolution (1600x900). You can just copy and paste the new CSS wholecloth into your game and it will work (and fix the pictured problem). Oh, except the font. I uploaded a new font with the CSS file. So, copy that passage over as well.

    Secondly, I must be missing something because none of the passages you describe are in the game file. So, I think you posted the wrong file. XD

    Take a look and re-up and I'll look it over.

    Like I say, nested if's can be tricky. However, 1.4 has "else if's" that make them a lot easier.

    Try this:
    <<set $foo = 2>>
    <<set $coo = 2>>

    <<if $foo eq 1 and $coo eq 1>>The number is 1.
    <<elseif $foo eq 2 and $coo eq 2>>The number is 2.
    <<elseif $foo eq 3 and $coo eq 3>>The number is 3.
    <<else>>
    Error.
    <<endif>>


    Thanks for your reply. I haven't touched twine today, but I will give your else if example a go, that might be the ticket. I always thought you could only use one else per if statement. 

    I am sure I have given you the correct file, there is only one. The passages you were looking for are on the very bottom left. The passage with the bunch of IF statements is to the left of the only two passages showing an orange !. Its a pity there isn't an info option that will tell you how many passages there are currently in total (including word count- not including code just for stat purposes) you have as that would be a good way to compare to files, but i suppose you can also go by file size.

    The updated css you mentioned is on the Sharpe Stylesheet thread of yours right? I'm wanting to keep the look/style I currently have. When you add a <hr> line in, is there a way to set a colour for it too? I'll look into it tomorrow.

    Thanks again for your help.
  • I have updated the CSS style sheet, I am sticking with the Lucida Console font. The Dos VGA one does suits a computer terminal look though.

    I do not know how to get everything looking as I had it before, with the stats list on the left and the target info at the top.  Now its reverse, on the right side with the target info on the bottom and a gap at the top.

    At present I have to scroll down the screen to read the rest of the intro, where as before it all displayed compactly on the screen with room to spare. Also is there a way to force word-wrap in a story region? one of the reasons in the previous version it looked skewed on your display was I had increased the border width to accommodate new
    things I was adding to the inventory list as it would write over the top of the border. If I could word wrap it within the border then I don't need it so wide. I have reduced the font size to 16 but even the previous css version had larger text and could display all of it on screen.

    How tricky is it to set it up the way you want versus how it will look on other peoples display configurations?

    I will keep playing with it and see how it all works.
  • I'm answering in haste.

    Dazakiwi38 wrote:
    I do not know how to get everything looking as I had it before, with the stats list on the left and the target info at the top.  Now its reverse, on the right side with the target info on the bottom and a gap at the top.


    What?! I tested it on a friend's computer and it looked fine. Yeah, that's a problem. A big one, and it stems from the note I made about it not being centered how I would like to center it. I'll take a look.

    Dazakiwi38 wrote:
    At present I have to scroll down the screen to read the rest of the intro, where as before it all displayed compactly on the screen with room to spare.

    Font-size is larger with Dumb Console. I think it was like 18 a long time ago, but I set it to the mid-20's later.

    Dazakiwi38 wrote:
    Also is there a way to force word-wrap in a story region?


    http://www.w3schools.com/cssref/css3_pr_text-wrap.asp
    http://www.w3schools.com/cssref/css3_pr_word-wrap.asp

    Dazakiwi38 wrote:
    How tricky is it to set it up the way you want versus how it will look on other peoples display configurations?


    It's the real trick. The only way you'll know for sure is to check your file on other computers. I put mine on flash drive and run it on friend's computers often.

    I noticed you have two posts, but I can't read the first right now.
  • Thanks for the links. A good reference page, but is there a site that is suitable for dummies? lol

    It says for text-wrap "The text-wrap property is not supported in any of the major browsers."

    This is the syntax
    text-wrap: normal|none|unrestricted|suppress;
    So where does this go? I tried putting it in the storyregions and .passage and body sections of the stylesheet. But when i add a new item to the inventory its printing it over the border lines still.

    There is an example at the top that looks slightly different
    p.test {text-wrap:none;}
    Keeping the same stylesheet that you updated, i am also seeing when i click on a link it displays the new passage with everything to the left of the screen for a split second and then adjusts. So you get this jerky
    flicker everytime. Sometimes the storyregion for the target info is displayed on the bottom left of the screen depending on what passage is showing. I'll paste the stylesheet code I have so you can have a look. My screen res btw is 1920 x 1080.
    body {
    /* This affects the entire page. */

    color: #00FF00;
    background-color: black;
    width: 1200px;
    margin-top: 5%;
    margin-bottom: 5%;
    margin-right: 80px;
    margin-left: 80px;
    text-wrap:normal;
    }

    #sidebar {
    /* This affects the Sugarcane sidebar */

    /* Removes the Sugarcane sidebar */
    display: none;
    }

    #passages {
    /* This is a container for all passages displayed on the page */

    /* Removes the vertical line to the left of Sugarcane passages */
    border: 0;
    }

    .passage {
    /* This only affects passages */

    /* Passage width */
    width: 610px;

    /* Passage height */
    height: auto;

    /* Floated left due to StoryRegion */
    float: left;

    /* Border */
    border-style:solid;
    border-width:2px;
    text-wrap:normal;

    /* Padding between passage's border and inside text */
    padding-top:25px;
    padding-bottom:25px;
    padding-right:50px;
    padding-left:50px;

    /* Text formatting */
    color: #00FF00;
    font-size: 16px;
    font-family: "Lucida Console";

    /* Sets the passage background color */
    background-color: black;
    }

    .passage a {
    /* This affects passage links */

    color: #00FF00;
    text-decoration:underline;
    }

    .passage a:hover {
    /* This affects links while the cursor is over them */

    color: #00FF00;
    text-decoration:underline;
    }

    .storyRegion {
    /* This affects any passage with the tag, "StoryRegion" */

    float: left;
    width: 230px;
    background-color: black;
    padding: 1em;

    /* Text formatting */
    color: #00FF00;
    font-size: 16px;
    font-family: "Lucida Console";

    /* Border */
    border-style:solid;
    border-width:2px;
    text-wrap:normal;
    }

    input {
    background-color: black;
    border-style: solid;
    border-width: 1px;
    border-color: #00FF00;
    font-family: "Lucida Console";
    color:#00FF00;
    font-size: 16pt;
    padding-top:1px;
    padding-bottom: 0;
    padding-right: 0;
    padding-left: 0;
    }

    h1 {
    font-family: "Lucida Console";
    color:#00FF00;
    }

    ul {
    list-style-type: square;
    margin: 0;
    }

    hr {
    color: #00FF00;
    }

    .tab {
    text-indent:40px;
    margin:0px;
    }
  • Sorry this thread has become two different topics  :o

    Here is my nested IF statements that I have changed as per your suggestions.

    When I play through and I do cut the power, variable is set to true. It should be displaying the paragraph
    [quote]With the power cut it should help a great deal. But its not. Its still passing the first IF statement but ignoring the others.

    How do other people get around this, when you want to include some paragraphs in a passage if the player has made some earlier choices?


    You come to a halt on the top step and cover to your right, where beyond the guard rails over looking the stairs meets the eastside hallway before going up to the next stair landing.
    Next to you another man leans against the stair wall peeking around the corner to the left.
    "Clear."
    <<if $mackHealth is 0>>
    With a man down this early in the mission isn't a good sign. You don't know what awaits upstairs.
    You have to assume the gunshot that killed Mack was heard.
    This removes the element of surprise completely. Likely we will be walking into an ambush.

    Got to think of an [[alternative plan.|altplan]]

    Time is the essence and there is no way you could clear each level.
    <<elseif $powercut eq "true" and $radiotaken eq "no">>
    With the power cut it should give you the advantage to say undetected long as possible.
    Hopefully everyone is still asleep.

    <<elseif $powercut eq "true">>
    With the power cut it should help a great deal.

    <<elseif $basementpowerboxrigged eq"true" and $radiotaken eq "yes">>
    They already know we are here. This removes element of surprise completely. Likely we will be walking into an ambush. Got to think of an [[alternative plan.|altplan]]


    <<elseif $basementpowerboxrigged eq "false" and $radiotaken eq "no" and $powercut eq "false">>
    There was two plans in place at the beginning, nows the time to commit to one of them.

    [[Choose the plan|altplan]]


    <<elseif $basementpowerboxrigged eq "false" and $radiotaken eq "yes" and $powercut eq "false">>
    There was two plans in place at the beginning, nows the time to commit to one of them.

    [[Choose the plan|altplan]]
    <<endif>>








  • First of all, I had an epiphany. Your game is probably trying to put the box on the right because you're using old code in the StoryRegions module. There are a couple updates to that code, and you'll need both in 1.4+. So, copy that over to your game and that should fix the appearance. otherwise,, I've tested it on another computer and didn't have any of the issues you described, so I'm having trouble figuring out what's the deal there.

    Dazakiwi38 wrote:
    The passage im using multiple IF statements where I am having problems with is called "floor1A",

    These conditions are set in earlier passages of" Remotedet" or  "sabotage1".


    I checked again. In the file you posted, none of those passages are there and there is only one if branch: <<if $inv == "Radio">>
  • Those are some funny variable names, but certainly descriptive! ;D

    Unfortunately, the statement is pretty bewildering. So, there might be an issue somewhere in that jungle.

    When I have problems with an if branch I print all the variables right before the branch (above $mackHealth in this case). Then, I know if a variable is at fault, or if it's the conditional branch.

    However, if $mackHealth is 0, then that's the one that's going to hit no matter what is further down. It stops when if finds the first true stement.  (EDIT: Highlighted, because this is probably the case. Also, note that in 1.4+, variables that are not set are by default 0. So, if you haven't set Mack's health, it's 0.)

    Attached is a file with your if conditional branch. It seems to work, so the problem is probably in your variables. Use the file to help test your branch to make sure it's doing what you think it should.

  • I had previously uploaded my game file from my dropbox folder where I has been saving it.  So to be sure I saved it into a normal folder and have uploaded it again. So now you should see those passages I have mentioned Floor1A is the one with the IF statements in question.
    Link: https://www.mediafire.com/?4fxaif9q72f8i9d I haven't updated the storyregion Module, I am not sure if i already have the correct updated one, as before I did find a page that had some updated things to change. I believe it was adding some array variables.

    Here is what I have for the StoryRegion Module currently.

    History.prototype.realdisplay = History.prototype.display;
    History.prototype.display=function(D,B,A,C){
    var regions = tale.lookup("tags", "StoryRegion");
    if(regions){
    var title;
    for(var i in regions){
    title = regions[i].title.split(" ").join("");
    if(!$("story"+title)){
    var region = document.createElement('div');
    region.id = "story"+title;
    region.className = "storyRegion";
    document.getElementsByTagName('body')[0].appendChild(region);
    }
    if($("story"+title)){
    setPageElement("story"+title,regions[i].title,"");
    }
    }
    }
    state.realdisplay(D,B,A,C);
    }
    state.display('StoryRegions Module','','offscreen');
    The nest of IF statements in the passage we are talking about does already have an output of those variables being checked. And my variables are set in the beginning of the game, in a passage called Varinit. I actually thought if a variable wasn't set at all it would throw an error, so I have been setting them all at the start.
    Far as I can tell in my tests the variables have been correct.

    Hopefully now you can view the proper version of my game in progress, those funny variable names makes a little more sense  ;D i tried to make them descriptive enough so I can keep track of them as the game gets bigger.

    [quote]However, if $mackHealth is 0, then that's the one that's going to hit no matter what is further down. It stops when if finds the first true statement.

    Hmm..ok so what I am trying to do will not work then, so to be sure, it has to be only IF statement to be true and then it stops reading IF statements altogether in that passage? Why does it do that? 

    I want to be able to several output paragraphs in my game if certain conditions are met within one single passage. And these will be just different combinations of conditions.

    If only one IF is passed then it means more work, having to create a new passage for every IF statement, this will clutter the work space up even more. As for right now perhaps the work around is with having a few <<display 'passage'>> and each of those passages will have a different IF statement with an output if true.

    Surely it could be possible for the dev to make it so within one single passage each IF statement will be read regardless if they pass or fails. If there isn't then may I suggest this it for the next update? 

    This would be useful for games that are created to be dynamic with having alternative text or descriptions based on variables when a player returns to a passage/location, from descriptions of weather, time of day, npc conversations, other game world changes made by the player or scripted in, combat system.

    I know some macros exist with <<once>>  <<becomes>> etc but they are more restrictive and linear still. Handy if you do not want to repeat the beginning of a passage or whatever but that's it.

    Let me know if you now can see those passages I have mentioned, they are down the very bottom.

    Thanks for your help. Hopefully I will get all this sorted and move on ahead with my game. I am learning lots already.
  • Just a quick reply without reading all of that or looking at your game (I'm getting ready to eat, then step out), try putting a condition in front of all your others <<if $mackHealth gt 0 . . . >>. So, you'll add the condition "$mackHealth gt 0 and" after the "<<if" and in front of all the conditional statements other than the first one (<<if $mackHealth eq 0>>).

    In other words, "If Mack is dead, do this. If not, do one of these other things."

    That should take care of the issue, I'll wager.

    If not, and you really want all of the ifs to be read, then don't nest them.
    <<if $this>>Do this.<<endif>>
    <<if $that>>Do that.<<endif>>
    <<if $another>>Do something else.<<endif>>
    There were multiple updates to the SR code. Delete your SR passage and copy the one from Dumb Terminal.

    The if conditional branch system works as it is intended to work and as it should work. You're just doing it wrong. ;)


    PS: I did take a quick glance at the game and there are more issues with the way you've set up your conditional branch. Can't go into it now, but it's not going to work.
  • So how would it be done?

    Just for an example, for purposes of me and other new comers to twine can grasp what is the best way to to achieve having multiple conditions. The wiki seems to be lacking some information on this, well as far as my eyes can see.

    So for an example in the previous passage the player was given three choices to choose from..
    Choice A and Choice B and Choice C

    And we want to keeping with consistency track a variable that may or may not be true based on an earlier choice in the game but want to reflect that outcome if true in the same passage. We will call this Variable X.

    How would you in a single passage check if one of those choices are true and have an output for it, including the variable that makes 4 IF statements.  To make it a little more tricky, what if Choice A and Variable X both being true will also have a unique output. So that brings it to 5 IF statements. 

    How would  you guys go about it?

  • After looking at Javascript site http://www.w3schools.com/js/js_if_else.asp I realised I misread Sharpe's earlier example. The very last else is needed ;D. So I whipped up a demo to see if I had struck gold and indeed you can have multiple conditions. I feel like an idiot. Sometimes you do need to step away from a problem and come back with fresh eyes.

    So here is my demo I made, I thought i'd make it available for others like myself who have struggled with it.

    https://www.mediafire.com/?7gjmg2s60ixv23f

    Or just look at the code for the multiple conditions
    <<if $eat eq "true" and $ChoiceVeh eq "car">>
    You are hungry and decide to go out to eat.
    <<elseif $eat eq "true" and $ChoiceVeh eq "motorbike">>>>
    You are hungry and decide to go out to eat. You don your riding gear on and head to the furthest bakery that sells food worth eating.
    <<elseif $sleep eq "true">>
    You feel more tired than anything and choose to have sleep.
    <<elseif $gym eq "true" and $ChoiceVeh eq "motorbike">>
    You put on your riding gear and take your Yahama R1 for run to the gym using the longest route possible.
    <<elseif $gym eq "true" and $ChoiceVeh eq "car">>
    You say sorry to the motorbike as you back your car out of the garage, then head off to the gym.
    <<elseif $movies eq "true" and $ChoiceVeh eq "car">>
    You remembered its the last day to go and see that movie that had caught your interest called "The intergarlactic space battle finale".
    <<elseif $movies eq "true" and $ChoiceVeh eq "motorbike">>
    You remembered its the last day to go and see that movie that had caught your interest called "The intergarlactic space battle finale".
    The traffic at this hour will be heavy, so you opt to take the bike this time.
    <<else>>

    <<endif>>



    [[Start again|Start]]
    Goes to look for a rock to crawl under..
Sign In or Register to comment.