If-Else Related Code

Started by henonchesser, Sat 01/08/2009 09:28:10

Previous topic - Next topic

henonchesser

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.

Gudforby

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.

henonchesser

#2
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?

Gudforby

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!

henonchesser

I'm having some trouble defining a global int. I think I'm figuring it out, but I won't dissuade help.

henonchesser

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 '('

henonchesser

Ok I fixed it now. Still working the bugs out.

henonchesser

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?

henonchesser

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.

Khris

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.

Code: ags
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.

Code: ags
function ophone_Interact() {

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

    // phone call

  }
  else cEgo.Say("The phones tuckered out from that last call");
}

henonchesser

Wow thank you. Thats really easy. Your the best!

Gudforby

Like I said, the solutions are often easier than you think.
Even easier than I thought!

tzachs

Also, if you want more than two responses you can use the MultiResponse module by SSH.

SMF spam blocked by CleanTalk