Creature scripts

Every creature in the game has a script that determines their behaviour. Nearly all creatures use basicnpc.txt by default, and most of the time this is all that's needed. But if you want something special to happen (in our case, run some code when the pesky goblin dies), you'll need to write your own creature script. Thankfully, if you copy basicnpc.txt, 99% of your work is done.

Create a copy of basicnpc.txt named c1goblin.txt (the naming convention for creature scripts that only get used in one town is 'c' for creature, then the number of the town, then a short description). Then change the DEAD_STATE (the state that runs when the creature is killed):

beginstate DEAD_STATE;
  set_flag(0, 1, 2);
  change_spec_item(0, 1);
    "The goblin keels over, dead. You paw through its belongings and find an expensive-looking necklace.", ""

SDF (0,1) is what we were using to keep track of progress on the necklace quest. It started at 0, and was set to 1 when the quest was given. Now it's set to 2. change_spec_item(0, 1) give 1 copy of special item 0 to the player. Special items are items that show up on the character screen rather than in the inventory. Like quests, they need to be defined in the scenario script (pesky.txt in our case):

beginstate LOAD_SCEN_STATE;
  init_quest(0, "Retrieve the Necklace", "Get the merchant's necklace back from the pesky goblins.");
    "Fancy Necklace",
    "This is the necklace the merchant wanted you to retrieve. Hmm, you could probably get a lot for this..."

Finally, set the script for the goblin in the Editor. Select the goblin (you might have to click twice to select the goblin instead of the terrain script) and change "Script" from "default" to "c1goblin":


Note that it's possible to set the default script for a creature in the scenario data file (peskydata.txt in our case). This is handy if you need to place many creatures that all use the same script.

Finally, let's have the goblin drop a fancy custom item when it dies, as an extra reward.