Script Noobery #2

Ideas and suggestions for game mechanics and rules.
Locked
User avatar
Teric neDhalir
Githyanki
Posts: 1495
Joined: Mon Jan 05, 2004 10:04 pm
Location: Manchester UK

Script Noobery #2

Post by Teric neDhalir »

Howdy
1) I want an NPC to buy fnords from players. A player goes off and collects fnords, and when she speaks to the npc again there will be a conversation node "I have fnords to sell". Obviously the script will need to count the fnords in the pcs inventory, delete them and reimburse the pc accordingly. This is pretty Stone Age stuff, but not something I've done before.. Anyone got the relevant scripts I could steal so I don't have to waste 3 days trying to figure out how to do it?

2) Let's say an npc can only cast the Detect Fnord spell twice a day. If I set a local variable that includes the game date that increments each time the spell is cast so that the npc cannot cast the spell when the variable > 2 will that be reset when the mod is restarted? I'm guessing yes, but wanted to check.

Tnd (The Noob DM)
User avatar
Rotku
Iron Fist Tyrant
Posts: 6948
Joined: Tue Jan 06, 2004 1:09 am
Location: New Zealand (+13 GMT)

Post by Rotku »

Gah! Been too long since I've touched the toolset.

This should do the trick for number 1, where the fnords tag is "fnord" and you are rewarding 3gp per fnord

Code: Select all

void main()
{
object oPC = GetPCSpeaker();
   int nItems = 0;
   object oItem = GetFirstItemInInventory(oPC);
   while (GetIsObjectValid(oItem) == TRUE)
   {
    if (GetTag(oItem) == "fnord")
      {
      nItems = nItems + GetNumStackedItems(oItem);
      DestroyObject(oItem);
      oItem = GetNextItemInInventory(oPC);
      }
   }
int nGold = nItems *3;
GiveGoldToCreature(oPC, nGold);
}
Regarding number 2, I believe you are right.
< Signature Free Zone >
User avatar
Teric neDhalir
Githyanki
Posts: 1495
Joined: Mon Jan 05, 2004 10:04 pm
Location: Manchester UK

Post by Teric neDhalir »

Cheers! I'll give that a go.
User avatar
ç i p h é r
Retired
Posts: 2904
Joined: Fri Oct 21, 2005 4:12 pm
Location: US Central (GMT - 6)

Post by ç i p h é r »

Local variables are not persistent across module resets. If you want variables to be persistent, use one of these functions:

http://www.alandfaraway.org/docs/Techni ... ersistence

FYI. This article is linked off the main Wiki tech node (called Data Persistence under the For Scripters heading). There are lots of useful bits of information up there for your perusal so visiting it periodically is something I highly recommend you do as you roll up your sleeves.

p.s. You might want to consider adopting a well designed quest system off the vault (or help us develop ALFA's) to handle these kinds of things. You'll spend more of your time building quests, less on script noobery, and hopefully keep your module free of ill conceived scripts. :wink:

p.p.s. If you're trying to learn how to script, I think that's great! As you probably already know, the lexicon is invaluable for learning the ropes and understanding how the scripting engine works. Most of it will only come with time and practice, however.
User avatar
Rotku
Iron Fist Tyrant
Posts: 6948
Joined: Tue Jan 06, 2004 1:09 am
Location: New Zealand (+13 GMT)

Post by Rotku »

ç i p h é r wrote:p.p.s. If you're trying to learn how to script, I think that's great! As you probably already know, the lexicon is invaluable for learning the ropes and understanding how the scripting engine works. Most of it will only come with time and practice, however.
I'd add Lilac Soul's to that list. With that and the lexicon it makes basic scripting relatively simple.
< Signature Free Zone >
User avatar
Teric neDhalir
Githyanki
Posts: 1495
Joined: Mon Jan 05, 2004 10:04 pm
Location: Manchester UK

Post by Teric neDhalir »

Rotku,
Sorry, that didn't work :( A bit of playing around and invoking Lilac Soul gives me this, which seems to be ok (giving 5 gold and 10XP per fnord):
// Reward PC for each copy of item with tag "fnord"
// they have in their inventory.
// Fired from conversation node after checking for presence of item
// with gc_check_item and sItem = "fnord"
// Teric neDhalir 060907



void main()
{
object oPC = GetPCSpeaker();
int nItems = 0;
object oItem = GetFirstItemInInventory(oPC);

while (GetIsObjectValid(oItem))
{
if (GetTag(oItem)=="fnord")
{DestroyObject(oItem);
nItems = nItems + 1;}
oItem = GetNextItemInInventory(oPC);
}

int nGold = nItems*5;
int iXP = nItems*10;
GiveGoldToCreature(oPC, nGold);
GiveXPToCreature(oPC, iXP);
}
Same words, different order. God, I hate scripting.
I find it hard to believe that we can't build a library of such simple functions after all the years people have been working on NWN! Or maybe I'm not looking in the right place.
Teric
User avatar
ç i p h é r
Retired
Posts: 2904
Joined: Fri Oct 21, 2005 4:12 pm
Location: US Central (GMT - 6)

Post by ç i p h é r »

I'm sure you'll find code you can scavenge from NWN1 mods, but there hasn't been a concerted effort to construct a library of scripts. We've got a spot for them in our repository though [if you wish to contribute].

IMPORTANT NOTE: If you intend to grant XP via custom scripts, be advised that you can only use the following function to do it. Anything else will result in an error or retraction of the XP given:

Code: Select all

void ACR_GiveXPToPC(object oPC, int nXpAmount);
You will also need the following line at/near the top of your script.

Code: Select all

#include "acr_xp_i"
Just a heads up.

p.s. I just added this to the Wiki "rewarding experience" script article so we have something in there.
User avatar
Rotku
Iron Fist Tyrant
Posts: 6948
Joined: Tue Jan 06, 2004 1:09 am
Location: New Zealand (+13 GMT)

Post by Rotku »

Teric neDhalir wrote:Rotku,
Sorry, that didn't work :(
Heh, I see the mistake now. Sorry about that - been a while since I touched the toolset.
< Signature Free Zone >
Locked