Checking what the user has entered is done with a combination of if statements and Said() kernel calls. The Said kernel takes a said string as it's parameter, and returns true if it matches what the user has entered, false if not.
Scroll down to the handleEvent() method in the RoomScript instance. There, you will find this bit of code.
(if(Said('look'))
(Print {You are in an empty room})
)
This is a simple Said call which checks if the player has entered the text "look". If they have, it prints "You are in an empty room".
The Said call checks the words specified in the vocabulary resource. With this resource, you can have multiple words meaning the same thing. Though our code says "look", because of the vocabulary resource, the player typing "examine" will work as well.
Said String OperatorsSaid strings can use a number of operators to give you greater control over parsing what the user typed. There are six different operators which can be used: /, ,, <, ( ), [ ], and >.
Operator #1: The Word Separator ( / )
The word separator is the most commonly used of the said string operators. It allows you to check multiple words from the sentence the player types.
(if (Said 'open/door')
(theDoor open:)
)
This checks to see if the user entered "open door". If they did, it opens the door using the door class (discussed in Chapter 22).
(if (Said 'look/at/tree')
(Print {The tree is bright green!})
)
This checks to see if the user entered "look at tree". If they did, it prints "The tree is bright green!"
There can only be a maximum of three parts to a sentence, so you will never use more than two word separator operators.
Operator #2: OR ( , )
The OR operator allows you to indicate alternative words when checking the player's input.
(if (Said 'open,pull / door')
(theDoor open:)
)
This checks to see if the user entered "open door" or "pull door". It accepts either. If they did, it opens the door using the door class.
You can put as many OR operators in your saids as you like. Just remember that in many cases, you'd probably be better off adding the words to the same group in the vocabulary resource.
Operator #3: Semantic Reference ( < )
The semantic reference operator. This is similar to the word separator, but can be used in grouping brackets (explained next).
(if (Said 'look<at / tree')
(Print {The tree is bright green!})
)
This checks to see if the user entered "look at tree". If they did, it prints "The tree is bright green!"
The example above works, but is generally not used. Semantic reference operators are generally used with grouping brackets and the no claim operator.
Operator #4: Grouping Brackets ( ( ) )
The grouping brackets allow you to extend the or and semantic reference operators with more complex expressions.
(if (Said '(turn<on),start / machine')
(Print {O.K.})
)
This checks to see if the user entered "turn on machine" or "start machine". It accepts either. If they did, it prints "O.K.".
(if (Said '(check<out),(look<at) / tree')
(Print {The tree is bright green!})
)
This checks to see if the user entered "look at tree" or "check out tree". It accepts either. If they did, it prints "The tree is bright green!"
You can see here just how useful grouping brackets are, and full use of the semantic reference operator.
Operator #5: Optional Grouping Brackets ( [ ] )
The optional grouping brackets allow you to catch words in the said string which are valid, but not needed.
(if (Said 'climb [< up,on,onto] / tree')
(Print {O.K.})
)
This checks to see if the user entered "climb tree" or "climb up tree" or "climb on tree" or "climb onto tree". It accepts either. If they did, it prints "O.K.".
Operator #6: No Claim ( > )
The no claim operator allows you to check part of a string, then the next part. This is very useful so that you don't need to do if saids for "look this" and "look that". You can simply do an if said for "look>", then if saids for "this" and "that".
(if (Said 'look>')
(if (Said '/door')
(Print {The door is blue!})
else
(if (Said '/tree')
(Print {The tree is bright green!})
else
(if (Said '[ /* , !* ]')
(Print {You are in an empty room.})
)
)
)
)
; Cond version
(if (Said 'look>')
(cond
((Said '/door')
(Print {The door is blue!})
)
((Said '/tree')
(Print {The tree is bright green!})
)
((Said '[ /* , !* ]')
(Print {You are in an empty room.})
)
)
)
This checks to see if the user entered "look". If they did, it checks if they entered "look door". If they did, it prints "The door is blue!". Otherwise, it checks if they entered "look tree". If they did, it prints "The tree is bright green!". Otherwise, it checks if they typed "look <anything>" or just "look". If they did, it it prints "You are in an empty room". The last if said is very important. If it is not there, the phrase won't be claimed, and the player will get message stating that the game doesn't understand it.
Qualifying adjectives:(if (Said 'take/key<gold')
(Print {Ahh! The gold key!})
)
(if (Said 'take/key<silver')
(Print {Ahh! The silver key!})
)
(if (Said 'take/key')
(Print {Which key do you mean, the silver or the gold key?})
)
This example forces the user to use the correct adjective in order to pick up a key. If the user does not, it falls through to the last rule which tells the user that they must be more specific.
Example of the preposition 'in', which is classified in the vocab as a preposition (in the template game):
; Responds to user input of: 'put key in lock'
(if (Said 'put<in/key/lock')
(Print {You put the key in the lock})
)
Example of the preposition 'with', but has an unclassified word class in the template game:
; Responds to user input of: 'open door with key'
(if (Said 'open/door/key<with')
(Print {You open the door with the key})
)
Don't despair with this discrepancy. By setting 'with' to a preposition it will behave just like the 'in' preposition example.
These examples were tested against the template game with minor modifications (several word additions, as well as making the word 'of' a noun).
User Input | | Parts of Speech | | Said string syntax | | Notes |
look | | verb | | look | | will match any input starting with 'look' ('look', 'look around', 'look at troll', etc) |
jump | | verb | | jump | | will match any input starting with 'jump' |
take a breath | | verb/noun | | take/breath | | Don't include articles in said strings ֠also responds to 'take breath' |
eat the goodies | | verb/noun | | eat/goodies | | Don't include articles in said strings ֠also responds to 'eat goodies' |
take key | | verb/noun | | take/key | | |
- Adjectives / Nouns acting like Adjectives
User Input | | Parts of Speech | | Said string syntax | | Notes |
take golden sword | | verb/adj/noun | | take/sword<golden | | |
drop heavy black sword | | verb/adj/adj/noun | | drop/sword<black<heavy | | also reponds to 'drop the heavy black sword' |
drop heavy goodies basket | | verb/adj/noun/noun | | drop/basket<goodies<heavy | | |
take goodies basket | | verb/noun/noun | | take/basket<goodies | | |
take basket of goodies | | verb/noun/prep/noun | | take<of/basket/goodies | | dont need to make 'of' a noun |
take basket of goodies | | verb/noun/noun-prep/noun | | take/goodies<basket<of | | need to make 'of' a noun for this to work ֠this appears to be the best thing to do. |
drop heavy basket of goodies | | verb/adj/noun/noun-prep/noun | | drop/basket<heavy/goodies | | |
take black magic voodoo sword | | verb/noun-adj/noun-adj/noun-adj/noun | | take/sword<magic<black<voodoo | | Can use any number of noun-adjs (black/magic/voodoo), in any order (both the user input & said string) |
take top secret water sword | | verb/noun/noun/noun/noun | | take/sword<top<secret<water | | Can use any number of nouns to describe the subject (top/secret/water). Position unimportant. 'Sword' must remain in the same position however. Othe nouns can be in any order (both in user input & said syntax) |
User Input | | Parts of Speech | | Said string syntax | | Notes |
where is sword | | adverb/indverb/noun | | is<where/sword | | as in a question Էhere is sword՛/td] |
throw basket of goodies quickly | | verb/noun/prep/noun/adverb | | throw<quickly/basket<of<goodies | | note that 'of' needs to be a noun for this to work |
quickly throw basket of goodies | | adverb/verb/noun/prep/noun | | throw<quickly/basket<of<goodies | | note that 'of' needs to be a noun for this to work |
crawl under table slowly | | verb/prep/noun/adverb | | crawl<under<slowly/table | | same as 'slowly crawl under table' |
slowly crawl under table | | adverb/verb/prep/noun | | crawl<under<slowly/table | | same as 'crawl under table slowly' |
quickly run down troll | | adverb/verb/adverb/noun | | run<quickly<down/troll | | also responds to 'quickly down run troll' (jibberish), 'run down troll quickly', 'quickly run troll down', does NOT work for 'run troll down quickly', 'run troll quickly down' |
User Input | | Parts of Speech | | Said string syntax | | Notes |
turn lamp on | | verb/noun/prep | | turn<on/lamp | | same said as 'turn on lamp' |
turn on lamp | | verb/prep/noun | | turn<on/lamp | | same said as 'turn lamp on' |
talk to troll | | verb/prep/noun | | talk<to/troll | | |
look in keyhole | | noun-verb/prep/noun | | look<in/keyhole | | |
walk behind curtain | | noun-verb/prep-noun/noun | | walk/curtain<behind | | |
swing sword at troll | | verb/noun/prep/noun | | swing<at/sword/troll | | |
throw sword at thief | | verb/noun/prep/noun | | throw<at/sword/thief | | |
eat goodies with troll | | verb/noun/prep/noun | | eat<with/goodies/troll | | |
put knife upon ledge | | noun-verb/noun/prep/noun | | put<upon/knife/ledge | | see non-working example 'put knife up on ledge' |
swing sword at fat troll | | verb/noun/prep/adj/noun | | swing<at/sword/troll<fat | | |
swing heavy sword at troll | | verb/adj/noun/prep/noun | | swing<at/sword<heavy/troll | | |
swing heavy sword at fat troll | | verb/adj/noun/prep/adj/noun | | swing<at/sword<heavy/troll<fat | | |
- Making words optional '[]'
User Input | | Parts of Speech | | Said string syntax | | Notes |
take long knife | | verb/adj/noun | | take/knife[<long] | | This will enforce that only the adjective 'long' is used, no other adjective is valid. Note that if the said string was 'take/knife', any adjective could be used & it would be valid |
take up sword | | verb/prep/noun | | take[<up]/sword | | Note that string will also accept 'take sword' as user input |
User Input | | Parts of Speech | | Said string syntax | | Notes |
lift up sword OR raise sword | | verb/prep/noun OR verb/noun | | (lift<up),raise/sword | | Both phrases will work, however it is interesting to note that 'raise <any preposition> sword' will also work. |
- Matching any word/no word/exact phrase
User Input | | Parts of Speech | | Said string syntax | | Notes |
look | | verb | | look[/!*] OR look[/noword] | | will only match 'look', not 'look this' or 'look at...' |
look wall OR look bird OR etc... | | verb | | look/* OR look/anyword | | will only match if there is a word following 'look' |
look troll | | verb | | look/troll[/!*] | | |
play game | | verb/noun | | play/game[/noword] | | only will match 'play game', will not match 'play game with troll' unless the 'noword' syntax is removed, then it would match |
take knife | | verb/noun | | take/knife | | note that this will also match 'take long knife', 'take rusty knife', etc. |
lift up sword OR raise sword | | verb/prep/noun OR verb/noun | | (lift<up),(raise[<noword])/sword | | This will ONLY accept 'lift up sword' or 'raise sword' |
- Exhaustive & Complex Examples
User Input | | Parts of Speech | | Said string syntax | | Notes |
give food to the dog | | verb/noun/prep/article/noun | | give/food/dog | | preposition not represented in said string, unnecessary |
give food to dog | | verb/noun/prep/noun | | give/food/dog | | |
give dog the food | | verb/noun/article/noun | | give/food/dog | | |
give dog food | | verb/noun/noun | | give/food<dog | | dog being treated here as an adjective (similar to goodies in 'take goodies basket') |
give food dog (nonsense) | | verb/noun/noun | | give/dog<food | | |
give food the dog (nonsense) | | verb/noun/article/noun | | give/dog/food | | Adding the article changes said string (see 'give food dog') |
take troll to game | | verb/noun/prep/noun | | take<to/troll/game | | see non-working example 'take troll out to game' |
take troll to game | | verb/noun/prep/noun | | take/troll/game | | see alternative parse of 'take<to/troll/game' |
take troll to ball game | | verb/noun/prep/noun/noun | | take<to/troll/game<ball | | ball should probably be classified as an adjective... |
take troll ball game | | verb/noun/noun/noun | | take/game<ball<troll | | nonsense! But it does work... also accepts 'take ball troll game' |
take troll game ball | | verb/noun/noun/noun | | take/ball<troll<game | | also responds to 'take game troll ball' ֠utter nonsense. |
take troll to ball game | | verb/noun/prep/noun/noun | | take/troll/game<ball | | similar to 'take troll to game', but with additional 'ball' noun which acts as an adj |
play game with troll | | verb/noun/prep/noun | | play<with/game/troll | | |
play checkers game with troll | | verb/noun/noun/prep/noun | | play<with/game<checkers/troll | | |
play game of checkers with troll | | verb/noun/noun-prep/noun/prep/noun | | play<with/checkers<game<of/troll | | note that 'of' needs to be a noun for this to work |
play game of checkers with grey troll | | verb/noun/noun-prep/noun/prep/adj/noun | | play<with/checkers<game<of/troll<grey | | note that 'of' needs to be a noun for this to work |
play short game of checkers | | verb/adj/noun/noun-prep/noun | | play/checkers<short<game<of | | note that 'of' needs to be a noun for this to work |
play short game of checkers with troll | | verb/adj/noun/noun-prep/noun/prep/noun | | play<with/checkers<short<game<of/troll | | note that 'of' needs to be a noun for this to work |
quickly play short game of checkers | | adverb/verb/adj/noun/noun-prep/noun | | play<quickly/checkers<short<game<of | | note that 'of' needs to be a noun for this to work |
quickly play short game of checkers again | | adverb/verb/adj/noun/noun-prep/noun/adverb | | play<quickly<again/checkers<short<game<of | | 41 characters. Maximum possible in user input string! |
again play short game of checkers quickly | | adverb/verb/adj/noun/noun-prep/noun/adverb | | play<quickly<again/checkers<short<game<of | | same syntax as 'quickly play short game of checkers again' |
User Input | | Parts of Speech | | Said string syntax | | Notes |
put knife up on ledge | | noun-verb/noun/adverb/prep/noun | | | | Parser reports bad sentence.[/td/ |
take troll out to game | | verb/noun/prep/prep/noun | | | | Parser reports bad sentence. |