logo logo

 Back to main page

The NWNX Community Forum

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Can somebody please look at my script?

 
Post new topic   Reply to topic    nwnx.org Forum Index -> Scripts and Modules
View previous topic :: View next topic  
Author Message
ultravolts



Joined: 22 Mar 2008
Posts: 9

PostPosted: Sun Mar 23, 2008 14:37    Post subject: Can somebody please look at my script? Reply with quote

Ok, here's the runthrough:
A player goes to the sewers and kills rats, there is a ratcatcher at the entrance to the sewers to whom the player hands in rat whiskers.

This scipt takes the wiskers, counts them and gives gold for each one. Then it updates the persistent number of how many whiskers the player has ever handed in and returns the value to the player.

For some reason it is not updating the SQL values and always says you have only ever handed in 0 whiskers.

The SQL values work fine in other scripts of the module.

Thanks in advance!

Code:
#include "nwnx_sql"
#include "nwn_include"
#include "nw_i0_tool"

int TakeAndPay(object oPC)
{
   int iGoldToGive = 0;
   int iNoWhiskers = 0;
   //----------
   object oItem = GetFirstItemInInventory(oPC);
   
   while (GetIsObjectValid(oItem)) {
      if(GetTag(oItem) == "RatWhisker")
      {
         DestroyObject(oItem);
         iNoWhiskers++;
         iGoldToGive++;
      }
      oItem = GetNextItemInInventory(oPC);
   }
   iGoldToGive = iGoldToGive * 2;
   GiveGoldToCreature(oPC, iGoldToGive);
   //----------
   return iNoWhiskers;
}

void main(){
   object oPC = GetPCSpeaker();
   object oModule = GetModule();

   int RatsKilled = GetPersistentInt(oPC, "RatsKilled", "pwdata");

   int TAP = TakeAndPay(oPC);
   
   RatsKilled = RatsKilled + TAP;
   
   SetPersistentInt(oPC, "RatsKilled", RatsKilled, 0, "pwdata");
   FloatingTextStringOnCreature("Your running total of whiskers handed in, ever: "+ IntToString(GetPersistentInt(oPC, "RatsKilled", "pwdata")) +".", oPC, TRUE, 5.0);
}
[/code]
Back to top
View user's profile Send private message
abraxas77



Joined: 17 Nov 2006
Posts: 15

PostPosted: Mon Mar 24, 2008 0:07    Post subject: Re: Can somebody please look at my script? Reply with quote

Though I cannot say for certain why your script does not work, I can offer a few enhancements to your script which may resolve the problem.

Code:
#include "nwnx_sql"
#include "nwn_include"
#include "nw_i0_tool"

int TakeAndPay(object oPC)
{
   int iGoldToGive = 0;
   int iNoWhiskers = 0;
   //----------
   object oItem = GetFirstItemInInventory(oPC);
   
   while (GetIsObjectValid(oItem)) {
      if(GetTag(oItem) == "RatWhisker")
      {
         DestroyObject(oItem, 1.0f);
         iNoWhiskers++;
      }
      oItem = GetNextItemInInventory(oPC);
   }
   iGoldToGive = iNoWhiskers * 2;
   GiveGoldToCreature(oPC, iGoldToGive);
   //----------
   return iNoWhiskers;
}

void main(){
   object oPC = GetPCSpeaker();

   int RatsKilled = GetPersistentInt(oPC, "RatsKilled", "pwdata");

   RatsKilled += TakeAndPay(oPC);
   
   FloatingTextStringOnCreature("Your running total of whiskers handed in, ever: "+ IntToString(RatsKilled) +".", oPC, TRUE, 5.0);

          SetPersistentInt(oPC, "RatsKilled", RatsKilled, 0, "pwdata");
}


Note:
- I added a 1.0 second delay to your DestroyObject call (it always a good idea set a delay in your DestroyObject calls, especially when it is used in a loop ... the same goes for CreateObject calls)

- I removed the GetPersistentInt call from the string literal you used as a parameter for FloatingTextScringOnCreature. That extra call to your database seems the mostly likely culprit for your bug.

In your version, it is difficult to tell whether the fact that zero whiskers are reported is due to an error getting/setting data to your DB, or whether the error is due to an error in your TakeAndPay function.

The changes above will (in least) enable you to make that distinction. If the floating text continues to always report zero, you know the problem is that your TakeAndPay function always returns zero. If the floating text reports only the number of items removed from the PC's inventory, you know the problem is related to getting/setting data from/to your database.


I hope that helps,
~Abraxas77~
_________________
aka. twp_andrew

:: Lead Script Design :: The World of Paladium II ::
Back to top
View user's profile Send private message
Gryphyn



Joined: 20 Jan 2005
Posts: 431

PostPosted: Mon Mar 24, 2008 0:08    Post subject: Reply with quote

SetPersistentInt(oPC, "RatsKilled", RatsKilled, 0, "pwdata");

this might have something to do with it.
Are you sure the script compiles?
The extra parameter ,0 should be causing the compiler to fail.

Oh, and here's a hit.
Don't go back to the database for the total (FloatingText) as you already have a variable with the correct value (RatsKilled) used to Set it.

Cheers
Gryphyn
Back to top
View user's profile Send private message
abraxas77



Joined: 17 Nov 2006
Posts: 15

PostPosted: Mon Mar 24, 2008 0:19    Post subject: Reply with quote

I assumed he was using the NWNX4 prototype:

// Set oObject's persistent integer variable sVarName to iValue
// Optional parameters:
// iExpiration: Number of days the persistent variable should be kept in database (default: 0=forever)
// sTable: Name of the table where variable should be stored (default: pwdata)
void SetPersistentInt(object oObject, string sVarName, int iValue, int iExpiration =
0, string sTable = "pwdata");



edit: whoops wrong prototype
_________________
aka. twp_andrew

:: Lead Script Design :: The World of Paladium II ::
Back to top
View user's profile Send private message
ultravolts



Joined: 22 Mar 2008
Posts: 9

PostPosted: Mon Mar 24, 2008 2:31    Post subject: Oops Reply with quote

Hi,
I just realised I haven't loaded this module since August last year.

I have the latest NWNX4 update and the new MySQL but I forgot to update the nwnx SQL scripts in my module like nwn_sql and nwn_include.

Perhaps that is causing me the problems, the old scripts still have the additional operator "0" to keep the variable in the SQL database forever.

I will test replacing all the scripts and see what happens.
Back to top
View user's profile Send private message
ultravolts



Joined: 22 Mar 2008
Posts: 9

PostPosted: Mon Mar 24, 2008 2:55    Post subject: Hmm Reply with quote

Aparently this is the latest update in regard to the nwn_sql scipt

// Set oObject's persistent integer variable sVarName to iValue
// Optional parameters:
// iExpiration: Number of days the persistent variable should be kept in database (default: 0=forever)
// sTable: Name of the table where variable should be stored (default: pwdata)
void SetPersistentInt(object oObject, string sVarName, int iValue, int iExpiration = 0, string sTable = "pwdata");
Back to top
View user's profile Send private message
Gryphyn



Joined: 20 Jan 2005
Posts: 431

PostPosted: Mon Mar 24, 2008 4:37    Post subject: Reply with quote

Sorry,
Forgot about iExpiration - I don't use 'defaulting' parameters in my scripting.

G
Back to top
View user's profile Send private message
utimagus



Joined: 22 Jun 2007
Posts: 20

PostPosted: Fri Apr 11, 2008 16:40    Post subject: Reply with quote

have you tried using the code with setlocal to make sure the correct data is being moved around?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    nwnx.org Forum Index -> Scripts and Modules All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group