How to code if statements followed by a a statement that happens regardless of previous result

So I have this code and it's giving me trouble and I knew the problems simple but can't figure it out
Here is the code

// First part.
msg ("Welcome to the world! There are a few questions you need to answer before entering. Please type your name.")
// Second part.
get input {
  if (Trim(result) = "") {
    // If the player didn't enter any valid characters, print an error message and call this function again.
    msg ("Invalid input.  Please try again.")
    get input {
    }
  }
  else {
    player.custom_name = Trim(result)
    msg ("You name is now " + (player.custom_name))
  }
}
// third part.
options = Split("Male;Female", ";")
ShowMenu ("What is your Gender?", options, false) {
  switch (result) {
    case ("Male") {
      msg ("So you are a man.")
      player.sex = "male"
    }
    case ("Female") {
      msg ("So you are Female.")
      player.sex = "female"
    }
  }

The problem is the way I have it coded it does do part one but then it skips to part three instead of doing part two and then going to part three, how do I arrange this?


The problem is that you have nested the 'Third part' incorrectly, it should be inside the 'Second Part'.

// First part.
msg ("Welcome to the world! There are a few questions you need to answer before entering. Please type your name.")
// Second part.
get input {
  if (Trim(result) = "") {
    // If the player didn't enter any valid characters, print an error message and call this function again.
    msg ("Invalid input.  Please try again.")
    get input {
    }
  }
  else {
    player.custom_name = Trim(result)
    msg ("You name is now " + (player.custom_name))
    // third part.
    options = Split("Male;Female", ";")
    ShowMenu ("What is your Gender?", options, false) {
      switch (result) {
        case ("Male") {
          msg ("So you are a man.")
          player.sex = "male"
        }
        case ("Female") {
          msg ("So you are Female.")
          player.sex = "female"
        }
      }
    }
  }
}

I would make the 2nd part a function, so that if the input continues to be invalid, that function can be called again ad infinitum.


I second that.


Thank you Agon!

Second question, since the name being entered is the result you ultimately want, you put the third in the same place as the second but lets say the first part and second part each had an outcome you want, and are separated by an else if statement, but the third part would occur regardless of the result of either, how would I code that?


think of like an outline:

I. (1)
-> A. (2A)
->-> 1. (3A)
-> B. (3B)
II. (2B)

'A' (2A) and 'II' (2B) are occurring and nearly the same time

'1' (3A) and 'B' (3B) are occurrding nearly at the same time

I. -> A.
I. -> II.
"+"
A. -> 1.
A. -> B.
=
I. -> A. -> 1.
I. -> A. -> B.
I. -> II.

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

'I.' is done first (1st clock-pulse-interval: 1 step/operation only)

'A.' and 'II.' are done next at nearly the same time (2nd clock-pulse-interval: 2 steps/operations)

'1.' and 'B.' are done next at nearly the same time (3rd clock-pulse-interval: 2 steps/operations)

a "straight path" is the simplest 'order of operations':

1
-> 2
->-> 3
->->-> 4
->->->-> 5

now, lets look at this:

1
2
3
4
5

while this seems like it is also a "straight path", it probably /usually is... but it may not be... as '1' can take a long time to finish, while '2' starts and finishes quickly (before '1' can finish). The '2' starts immediately after the '1' starts.


whereas, back with this:

1
-> 2
->-> 3
->->-> 4
->->->-> 5

the '1' has to take a much longer time, for '2' to finish before it does, so it's much more UN-likely for '2' to finish/"happen" before '1' finishes/"happens"


Dcoder's advice to create functions would be the best method.


Create a function named GetName

Paste this into the script in code view:

msg ("<br/>Please type your name.")
get input {
  if (Trim(result) = "") {
    // If the player didn't enter any valid characters, print an error message and call this function again.
    msg ("Invalid input.  Please try again.")
    GetName
  }
  else {
    game.pov.custom_name = Trim(result)
    msg ("Your name is now " + (game.pov.custom_name))
    // third part.
    // Call GetGender to collect the player's gender.
    GetGender
  }
}

Create a function named GetGender

Paste this into the script in code view:

options = Split("Male;Female", ";")
ShowMenu ("What is your Gender?", options, false) {
  switch (result) {
    case ("Male") {
      msg ("So you are a man.")
      game.pov.sex = "male"
      // Move the player to the first room.
      MoveObject (game.pov, room)
    }
    case ("Female") {
      msg ("So you are Female.")
      game.pov.sex = "female"
      // Move the player to the first room.
      MoveObject (game.pov, room)
    }
    default {
      // This should never happen, but the function will run again in case there is no match.
      msg ("Something went wrong.  Restarting...<br/><br/>Welcome to the world! There are a few questions you need to answer before entering. Please type your name.")
      GetGender
    }
  }
}

To avoid worrying with your initial room's description:

Create a room called Welcome, and start the player in that room.

For the before entering script, put this to keep the room description from printing:

// Keep the fake room's description from printing.
game.autodescription = false

For the after entering script, put this to call the scripts:

msg ("Welcome to the world! There are a few questions you need to answer before entering.")
GetName

For the after exiting script, put this to turn the room descriptions back on:

// Turn auto descriptions back on now that we have bypassed it for this room
game.autodescription = true

That should get you going.

The GetGender script will move the player to room once everything has been set up.


Entire example game:

<!--Saved by Quest 5.7.6606.27193-->
<asl version="550">
  <include ref="English.aslx" />
  <include ref="Core.aslx" />
  <game name="Get the Information, Tonto!">
    <gameid>ae3be2c2-6f07-43c1-80f5-015a65c103a9</gameid>
    <version>1.0</version>
    <firstpublished>2018</firstpublished>
  </game>
  <object name="room">
    <inherit name="editor_room" />
    <isroom />
    <beforeenter type="script">
    </beforeenter>
  </object>
  <object name="Welcome">
    <inherit name="editor_room" />
    <usedefaultprefix type="boolean">false</usedefaultprefix>
    <alias>Welcome to the world!</alias>
    <enter type="script">
      msg ("Welcome to the world! There are a few questions you need to answer before entering.")
      GetName
    </enter>
    <beforeenter type="script">
      // Keep the fake room's description from printing.
      game.autodescription = false
    </beforeenter>
    <onexit type="script">
      // Turn auto descriptions back on now that we have bypassed it for this room
      game.autodescription = true
    </onexit>
    <description type="script">
    </description>
    <object name="player">
      <inherit name="editor_object" />
      <inherit name="editor_player" />
    </object>
  </object>
  <function name="GetName"><![CDATA[
    msg ("<br/>Please type your name.")
    get input {
      if (Trim(result) = "") {
        // If the player didn't enter any valid characters, print an error message and call this function again.
        msg ("Invalid input.  Please try again.")
        GetName
      }
      else {
        game.pov.custom_name = Trim(result)
        msg ("Your name is now " + (game.pov.custom_name))
        // third part.
        // Call GetGender to collect the player's gender.
        GetGender
      }
    }
  ]]></function>
  <function name="GetGender"><![CDATA[
    options = Split("Male;Female", ";")
    ShowMenu ("What is your Gender?", options, false) {
      switch (result) {
        case ("Male") {
          msg ("So you are a man.")
          game.pov.sex = "male"
          // Move the player to the first room.
          MoveObject (game.pov, room)
        }
        case ("Female") {
          msg ("So you are Female.")
          game.pov.sex = "female"
          // Move the player to the first room.
          MoveObject (game.pov, room)
        }
        default {
          // This should never happen, but the function will run again in case there is no match.
          msg ("Something went wrong.  Restarting...<br/><br/>Welcome to the world! There are a few questions you need to answer before entering. Please type your name.")
          GetGender
        }
      }
    }
  ]]></function>
</asl>
I CAN POST THIS HERE.

here's a test for you to run, to see how the 'order of operations' works:

(take a guess at the order of numbers that will be displayed, will they be in "contigious" order (the 'ifs' make this impossible without even dealing with what will be the actual order of operations that occurs): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, or not?, and then run this example, and see if you guessed right or not)

<game name="example_game">

  <attr name="integer_attribute_1" type="int">0</attr>
  <attr name="integer_attribute_2" type="int">0</attr>
  <attr name="integer_attribute_3" type="int">0</attr>

  <attr name="start" type="script">

    msg ("0")

    game.integer_attribute_1 = GetRandomInt (10,11)

    if (game.integer_attribute_1 = 10) {

      msg ("1")

      game.integer_attribute_2 = GetRandomInt (20,21)

      if (game.integer_attribute_2 = 20) {

        msg ("2")

      } else if (game.integer_attribute_2 = 21) {

        msg ("3")

      }

      msg ("4")
  
    } else if (game.integer_attribute_1 = 11) {

      msg ("5")

      game.integer_attribute_3 = GetRandomInt (30,31)

      if (game.integer_attribute_3 = 30) {

        msg ("6")

      } else if (game.integer_attribute_3 = 31) {

        msg ("7")

      }

      msg ("8")

    }

    msg ("9")

  </attr>

</game>

the 'on ready' ( http://docs.textadventures.co.uk/quest/scripts/on_ready.html )sometimes works... but sometimes it doesn't work too, so you can give it a try (put the script that you want to wait, inside of the 'on ready', and have the 'on ready be after/below the script that you want to finish first, before the script within the 'on ready' is run/started/executed/activated


Log in to post a reply.

Support

Forums