Author Topic: Quest - Retrieve Random Item  (Read 337 times)

Legacy_Snarkblat

  • Full Member
  • ***
  • Posts: 104
  • Karma: +0/-0
Quest - Retrieve Random Item
« on: November 26, 2011, 05:27:04 pm »


               Hello again.

As part of the Gilead module I'm working on, I'd like to have an NPC that gives the PC random "fetch and retreive" quests. It should be rather simple, but I think declaring the random CustomTokens is giving me trouble, specifically declaring the "oItem = ".

Say I have 10 or so items.
PC asks NPC for a job. NPC's actions taken declare a random item, and they ask the PC for a <CUSTOM401> or such. The item is set as a local variable, and the PC brings it back, gets some gold for their troubles.

Here's a snippet what I have so far:

//actions taken on the NPC node
void main()
{
    object oPC = GetPCSpeaker();
    // Give the speaker the items
    CreateItemOnObject("it_shoppinglist", oPC, 1);

    // Set the variables
    SetLocalInt(OBJECT_SELF, "quest", 1);

  object oItem;
  switch(d4()) {
    case 1:
      oItem = "it_bread";
      break;
    case 2:
      oItem = "it_milk";
      break;
    case 3:
      oItem = "it_fish";
      break;
    case 4:
      oItem = "it_meat";
      break;
}
SetCustomToken(401, oItem);
}

Doesn't compile, obviously. I feel like I'm missing something glaringly obvious, and would appreciate someone giving me a helpful nudge.
               
               

               


                     Modifié par Snarkblat, 26 novembre 2011 - 05:29 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Quest - Retrieve Random Item
« Reply #1 on: November 26, 2011, 07:21:09 pm »


               ok, a couple of problems there.&nbsp;&nbsp;&nbsp; First you have never declaired what type of varaiable oItem is. You have to explicitly tell the compiler what type of data a varaiable holds before you can use it.
here is a list of the data types usen in NWScript.Data Types

Now if you have that script in the script editor and double click on SetCustomToken  you will get some help information at the bottom of the screen for how the function needs to be implimented.

It will show this.  

// Set the value for a custom token.
void   SetCustomToken   ( int nCustomTokenNumber, string sTokenValue 


The colored parts I will explain a little better.&nbsp;

void  The only thin this first void says is that the function does not return any data to the calling code.    If the function did return data to the calling code It would be the Data Type of the Data returned. 

SetCustomToken this is the lable the function is know by. Just like any Lable/Variable It has to have a data tye associated with it. In this case it is a Void, meaning that is does something but does not return any data to the calling code.
 
nCustomTokenNumber and sTokenValue are the Arguments to the function. Thies are the lables that the function will use internaly for the data that you pass to the function. The actual lables are meaningless but are comonly given names to help the user understand what type of data the function need passed to it.

  int and string are the Data Types that the function expects to be passed to it for the Arguments.  So nCustomTokenNumber has to be of data type int and sTokenValue has to be of type string

Now before I get back to your script. Let me comment on the 'o' in oItem. it is a common practice to have a Naming Conventions in place when writting a script. The Conventions are not enforced by the compiler. They are just rules set in place and followed by the scripter to make there ability to read the code easier.  By the Conventions adopted by most NWN Scripters the 'o" in front of your oItem lable would tell then that it is a varaiable/Lable for an object.

Since your script looks like it is just trying to set the custom token with a varaiable the has to be a string, I would suggest changing the name to something like sItemName.  and declare it towards the begining of the script with: string sItemName;

Also you are currently setting this varaiable to the ResRef's of the items.  That is not what you want. you should change them to the names you want the custon token set to, "bread", "Milk" and so forth.
               
               

               


                     Modifié par Lightfoot8, 26 novembre 2011 - 07:44 .