The Topic
What if we wanted to detect whenever a player gave an item, such as a fishle, to an NPC so that we could do something in response, instead of just having the NPC boringly give it back?
The Explanation
First we need to find where the code is which deals with NPCs receiving objects. If we search for give, one of the lines that shows up is the function QuestGive , in quests.ls. Looking at this, we see that it contains a number of the reactions which occur when a player gives an item to a NPC.
Looking further, the area right after the line set Exchanger = FALSE looks convenient for placing our code. It also contains a bit of code from which we can see what variable contains the name of the item, which we will need to use. The code in this area uses the variable ItemToGive to check the item which the player gave, from which we create the following code, which we will place after that line. if ItemToGive = "Fishle" then
--do stuff
movie.sendMessage(TheName, "sqa", NPCsName&" says ""E& \
"By george! I don't want your bloody Fishle!""E)
exit
end if
So, we now have a piece of code which checks if the item is a fishle, and if so sends an irate message to the player and exits. Why do we have to explicitly exit? This prevents the default scripts from doing anything which we don't want, such as sending the default message to the player.
The QUOTE structure is a way to add a quote to a string, since normally a quote would be interpreted as a beginning or end of a string. Other things which can be used in this way are RETURN .
What if we wanted to do something a bit more complicated- say, keep a record of the number of times NPCs had been given a fishle. We'd need a global variable which we could increment with the number of times. If we add the following line to the very top of the file, we'll have just such a variable. global NumberOfFishleTimes
Now, we can easily add our improvement to our code by incrementing our variable each time we're given a fishle, and then broadcasting this number as well. if ItemToGive = "Fishle" then
set NumberOfFishleTimes = NumberOfFishleTimes + 1
movie.sendMessage(TheName, "sqa", NPCsName&" says ""E& \
"By george! I don't want your bloody Fishle! This is the " \
&NumberOfFishleTimes&"st time I've been bothered by you fools!""E)
exit
end if
Now we have a script which will cause NPCs to curse at players who try to give them fishles, and which will make them keep track of the number as well!
|