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 
 
Help GetPersistentObject

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



Joined: 16 Jul 2005
Posts: 19

PostPosted: Fri Feb 17, 2006 23:14    Post subject: Help GetPersistentObject Reply with quote

I have the following scripts for OnClientEnter & OnClientLeave.
OnClientLeave works and the Table is created with the objects in it, but when I come back in with the character OnClientEnter dosent put the objects back on him. This should work but the problem seems to be associated with it running from OnClientEnter. It works fine from a placeable. Maybe a timing issue related to OnClientEnter. Please Help?

[ONCLIENTLEAVE]
Code:

#include "aps_include"
void main()
{
//Write PC info to SQL Database [pcdata]
object oPC = GetExitingObject();
int iItem;
string sTable;
string pcname = GetName(oPC);
//Create PC Characters Inventory Table
string sChar;
int iCnt = 0;
int iLen = GetStringLength(pcname);
for(iLen; iLen > 0; iLen--)//Get rid of any spaces
    {
    sChar = GetSubString(pcname, iCnt, 1);//Get next character
    if(sChar == " ")
        {
        //Skip Space
        }
    else
        {
        sTable = sTable + sChar;
        }
    iCnt++;
    }
SQLExecDirect("DROP TABLE " + sTable);
SQLExecDirect("CREATE TABLE " + sTable + " (" +
"player varchar(64) NOT NULL default '~'," +
"tag varchar(64) NOT NULL default '~'," +
"name varchar(64) NOT NULL default '~'," +
"val blob," +
"expire int(11) default NULL," +
"last timestamp NOT NULL default CURRENT_TIMESTAMP," +
"PRIMARY KEY  (player,tag,name)" +
") ENGINE=MyISAM DEFAULT CHARSET=latin1;");

//Save PC CHaracter's Inventory to Their Table
object oItem = GetFirstItemInInventory(oPC);
while (GetIsObjectValid(oItem))
    {
    SetPersistentObject(GetModule(), "Item_" + IntToString(iItem), oItem, 0, sTable);
    oItem = GetNextItemInInventory(oPC);
    iItem++;
    }
}




[ONCLIENTENTER]
Code:

#include "aps_include"
void main()
{
object oPC = GetEnteringObject();
object oChest1 = GetModule();
string sTable;

//Write PC info to SQL Database [pcdata]
int iItem;
string pubcdkey = GetPCPublicCDKey(oPC);
string playername = GetPCPlayerName(oPC);
string pcname = GetName(oPC);
string sChar;
int iCnt = 0;
int iLen = GetStringLength(pcname);

//Make table name from PC's name
for(iLen; iLen > 0; iLen--)
    {
    sChar = GetSubString(pcname, iCnt, 1);//Get next character
    if(sChar == " ")
        {
        //Skip Spaces
        }
    else
        {
        sTable = sTable + sChar;
        }
    iCnt++;
    }

SQLExecDirect("SELECT playername FROM pcdata WHERE pubcdkey='" + pubcdkey +
"' AND pcname='" + pcname + "';");
WriteTimestampedLogEntry("Looking for PC: " + pcname);
if (SQLFetch() == SQL_ERROR)
    {
    // Not found, so create a new character entry into pcdata
    WriteTimestampedLogEntry("Creating new PC: " + pcname);
    SQLExecDirect("INSERT INTO pcdata (pubcdkey, playername, pcname, nvisits, lastlogin) VALUES('" +
    pubcdkey + "', '" + playername + "', '" + pcname + "', '1', CURRENT_TIMESTAMP)");
    // Retrieve the pubcdkey again
    SQLExecDirect("SELECT pubcdkey FROM pcdata WHERE pubcdkey='" + pubcdkey +
    "' AND pcname='" + pcname + "';");
    SQLFetch();
    //Create PC Characters Inventory Table
    SQLExecDirect("CREATE TABLE " + sTable + " (" +
    "player varchar(64) NOT NULL default '~'," +
    "tag varchar(64) NOT NULL default '~'," +
    "name varchar(64) NOT NULL default '~'," +
    "val blob," +
    "expire int(11) default NULL," +
    "last timestamp NOT NULL default CURRENT_TIMESTAMP," +
    "PRIMARY KEY  (player,tag,name)" +
    ") ENGINE=MyISAM DEFAULT CHARSET=latin1;");

    WriteTimestampedLogEntry("Created");
    }
else
    {
    // Character was found
    WriteTimestampedLogEntry("Found PC: " + pcname);
    SQLExecDirect("UPDATE pcdata SET nvisits = nvisits + 1, lastlogin = CURRENT_TIMESTAMP WHERE pubcdkey='" + pubcdkey + "';");
    WriteTimestampedLogEntry("PUBCDCKEY=" + pubcdkey);
    //Get PC Character's Inventory To Their Table
    int bContinue = TRUE;
    object oCreated;
    while (bContinue)
        {
        oCreated = GetPersistentObject(oChest1, "Item_" + IntToString(iItem), oPC, sTable);
        if (!GetIsObjectValid(oCreated))
            bContinue = FALSE;
        else
            iItem++;
        }

    }
WriteTimestampedLogEntry("Returning PUBCDKEY " + pubcdkey);
}
Back to top
View user's profile Send private message
Papillon
x-man


