Multiple conditions and else/if: Testing my understanding.

Started by ElaineMc, Sat 15/05/2004 18:14:30

Previous topic - Next topic

ElaineMc

It's official: I really have to start learning how to script properly. I have a general idea of how text scripting works, in terms of else/if, or expressions, and multiple conditions [thanks to the tutorial]. What I want to check is to see if I have the right idea in terms of combining these elements.

Example situation: Mary needs a book. John knows that Sue has the book. Sue is willing to sell it to Mary, but not give it to her.

If Mary talks to Sue before she's talked to John, she doesn't know Sue has the book; and Sue doesn't know Mary needs it.

If she talks to Sue after talking to John, but before having the funds, Sue tells her she has the book Mary wants, but won't give it to her for free.

Mary has to talk to John, and must have money, before she can buy the book from Sue.

Okay, if I understand how this works, my code would have to check for multiple conditions, as well as use else/if to decide what happens under a given set of circumstances. What I'm not sure is quite how I'd set it up.Ã,  Would it be something like this?

Global Integers: TalkToJohn, set to 1 after talking to John, set to 0 before.
Inventory: The sum of money is the first item in her inventory, making it player.inv[1].
Dialog topics:
1 - Standard greeting, since Mary doesn't know Sue has the book.
2 - Mary knows, but doesn't have the money. Sue tells her how much the book is.
3 - Mary knows, *and* has the money.

My code would have to checkfor multiple conditions:
if ((player.inv[1] == 1) && (GetGlobalInt(TalkToJohn) == 1)) {Ã,  Ã, 
Ã,  Ã, DisplaySpeech(MARY, "Sure, I'll sell it to you-- here you go. That'll be fifty Reality Dollars, please!");
Ã,  }

However, since I have three possible outcomes, I have to check for three possible combinations, correct? That would mean using else/if. So then, I might have:
if ((player.inv[1] == 0) && (GetGlobalInt(TalkToJohn) == 0)) {Ã,  Ã, 
Ã,  Ã, DisplaySpeech(MARY, "Hi, there.");
Ã,  }

else if ((player.inv[1] == 0) && (GetGlobalInt(TalkToJohn) == 1)) {Ã,  Ã, 
Ã,  Ã, DisplaySpeech(MARY, "Sure, I have the book, but it's going to cost you fifty bucks.");
Ã,  }

else if ((player.inv[1] == 1) && (GetGlobalInt(TalkToJohn) == 1)) {Ã,  Ã, 
Ã,  Ã, DisplaySpeech(MARY, "Sure, I have it, and I'm willing to sell. That'll be fifty Reality Dollars, please!");
Ã,  }

Am I on the right track here? Do I at least have the correct general idea? If not, where am I going wrong?
1. Never make unnecessary noise.
2. Never use unnecessary light.
3. Never expect a rescue.
Outrider - Zombies exist. Deal with them.

InCreator

I really don't understand what you mean by all that, but I think that more nesting would give better ovelook, something like that:

if ((player.inv[1] == 0)
{
if (GetGlobalInt(TalkToJohn) == 1))
Ã,  DisplaySpeech(MARY, "Sure, I have the book, but it's going to cost you fifty bucks.");
else
Ã,  DisplaySpeech(MARY, "Hi, there.");
}
else
{
if (GetGlobalInt(TalkToJohn) == 1))

... and so on

Note that you don't have to use brackets if if condition leads to only one thing/function/whatever to accomplish

Ginny

Yes, though I still like to use brackets just for consistency. Anyway, nesting is the way to go, but, in order to check wether Mary has the money or not, you don't use player.inv[1] == 1, as far as I remember.
So, if I suppose that the inventory number for the money (not the place in the characters actual inventory) is 2, use character[MARY].inv[2] == 1.
Also, you can't use GetGlobalInt(TalkToJohn) just like that, you need to either use a global variable and export and then import it, or you need to use GetGlobalInt(4), or whatever number you choose it to be. But the best way would be, imo, if you don't want to use int TalkToJohn; or something, is to put at the top of the global script (I think; not sure exactly, and can't test on account of AGS not working): #define TalkToJohn 4
and keep a note that you're using global int 4. That way, you can do this:

//talking to Sue
if (GetGlobalInt(TalkToJohn) == 1) {
if (character[MARY].inv[2] == 1) {
DisplaySpeech(SUE, "Here you go, take the book.");
}
else { //Don't have money
DisplaySpeech(SUE, "I have the book, but it costs 50 dollars.");
}
}
else { //Haven't talked to John
DisplaySpeech(SUE, "Hi there.");
}

However, if you want other dialogs available with both of them, maybe it's best to use dialogs rather than displaying speech. It'll probably be simpler that way too. :)

P.s. Btw, Elaine, my Icq doesn't work atm, in fact hardly anything on mypc works, but after i reinstall, tell me at what stage you are with the game, if it's time for puzzles yet. If you need help with text scripting also, I can help, I hope ;).
Try Not to Breathe - coming sooner or later!

We may have years, we may have hours, but sooner or later, we push up flowers. - Membrillo, Grim Fandango coroner

SMF spam blocked by CleanTalk