[SOLVED] Trying to randomize character response

Started by harmonicapress, Thu 22/10/2015 21:28:59

Previous topic - Next topic

harmonicapress

Hey guys, I thought I had the coding right to have my character respond with a few random responses each time you examined a hotspot, for some reason it gave me a parse error when I used the example I got from the dynamic help section, I removed the else option and got the game to run but now it skips my lead character (cBingo)'s initial sentence and fires all 3 "random" sentences in one string. here is the code as I have it, with the else's foolishly removed just because that's the only way i could get the game to run while preserving most of the body of my attempted code. I'm sure this is a quick fix but I couldn't find anything in the forum archive that helped. Thanks for your time and insight!
Code: ags

function hMemos_Look()
{
int ran=Random(2);
if (ran==0) 
cBingo.Say("There's a lot of memos here...Send Mendoza Case evidence to DA's office.");
cBingo.FaceCharacter(cLucy, eNoBlock);
cBingo.Say("Lucy, they sent Carlito Mendoza to the electric chair over six months ago.");
cLucy.Say("Guess I don't need that one any more.");
if (ran==1)
cBingo.Say("There's a lot of memos here...This one looks like a shopping list. Catfood, milk, eggs, saurkraut...saurkraut? Ewwww.");
cLucy.Say("Don't you judge me, ya loafy curmudgeon.");
if (ran==2)
cBingo.Say("There's a lot of memos here...This one says: 'Remember to belittle and insult Bingo at least once a day.'");
cLucy.Say("You really are a short, fat, worthless excuse for a detective. Thanks for the reminder."); 
cBingo.Say("Ouch.");
}

Cassiebsg

#1
You need to remember to group your code with { and }, other wise it's a mess.

Code: ags

function hMemos_Look()
{
  int ran=Random(2);
  if (ran==0)
  { // remember to open a bracket for a group of commands!
    cBingo.Say("There's a lot of memos here...Send Mendoza Case evidence to DA's office.");
    cBingo.FaceCharacter(cLucy, eNoBlock);
    cBingo.Say("Lucy, they sent Carlito Mendoza to the electric chair over six months ago.");
    cLucy.Say("Guess I don't need that one any more.");
  } // and close it again when finished this group.
  else if (ran==1)
  {
    cBingo.Say("There's a lot of memos here...This one looks like a shopping list. Catfood, milk, eggs, saurkraut...saurkraut? Ewwww.");
    cLucy.Say("Don't you judge me, ya loafy curmudgeon.");
  }
  else if (ran==2) // or just else, since it's the last possible option.
  {
    cBingo.Say("There's a lot of memos here...This one says: 'Remember to belittle and insult Bingo at least once a day.'");
    cLucy.Say("You really are a short, fat, worthless excuse for a detective. Thanks for the reminder.");
    cBingo.Say("Ouch.");
  }
}


I haven't tried the code, so not sure if there's something else wrong with it.
Hope it helps. :)
There are those who believe that life here began out there...

Crimson Wizard

#2
Quote from: Cassiebsg on Thu 22/10/2015 21:39:37
You need to remember to group your code with { and }, other wise it's a mess.

I would like to point out that word "mess" is not explaining the problem. It is not about mess, it is different logic.

For example:
Code: ags

if (ran==0) 
cBingo.Say("There's a lot of memos here...Send Mendoza Case evidence to DA's office.");
cBingo.FaceCharacter(cLucy, eNoBlock);
cBingo.Say("Lucy, they sent Carlito Mendoza to the electric chair over six months ago.");
cLucy.Say("Guess I don't need that one any more.");


This code has same effect as:
Code: ags

if (ran==0) 
{
    cBingo.Say("There's a lot of memos here...Send Mendoza Case evidence to DA's office.");
}

cBingo.FaceCharacter(cLucy, eNoBlock);
cBingo.Say("Lucy, they sent Carlito Mendoza to the electric chair over six months ago.");
cLucy.Say("Guess I don't need that one any more.");


In this case only first statement (line) will be tied to "if", and the rest won't. So the first line will be played only if condition matches, but the rest 3 will play always.

harmonicapress

SOLVED
Thanks to both of you. Cassiebsg, your code worked perfectly, and Thank you crimson wizard for explaining how the bracket links all following code to the if statement, really appreciate an answer with a lesson. Teach a man to fish and he wont post lame questions in the forum...or something like that:) cheers.

Snarky

Cassiebsg, I've taken the liberty to correct the indentation of your code, according to the principle to always indent inside a block (between the {} brackets). This way, the formatting matches the logic of the code, and what Crimson Wizard describes becomes immediately obvious.

Cassiebsg

Sorry about not explaining, but was in a hurry to go to bed.  (roll) Still, you do it so much better than I could have. (nod)

Snarky, that's okay, thanks. (nod)
I don't like indenting like that, but that's my personal way as I find it harder to read the block codes, but that's probably just me. (roll)
There are those who believe that life here began out there...

SMF spam blocked by CleanTalk