Using text field for searching thru books

Started by Bijulinus, Mon 17/09/2007 12:19:31

Previous topic - Next topic

Bijulinus

Hello.
I'd like to find some scripts to do the following thing:
A char finds a book. Instead of actually read it, when he interact or look it, a text-field appears and he can put some text. If that text appears in the book, the player finds it. Like searching in a dictionary.

Actually, I want the book to be formed by a Map-like, String-String association.
If the given String is contained in the book, please display the associated String.
Es:
"House" , "Where I live"
"Car", "I use it to go to work"
"TV", "It's annoying"

This book is about houses, cars and tvs :)

I hope what I want to do is clear. Any hints? What I cannot do yet is:
-creating the array of Strings;
-using the text-field.

Thanks a lot, everybody.
I'm too lazy at the moment to find a good signature.

Ashen

The simplest way would probably be to just use conditions, e.g.:
Code: ags

if (txtSearchBook.Text == "Car") player.Say("I use it to go to work");
else if (txtSearchBook.Text == "House") player.Say("Where I live");
 // etc


So, you wouldn't need an array of Strings. Of course, that could get a bit cluttered, it there were lots of thing you could look for. I'd use a struct to store the Search-Response Strings, e.g.:
Code: ags

struct StringMap {
  String Search;
  String Response;
};

StringMap BookText[20]; // Or however many entries you have. Crreates an array of BookText[0].Search, BookText[0].Response, BookText[1].Search, BookText[1].Response, etc.


Then you'll need to set the Strings somewhere (e.g. game_start):
Code: ags

BookText[0].Search = House;
BookText[0].Response = "Where I live";
BookText[1].Search = "Car";
BookText[1].Response = "I use it to go to work.";
//etc


And just use a while loop to search the options:
Code: ags

int TempI;
while (TempI > 20) { // or whatever the size of the array was
  if (txtBookSearch.Text == BookText[TempI].Search) { //  Or use String.CompareTo, to make it not case sensitive
    player.Say(BookText[TempI].Response);
    TempI = 21; //  Ends the loop
  }
  else TempI ++;
}
if (TempI == 20) player.Say("There's nothing here about that."); 
  // Because the loop takes it to 21, this only runs if it's gone through all options and found no match


Or, of course, you could use the text parser instead.

What problems are you having using the text field? What have you tried, and what's gone wrong?
I know what you're thinking ... Don't think that.

Bijulinus

Thanks! I think the first method will be enough.

What I dont know is where I put those ifs?

I believe I will put the text field in a GUI that pop ups when player interacts with the Book object.
The problem is: that code is dependent by the Book chosen, so where I put it?

I'm not sure: the best use of text field is checking textField.Text value, isnt it?

Thanks a lot again
I'm too lazy at the moment to find a good signature.

Ashen

Quote
What I dont know is where I put those ifs?
Pretty much depends on how you've set the GUI up. The obvious places are in the Activate function for the TextBox (which runs when the player presses enter), or the Click function for a button on the GUI if you'd rather do it that way. If you don't mind not being able to control the look of the GUI, you could use an InputBox.

Quote
The problem is: that code is dependent by the Book chosen, so where I put it?
What is the book - an Object or a Hotspot, or something? Turn the GUI on from the 'Interact with (whatever)' interaction for it.
Does this mean you have more than one book the player could be searching? If so, you'll need some way of telling which book is being searched (a variable check should be enough; you could use a different GUI for each book, but that could get a bit bulky), and more than one array (to store responses for the different books) if you end up using arrays.

Quote
I'm not sure: the best use of text field is checking textField.Text value, isnt it?
I don't understand this line. Look up the TextBox properties and see if that helps.
I know what you're thinking ... Don't think that.

Bijulinus

I got it.
When the player presses enter (Activate function) I check what was searched.
How do I get the content of the textfield? (I believe textfield.Text method)
The book is an object: maybe I can use Properties to specify which "content" that book will have, and then (if possible) the script check the property.
I'm too lazy at the moment to find a good signature.

Ashen

Properties seem a little pointless here - you'd have to get the Object property and store it in another variable that you check against the content of the book. Since you'd have to do that for every object individually, it'd probably be easier to ignore properties and just assign the value directly, ie.:
Code: ags

CurrentBook = 1;

//Instead of:
CurrentBook = oThisObj.GetProperty("Book");


Quote
How do I get the content of the textfield?
Still not entirely sure what you're asking, as it's right there in the manual: You get the TextBox text using (TextBoxName).Text - so if your TextBox is called 'textfield', then yes it'd be textfield.Text, but if it's called 'txtSearchBook', you'd use txtSearchBook.Text, etc ...
I know what you're thinking ... Don't think that.

SMF spam blocked by CleanTalk