Time to Randomize

Blue, sun, television. Just thought we'd start with a few random words. Remember what happens when Foxy talks to Dork? Here's the stimulating conversation that ensues when Foxy interacts with Dork:

Foxy: Hi! My name is Foxy.
Dork: Quack, quack, quack.
Foxy: Oh, I thought you could talk.
Dork: Quaaaaaack!
Foxy: I feel silly talking to a duck.
Dork: Thanks a lot!

The same conversation happens every time. It would be a lot more pleasurable if Dork said some random remark when Foxy spoke with him. The current conversation is too precious to get rid of though so what we should do is have the conversation take place the first time Foxy talks to Dork, but then have a random Dorky remark every time after that.

Let's start by making our current conversation happen only once. Open the global script by finding it in the File menu or by pressing Ctrl+Shift+G. Scroll all the way to the bottom and you should see the cDork_Talk() function that we previously added with our delightful conversation. Remember our good old friend the DoOnceOnly() function? No? Shame on you! Go back and read about it in Chapter 4. I mean it mister! Now, put all the code that is already in the cDork_Talk() function in a DoOnceOnly() block as follows:

if (Game.DoOnceOnly("FirstTalkWithDork") == true)
{
  <Stuff already there goes here>
}

Now if you run the game and talk to Dork he'll only respond once. Go and try it while we wait...

Now that Dork will only go through the whole conversation once, let's add some random remarks for him to say for every conversation after the first one. We're going to just show you the final function and then we'll go through and explain it. Make the cDork_Talk() function look like the following and notice that the new parts have a "new" comment next to them.

function cDork_Talk()
{
  int myRandomNumber; // new
  cFoxyMonk.Walk(cDork.x - 20, cDork.y, eBlock, eWalkableAreas); // new (moved)
  cFoxyMonk.FaceCharacter(cDork); // new (moved)
  if (Game.DoOnceOnly("FirstTalkWithDork") == true)
  {
    cFoxyMonk.Say("Hi!  My name is Foxy.");
    cDork.Say("Quack, quack, quack.");
    cFoxyMonk.Say("Oh, I thought you could talk.");
    cDork.Say("Quaaaaaack!");
    cFoxyMonk.Say("I feel silly talking to a duck.");
    cDork.Say("Thanks a lot!");
    cDork.Say("&1 Quack-quack-quack-quack quack-quack-quack-quack!");
  }
  else // this whole 'else' block is new
  {
    cFoxyMonk.Say("Hey there again.");
    myRandomNumber = Random(4);
    if(myRandomNumber == 0)
    {
      cDork.Say("Quack, quack and stuff.");
    }
    else if(myRandomNumber == 1)
    {
      cDork.Say("I'm a little duck and I like the water.");
    }
    else if(myRandomNumber == 2)
    {
      cDork.Say("I wonder if I can fly away from you.");
    }
    else if(myRandomNumber == 3)
    {
      cDork.Say("You're a fox and you have wings!!!???? WOW!!!.");
    }
    else if(myRandomNumber == 4)
    {
      cDork.Say("The planets will allign soon enough.");
    }
  }
}

In order to generate a random message every time Foxy interacts with Dork, the first thing we need to do is generate a random number. And when we generate this random number, we have to store it somewhere so we can use it later. That's where the first line comes into play.

int myRandomNumber;

This line declares a variable called myRandomNumber that is of the integer type. That means the variable can hold a number in it. Now, notice that we moved the Walk and FaceCharacter functions out of the if block as well. We did this because no matter what conversation will take place, Foxy will need to walk over to Dork and face him first.

Let's look at the else block now. If we've already gone through the first conversation once with Dork, then the DoOnceOnly() function will return false and the code in the else block will run. In this block three things will happen. First, Foxy will say hey again to Dork. Then, a random number between 0 and 4 inclusive will be generated by this line:

myRandomNumber = Random(4);

This will give us 5 possible values (0, 1, 2, 3, 4) that we can use. To get this random number we call the Random() function passing it a maximum. Since we passed it 4, we will not get a number greater than 4 back. Now notice what's on the left of the equal sign? That's right! It's our variable. The Random() function gives us back our random number and we have to store it somewhere or we will lose it. We put it into our variable myRandomNumber. I'm sure you know that the variable can be named anything you like, but our name is best, so THERE!!

Now that we have a random number between 0 and 4 inclusive stored in our variable, we do the third thing in the else block, and that is to check our random number and choose something for Dork to say based on it. For that we use an if/else if block. And that's it! Save, run, have fun.

Side Note: For more information on variables please see Appendix I.