Nesting If Statements (SOLVED... sort of)

Hey y'all. :) Having some difficulty with nesting simple If statements, and I wasn't sure what the issue was. Here is my example (gamebook, offline):

  if (HasSeenPage(Page2) && HasSeenPage(Page3)) {
    msg ("hello")
  }

From what I can tell, this will exclusively print the message "hello" if, and only if, the player has seen Page2 and Page3. It does not care if the Player sees one or the other, or neither, only both.

The other option (really prefered for what I am doing):

if (HasSeenPage(Page2)) {
  	if (HasSeenPage(Page3)) {
    msg ("hello")
  }
  msg ("goodbye")
  }

This will print "hello" both are true, but print "goodbye" if only the first is true, correct? I'm not very comfortable with nesting as looking at layers of code easily confuses me, so if someone could correct this then I'd really appreciate it.

Thanks!


Okay, so it seems that the letter has, all of a sudden, decided to work. I'd put it down to a nesting error that I corrected when pasting it onto the forum! The former, however, doesn't work at all. I suppose the latter can be used to achieve the same purpose as the former, but I would still appreciate any input as to what is wrong with it.


I've not tried && in Quest before; but I know and works.


Okay, well I tried this:

if (HasSeenPage(Page2)) and (HasSeenPage(Page3)) {
        msg ("abc")
      }

And it was compeltely wrong. Tried to call the function "and". Then I did:

if (HasSeenPage(Page2)) {
        and (HasSeenPage(Page3)) {
          msg ("abc")
        }

With similar results. Finally:

 if (HasSeenPage(Page3) and HasSeenPage(Page4)) {
        // ABC
      }

None of these seemed in any way successful. I'm kind of stuck on this. The only alternative involves an unhealthy amount of nesting.


The first two are wrong.

The third one is correct, and should work. I assume that you've got some code other than // ABC in your actual game, so that you can see if it's working?

If it isn't working, then something must be wrong. It might be worth adding some debugging statements, so that you can see why it isn't behaving as expected.

Something like:

msg ("debug - HasSeenPage(Page3) is: "+HasSeenPage(Page3))
msg ("debug - HasSeenPage(Page4) is: "+HasSeenPage(Page4))
if (HasSeenPage(Page3) and HasSeenPage(Page4)) {
  msg ("debug - 'and' is true")
  // ABC
}

I tried running it, returned that the pages had never been visited, although they obviously had been. So I decided to reinstall Quest, and then run the exact same stuff, and it worked totally fine.

I've had this issue before, when creating page links, though, in that the only solution seems to be to reinstall Quest. I'm not sure why on earth this occurs, but at least it is fixed.

Thanks!


Not sure if you want to/need to know this, but "or" adds a lot more options, too. Sorry if this isn't what you're looking for, but just throwing this out there:

if 
(HasSeenPage(Page1) 
and (HasSeenPage(Page2) 
or (HasSeenPage(Page3))
and (HasSeenPage(Page4) 
or (HasSeenPage(Page5)))

msg ("this thing happens")

And then you have to iterate that for all combinatorial options with "else" and "else if" and "otherwise" and such.

I've been using this in gamebook mode (sorry again if this is no help) to handle all kinds of pages the player might or might not have visited. It actually can be quite robust and specific if you mind your parentheses. :)


Boolean Logic Operators / Truth Tables:

Definition (the "duh" / obvious) Logic Operator:

true ---> TRUE
false ---> FALSE

'not' (Negation: Opposite) Logic Operator:

not true ---> FALSE
not false ---> TRUE

'and' Logic Operator:

true and true ---> TRUE
true and false ---> FALSE
false and true ---> FALSE
false and false ---> FALSE

'or' Logic Operator:

true or true ---> TRUE
true or false ---> TRUE
false or true ---> TRUE
false or false ---> FALSE

// -----------------------------------------------------

human language conception/usage:

----------------------------------------------------

'and' logic:

if (clean room and mow lawn) {
  you get $5
}

if you don't clean room nor mow the lawn, do you get $5? NO!

if you clean room but you don't mow the lawn, do you get $5? NO!

if you mow the lawn but you don't clean your room, do you get $5? NO!

if you mow the lawn and you clean your room, do you get $5? YES!

// ----------------------------------------

'or' logic:

if (clean room or mow lawn) {
  you get $5
}

if you don't clean room nor mow the lawn, do you get $5? NO!

if you clean room but you don't mow the lawn, do you get $5? YES!

if you mow the lawn but you don't clean your room, do you get $5? YES!

if you mow the lawn and you clean your room, do you get $5? YES!

// ---------------------------------

as quest programming/scripting/coding language:

// -----------------------------------------

// 'and' logic:

stringlist_variable = Split ("true;false", ";")

player.mow_lawn = ToBoolean (StringListItem (stringlist_variable, GetRandomInt (0,1)))
player.clean_room = ToBoolean (StringListItem (stringlist_variable, GetRandomInt (0,1)))

if (player.mow_lawn and player.clean_room) {
  player.currency = player.currency + 5
}

// ------------------------------------------------

// 'or' logic:

stringlist_variable = Split ("true;false", ";")

player.mow_lawn = ToBoolean (StringListItem (stringlist_variable, GetRandomInt (0,1)))
player.clean_room = ToBoolean (StringListItem (stringlist_variable, GetRandomInt (0,1)))

if (player.mow_lawn or player.clean_room) {
  player.currency = player.currency + 5
}

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums