Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: JJJAC55 on Sat 12/03/2005 15:44:55

Title: more problems with the dialog
Post by: JJJAC55 on Sat 12/03/2005 15:44:55
Well ok.  I successfully made a telephone booth thing using dialog and dialog_request.  Now im trying to make a shop.  I went to the global script, and I wrote down function dialog_request again, with the new variable x2.  It says 'variable dialog_request already defined.' please help me!

Heres my probably-horribly-messed-up script. HELP!

function dialog_request (int x1) {
  if (GetGlobalInt(1) >= 5) {
  NewRoomNPC(TROLLY, 1, 270, 140);
  MoveCharacter(TROLLY, 24, 140);
}
else {
  DisplaySpeech(ROGER, "I don't got no 5 bucks.");
}
}

function dialog_request (x2) {
  Display(K it works);
}
Title: Re: more problems with the dialog
Post by: Ashen on Sat 12/03/2005 16:11:24
You only need to create the function once. Try this:

function dialog_request (int param) {
  if (param == 1) {
    if (GetGlobalInt(1) >= 5) {
      NewRoomNPC(TROLLY, 1, 270, 140);
      MoveCharacter(TROLLY, 24, 140);
    }
    else {
      DisplaySpeech(ROGER, "I don't got no 5 bucks.");
    }
  }// end of param 1
  else if (param == 2) {
    Display("K it works");
  }// End of param 2
  else if (param == 3) {
     // Whatever
  }// End of param 3
  //And so on
}  //  End of dialog_request


And note, you'd put run-script 1 not run-script x1
Title: Re: more problems with the dialog
Post by: on Sun 13/03/2005 23:08:36
In case you didn't understand Ashen's code, let me explain.  In AGS, you only define functions once.  Any time you see a line like:

function myFunction(int myVar, string myStr) {

It is the beginning of a function definition.  The parameters (in this example, myVar (an int variable) and myStr (a string object)) are local to the function.  This means that you can not call the parameter outside of the function, i.e.

// main global script
function myFunction(int myVar, string myStr) {
  if (myVar == 5) {
    DisplaySpeech(GetPlayerCharacter(), myStr);
    return 1;
    }
  else return 0;
  }

function invalid() {
  if (myVar == 8) DisplaySpeech(GetPlayerCharacter(), myStr);
  else DisplaySpeech(GetPlayerCharacter(), " invalid ");
  }


Wouldn't work correctly.  myVar and myStr aren't declared globally (outside of any function), so if you try to test/save the game, you will get an error telling you they are undefined symbols.  As I said, they are local to the function, they cannot be called outside of the function they are created in.

Parameters are often used for testing, which as Ashen's code shows, is what you want to do.  The name of the parameter doesn't affect the way the function operates, but it can determine the outcome of running the function.  By testing the values of the parameter, you can determine which script you are trying to access (see Ashen's code).

If this doesn't help and/or you already knew this and I insulted you by writing this, I'm sorry.  I hope you understand functions in AGS better now.  Michael Rittenhouse