Joined: 28 Dec 2004
Posts: 1060
Location: Germany

PostPosted: Mon Feb 20, 2006 23:51    Post subject: Reply with quote

You are writing to the DB with
Quote:
SetPersistentObject(GetModule(), "Item_" + IntToString(iItem), oItem, 0, sTable);

and read with
Quote:
Created = GetPersistentObject(oChest1, "Item_" + IntToString(iItem), oPC, sTable);


The first parameter is probably your problem. When you look at the functions in aps_include, you'll see that it stores effectively under GetTag(oModule) while it tries to retrieve the items by GetTag(oChest) - which won't work because of differing strings/tags.

You can also check out the odbc log file to see what is going wrong.
_________________
Papillon
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
celtic3



Joined: 16 Jul 2005
Posts: 19

PostPosted: Tue Feb 21, 2006 1:03    Post subject: reply Reply with quote

They are not different tags since oChest1 = GetModule. Also It works on a placeable's hook as is but not on the modules hook as I said.
Back to top
View user's profile Send private message
celtic3



Joined: 16 Jul 2005
Posts: 19

PostPosted: Tue Feb 21, 2006 1:54    Post subject: Resolved Reply with quote

I figured it out, and I apreciate you trying to help. It seems a timing issue is involved with the OnClientEnter hook. Using ExecuteScript("script", GetEnteringObject()); I called the above script from the OnClientEnter. I changed oPC = GetEnteringObject(); in the script to oPC = OBJECT_SELF;.
Nothing happens still.
Put a DelayCommand(6.0, ExecuteScript("script", GetEnteringObject())); and it works like a champ.

Anyone know what the timing issue is?
Back to top
View user's profile Send private message
Rami_Ahmed



Joined: 07 Dec 2005
Posts: 37
Location: Denmark

PostPosted: Mon Jun 05, 2006 20:03    Post subject: Re: Resolved Reply with quote

celtic3 wrote:
Anyone know what the timing issue is?

Probably because the players inventory is not fully loaded when the player enters the module, so delaying it a bit, the players inventory will be loaded. I guess it's a lag issue actually.
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
Acrodania



Joined: 02 Jan 2005
Posts: 208

PostPosted: Mon Jun 05, 2006 20:52    Post subject: Re: Resolved Reply with quote

Rami_Ahmed wrote:
celtic3 wrote:
Anyone know what the timing issue is?

Probably because the players inventory is not fully loaded when the player enters the module, so delaying it a bit, the players inventory will be loaded. I guess it's a lag issue actually.


Yep, the module's OnEnter scripts fire before the PC is completely in the module. You almost always have to add a short delay to accomodate it. Same thing happens when you try to jump a PC OnEnter....

The delay for the PC to fully get into the module and see a area can be as long as 15 seconds with a slow client computer and poor connections.
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