Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: henonchesser on Sat 01/08/2009 09:28:10

Title: If-Else Related Code
Post by: henonchesser on Sat 01/08/2009 09:28:10
Hi, I'm working on a small project its going smoothly, but I ran into a simple snag.

I think I know how this works but I can't seem to get it right.

I  want an object to respond one way (ie with a spoken reply from the player) the first time you interact with it, and a second way every time after the first.

For example a telephone that lets you pick it up once and talk to the person, but after that call it's dead.
Title: Re: If-Else Related Code
Post by: Gudforby on Sat 01/08/2009 09:37:39
I would make a global variable to help you out. Set the initial value to 0. The you can use the if-else code to check the value of the variable. If the value is 0 the player hasn't interacted with the phone yet, so the spoken replay will occur. You will then set the variable to let's say 1, and then the next time the player interacts with it, the script run the script where for example the phone is dead.
Title: Re: If-Else Related Code
Post by: henonchesser on Sat 01/08/2009 09:49:03
Haha, I feel really dumb when it turns out to be that simple. Thank you very much!

Wouldn't mind showing me some example code would you?
Title: Re: If-Else Related Code
Post by: Gudforby on Sat 01/08/2009 09:50:49
no problem :)

If it's one thing I've learned about scripting, it is that the solutions often are easier than you think.

Good luck with your game!
Title: Re: If-Else Related Code
Post by: henonchesser on Sat 01/08/2009 09:59:08
I'm having some trouble defining a global int. I think I'm figuring it out, but I won't dissuade help.
Title: Re: If-Else Related Code
Post by: henonchesser on Sat 01/08/2009 10:07:15
function ophone_Interact()
{
if (GetGlobalInt(phonecall) == 10) {

cEgo.Walk(537, 435, eBlock);
cEgo.Say("Hello?");
cEgo.Say("Who is this?");
cEgo.Say("Joe who...?");
cEgo.Say(".... I don't think I know a Joe Ma...");
cEgo.Say("....Ha...Ha... I see what you did there...");
SetGlobalInt(phonecall, 9);
}

if else  {    <-------------------------- THATS LINE 74
cEgo.Say("The phones tuckered out from that last call");

}

this here is my code.

Failed to save room room1.crm; details below
room1.asc(74): Error (line 74): expected '('
Title: Re: If-Else Related Code
Post by: henonchesser on Sat 01/08/2009 10:09:33
Ok I fixed it now. Still working the bugs out.
Title: Re: If-Else Related Code
Post by: henonchesser on Sat 01/08/2009 10:13:38
Ok so I have a new problem now. The while the variables initial value is 10. The game doesn't recognize this. So when I got to use the phone it uses the ELSE statement command lines.

Whats up?
Title: Re: If-Else Related Code
Post by: henonchesser on Sat 01/08/2009 10:19:47
Well I came up with a band-aid solution that resests itself everytime you enter this room....

function room_AfterFadeIn()
{
cEgo.Say("Room 1! How fitting!");

if (GetGlobalInt(phonecall) == 10) {
}

else {
  SetGlobalInt(phonecall, 10);
}
}

Sorry to keep bumping my own thread, but no ones really on to night... so I don't think it will bother anyone. Just wanted to keep you up to date.
Title: Re: If-Else Related Code
Post by: Khris on Sat 01/08/2009 10:51:19
It's almost noon here. Please don't double/ triple post, this isn't a chat room. Have some patience.

Using Get/SetGlobalInt is obsolete; you can either create a global variable via the pane by the same name in the editor tree or declare it right above the function.

bool phone_used;  // initially false

function ophone_Interact() {

  if (phone_used) {
    cEgo.Say("The phones tuckered out from that last call");
    return; // exit the function
  }

  cEgo.Walk(537, 435, eBlock);
  cEgo.Say("Hello?");
  cEgo.Say("Who is this?");
  cEgo.Say("Joe who...?");
  cEgo.Say(".... I don't think I know a Joe Ma...");
  cEgo.Say("....Ha...Ha... I see what you did there...");

  phone_used = true;  // set bool to true
}


There's a special function though specifically for this purpose.

function ophone_Interact() {

  if (Game.DoOnceOnly("phone call")) {

    // phone call

  }
  else cEgo.Say("The phones tuckered out from that last call");
}
Title: Re: If-Else Related Code
Post by: henonchesser on Sat 01/08/2009 11:12:32
Wow thank you. Thats really easy. Your the best!
Title: Re: If-Else Related Code
Post by: Gudforby on Sat 01/08/2009 12:03:20
Like I said, the solutions are often easier than you think.
Even easier than I thought!
Title: Re: If-Else Related Code
Post by: tzachs on Sat 01/08/2009 14:07:14
Also, if you want more than two responses you can use the MultiResponse module (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=27947.0) by SSH.