Problems with If Statements...

Started by Joseph DiPerla, Sat 23/12/2006 20:51:57

Previous topic - Next topic

Joseph DiPerla

This is my code:

int Eat_Cereal = 0;

if (Eat_Cereal == 0) {
oCereal.Graphic = 69;
cHomer.Say ("UHHHMMMM. Haaaiiirrryyyy...");
cHomer.Say ("***DROOL***");
Eat_Cereal++ ;
}
if (Eat_Cereal == 1) {
  cHomer.Say ("Ohhh. The Bowl is Empty!");
  }


I want Homer to eat the Cereal, and when he eats it, it basically empties the bowl. So the next time he tries to eat it, it has a different interaction on it. But this script above doesn't work. It keeps Doing everything, no matter what.

Anything visibly wrong with this code?
Joseph DiPerla--- http://www.adventurestockpile.com
Play my Star Wars MMORPG: http://sw-bfs.com
See my Fiverr page for translation and other services: https://www.fiverr.com/josephdiperla
Google Plus Adventure Community: https://plus.google.com/communities/116504865864458899575

R4L

Where did you put the code? And are you declaring that int inside or outside of a function?

auriond

I had a similar problem too, and I solved it by declaring the second function as "else if".

In other words:
Code: ags

if (Eat_Cereal == 0) {
oCereal.Graphic = 69;
cHomer.Say ("UHHHMMMM. Haaaiiirrryyyy...");
cHomer.Say ("***DROOL***");
Eat_Cereal++ ;
}
else if (Eat_Cereal == 1) {
  cHomer.Say ("Ohhh. The Bowl is Empty!");
  }


Not sure if that's the problem you're facing though...

Joseph DiPerla

The else if didn't work either. It just keeps displaying the first message and not the second one instead.

What I did was, in the interaction editor for the object, I did a conditional where if the player character was Homer, it would run a script. Thats where I put the script.

Thanks anyway. Any other suggestions?
Joseph DiPerla--- http://www.adventurestockpile.com
Play my Star Wars MMORPG: http://sw-bfs.com
See my Fiverr page for translation and other services: https://www.fiverr.com/josephdiperla
Google Plus Adventure Community: https://plus.google.com/communities/116504865864458899575

monkey0506

#4
I see no visible problems with the code (aside from the use of dual ifs instead of if/else-if). See...the problem with the original code is that you update the Eat_Cereal variable in the first statement so by the time the second is run Eat_Cereal is already set to 1, thus both statements would be run the first time, and the second every consecutive time.

You said at first that he was doing everything, but now you say it's only running the first message...but yet you act as if nothing has changed...What I'm envisioning is this...

You've declared the Eat_Cereal variable within your conditional function haven't you? i.e., the line such as:

Code: ags
int Eat_Cereal = 0;


Is actually inside the function, am I right? This would mean the variable is destroyed at the end of the function, and recreated (and therefore reinitialized) at the start of the function.

This would explain the "it keeps doing everything" status of the if-if structure, and the "it just keeps displaying the first" status of the if-else structure.

So basically you just need to move the variable declaration outside the function.

If you've declared the variable outside the function feel free to correct me though.

SSH

Sorry to keep pimping my modules, by they were made to solve these kind of problems. Import MultiResponse.

Code: ags

int state=Multi.Say(cHomer, "UHHHMMMM. Haaaiiirrryyyy...>Ohhh. The Bowl is Empty!");
if (state==0) {
  oCereal.Graphic = 69;
  cHomer.Say ("***DROOL***");
}
12

Joseph DiPerla

Thanks SSH, but Monkey's workaround worked great! Thanks guys.
Joseph DiPerla--- http://www.adventurestockpile.com
Play my Star Wars MMORPG: http://sw-bfs.com
See my Fiverr page for translation and other services: https://www.fiverr.com/josephdiperla
Google Plus Adventure Community: https://plus.google.com/communities/116504865864458899575

SMF spam blocked by CleanTalk