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 
 
Simple NWNX Database Over Bioware's DB

 
Post new topic   Reply to topic    nwnx.org Forum Index -> Windows technical support
View previous topic :: View next topic  
Author Message
werehound



Joined: 17 Aug 2010
Posts: 41

PostPosted: Tue Aug 17, 2010 21:28    Post subject: Simple NWNX Database Over Bioware's DB Reply with quote

So, been playing with NWNx2 for about a year now, and databases still escape me. Just looking to create user-individualized databases for storing gold and item resrefs (to *create* an updated item each time their chest is loaded). By Individualized, I mean working off the player's cd key. I've got the Bioware Databases running, but it's causing the server to crash once I had over 50 databases loaded and transferred. The crash mainly occurs when the server is trying to write new data to the database.

However, I have absolutely no idea how to create the databases in NWN, or access them in game.

I read up on MySQL a bit, and know about SELECT and UPDATE and all those commands, but I have no idea how to access them ingame via scripting. So, I guess my query is:

Help?

At the moment, the script stores the actual item:

Here's the current script to save the items in the nwn DB:
Code:
//::///////////////////////////////////////////////
//:: Name Placeable OnClosed Bank Script
//:: FileName bank_close
//:: Copyright (c) 2010 Werehound Silverfang
//:://////////////////////////////////////////////
/*
    This script will store all items within the container under the
    username gathered with the corresponding OnOpened event.
*/
//:://////////////////////////////////////////////
//:: Created By: Werehound Silverfang
//:: Created On: 4/09/10
//:://////////////////////////////////////////////

//Define the maximum number of items each container can hold.
int MAX_ITEMS = 100;

void main()
{
    object oCloser = GetLastClosedBy();
    string sName = GetLocalString(OBJECT_SELF, "Key");
    string sStored = "Player: " + sName + " stored the following: ";

    //Let's perform the count to make sure there's an acceptable
    //number  of items to be stored.
    object oItem = GetFirstItemInInventory(OBJECT_SELF);
    int count = 0;
    while (oItem != OBJECT_INVALID){
        count++;
        if (GetHasInventory(oItem)){
            FloatingTextStringOnCreature("You may not store containers!", oCloser, FALSE);
            return;
        }
        oItem = GetNextItemInInventory(OBJECT_SELF);
    }
    if (count > MAX_ITEMS){
        FloatingTextStringOnCreature("You cannot store more than " + IntToString(MAX_ITEMS) + " items!", oCloser, FALSE);
        return;
    }

    //Now we actually store the items.
    oItem = GetFirstItemInInventory(OBJECT_SELF);
    int i = 1;
    while (oItem != OBJECT_INVALID){
        string sItemName = GetName(oItem);

            StoreCampaignObject(sName, "Stored_Item_" + IntToString(i), oItem);
            DestroyObject(oItem);
            if (sItemName != "") sStored += sItemName + "; ";
            i++;

        oItem = GetNextItemInInventory(OBJECT_SELF);
    }
    //Set the number of items stored.
    SetCampaignInt(sName, "Banked_Items", i);
    //Output String containg the resrefs of all stored items.
    if (sStored != "Player: " + sName + " stored the following: ") PrintString(sStored);
    //Delete the key for the next user. This technique prevents items
    //from being stored into the vault of anyone who uses the placeable.
    DeleteLocalString(OBJECT_SELF, sName);
}


And here's the current code that loads the items from the database.
Code:
//::///////////////////////////////////////////////
//::///////////////////////////////////////////////
//:: Name Placeable OnOpened Bank Script
//:: FileName bank_open
//:: Copyright (c) 2010 Werehound Silverfang
//:://////////////////////////////////////////////
/*
    This script will retrieve all stored items under the player's username.
*/
//:://////////////////////////////////////////////
//:: Created By: Werehound Silverfang
//:: Created On: 4/09/10
//:://////////////////////////////////////////////
void main()
{
    object oPC = GetLastOpenedBy();
    string sName = GetPCPlayerName(oPC);
    string sLoaded = "Player: " + sName + " loaded the following: ";
    SetLocalString(OBJECT_SELF, "Key", sName);
    int nItems = GetCampaignInt(sName, "Banked_Items");
    int i = 1;
    while (i <= nItems)
    {
        object oStored = RetrieveCampaignObject(sName, "Stored_Item_" + IntToString(i), GetLocation(OBJECT_SELF), OBJECT_SELF);
        string sItemName = GetName(oStored);
        if (sItemName != "") sLoaded += sItemName + "; ";
        DeleteCampaignVariable(sName, "Stored_Item_" + IntToString(i));
        i++;
    }
    if (sLoaded != "Player: " + sName + " loaded the following: ") PrintString(sLoaded);
    DeleteCampaignVariable(sName, "Banked_Items");
}


The crash only occurs when saving items. What is going on, and how can NWNx2 DB help my cause?
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
Zebranky



Joined: 04 Jun 2006
Posts: 415

PostPosted: Wed Aug 18, 2010 0:23    Post subject: Reply with quote

Persistent storage is a somewhat tricky beast, so I'll start with the lazy way out and point you at the code for one approach: http://mercuric.net/nwn/pcontainer.rar

Take a look and feel free to ask any questions.
_________________
Win32 SVN builds: http://www.mercuric.net/nwn/nwnx/

<Fluffy-Kooshy> NWNx plugin is to this as nuclear warheads are to getting rid of fire ants.

<ThriWork> whenever I hear nwn extender, I think what does NWN need a penis extender for?
Back to top
View user's profile Send private message Visit poster's website
Zebranky



Joined: 04 Jun 2006
Posts: 415

PostPosted: Wed Aug 18, 2010 0:25    Post subject: Reply with quote

Oh, and to answer your question about the crash, it's hard to say without doing some debugging, but the CodeBase DB code that BioWare used for the campaign DB causes the program to exit silently on some error conditions. This is but one of many reasons that you should be using NWNX/SQL instead. Smile
_________________
Win32 SVN builds: http://www.mercuric.net/nwn/nwnx/

<Fluffy-Kooshy> NWNx plugin is to this as nuclear warheads are to getting rid of fire ants.

<ThriWork> whenever I hear nwn extender, I think what does NWN need a penis extender for?
Back to top
View user's profile Send private message Visit poster's website
Greyfort



Joined: 20 Jul 2010
Posts: 66

PostPosted: Wed Aug 18, 2010 5:41    Post subject: Reply with quote

I took your scripts and placed them on the on_open and on close of a chest.
I had no problems.

The known glitch as I understand it is as follows

1) only items that are default nwn items or module made can be stored. custom items will not store and may cause error.

what error are you getting?

Is it with stack size if so you need to limit gold to stacks of 50,000 that is another known glitch.

Also you dont want to use the inventory keyboard button it breaks the chest. The way around this is to create the chest when player steps into area/trigger, then remove the chest when player steps out of area/trigger.

Also you can have only one pc use chest at a time. So you have to have a place ware only one pc can enter at a time so there is no mistakes(only for the impatient ones).

Other then that, I probably forgot a few bugs but those are the big ones.

You can easily take your script and use nwnx scripting, as sub 4 you SET/GET Campaign string/int/float/etc functions.

Also I changed part of your on_open...to

// added/changed so as not to over write any possible nwn db named just my player name
//string sName = GetPCPlayerName(oPC);
string sDB_PREFIX="WS_BANK_";
string sName = ""+ sDB_PREFIX + GetPCPlayerName(oPC)+"";
//or
//string sName = ""+"WS_BANK_"+GetPCPlayerName(oPC)+"";

no need for you to change it but you could do it either way. Just remember with other DB info you want to store you may wipe out your bank data so if you have crafting skills you would want a diffrent db prefix. Just a safety option.

Good work on your code I got no errors I'll try to see if I can crash it LOL

Keep posting your questions We are here to help.
God Bless

update 1] (8.17.2010): MUST ONLY PUT 50,000gp in at stack, you can have multiple stacks though. So 2 stacks 50k=100k gp.
!!! DON'T !!! put over 50k you will loose
Back to top
View user's profile Send private message
werehound



Joined: 17 Aug 2010
Posts: 41

PostPosted: Wed Aug 18, 2010 7:17    Post subject: Reply with quote

Custom items were being stored fine. My problem occurred on the database generation. I wasn't given any errors at all. The log contains none, and my personal debug script failed to run (it was a few steps into the script, after database creation).

No errors at all, just :Neverwinter Nights Server has stopped working" without any information.
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
Greyfort



Joined: 20 Jul 2010
Posts: 66

PostPosted: Wed Aug 18, 2010 20:26    Post subject: Reply with quote

Hmm server error? I didn't get a server error, but I did add a data base prefix (SEE ABOVE POST).

I took your script and made a quick little module(houndsilver),putting in safe guards (SEE ABOVE POST) that were once issues.

As you said I was able to store custom items(that was nice use to be problem), I could not put more then 50kstack of gold in but could put multiple stacks in.

I can post mod (houndsilver) on vaults if you like?
Other then gold I have had no issue. Do you have others to help you test mod (houndsilver) if so use mod (houndsilver) see if crashes, then test your mod. If you mod crashes use data base prefix, and safe guards from (houndsilver).

OH do you have any module events on_load / client_enter, that call for a nwn data base? I haven't had any issues but if script has possible sql error if useing sql with nwn db there can be issues. Dragons Wake bank gives an example of this on vault they use sql to store data.
LINK:
http://nwvault.ign.com/View.php?view=Scripts.Detail&id=2246

UPDATE: 8.19.2010

module:(houndsilver) Will take your script out after you get a copy of mod incase you want to keep it to your self.

vault link:
http://nwvault.ign.com/View.php?view=Modules.Detail&id=6171

Any other questions we are here to help


Last edited by Greyfort on Fri Aug 20, 2010 5:02; edited 1 time in total
Back to top
View user's profile Send private message
werehound



Joined: 17 Aug 2010
Posts: 41

PostPosted: Thu Aug 19, 2010 8:18    Post subject: Reply with quote

I've got roughly 14 players who paly my server constantly waiting to test (more than that, but on average through the day 14 players). Will test the safe-checks then get back to u.

Thanks guys.
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
Sethan



Joined: 04 Oct 2008
Posts: 47

PostPosted: Sat Aug 21, 2010 4:44    Post subject: Reply with quote

Something to consider regarding this is what to do when the placeable gets stuck in the open position.

We use something similar for persistent storage in our PW, but each persistent storage item is a unique item in the world and physically secured (usually in a private or semi-private location with a key).

We use OnDisturbed to store things in the DB rather than OnClose. This also prevents things like item duplication.
Back to top
View user's profile Send private message
maddogfargo



Joined: 03 Nov 2009
Posts: 49

PostPosted: Fri Nov 19, 2010 1:34    Post subject: Reply with quote

Hi Werehound...

Another poster recently asked about persistent storage. I replied here:

http://www.nwnx.org/phpBB2/viewtopic.php?t=1703

That system may help you a bit. It uses a storage creature (Chest NPC). Everything in the creature's inventory is stored exactly as it appears in-game with variables, new tags, new names, etc. Other systems have used a similar approach. While this may not be as elegant or feature packed as some...it is simple, making it easy to alter/customize, including changing it from using BioDB to using SQL if you prefer.

Also...if you want to see some more SQL database commands for NWN I recommend looking at the functions included in the APS persistance system on NWVAULT. Seeing how they did it helped me out a lot.
_________________
* illegible scribble *
Back to top
View user's profile Send private message
werehound



Joined: 17 Aug 2010
Posts: 41

PostPosted: Sun Dec 05, 2010 21:01    Post subject: Reply with quote

I actually moved away from NPC storage as you could overflow the NPC without much difficulty, even with a 50 item limit.
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
maddogfargo



Joined: 03 Nov 2009
Posts: 49

PostPosted: Mon May 02, 2011 23:48    Post subject: Reply with quote

werehound wrote:
I actually moved away from NPC storage as you could overflow the NPC without much difficulty, even with a 50 item limit.


I've been away for a while. Sorry for the delayed reply.

I havent had any overflow issues as you are manipulating the storage creature's inventory directly, not transferring to/from a 'bank chest' or other container. You see the inventory status and space live and the game engine wont let you over-fill it.

Feel free to pop in and check it out sometime. It is on my OC Quartz shop server and Netheria server. Connection has been a little flakey this week though so be patient if u cant get in. I havent found the right hammer to beat the server with just yet. Laughing
_________________
* illegible scribble *
Back to top
View user's profile Send private message
werehound



Joined: 17 Aug 2010
Posts: 41

PostPosted: Wed May 04, 2011 7:04    Post subject: Reply with quote

Using a SQL DB right now, working great.
You can indeed over-fill an NPC inventory. Just quick write a script to add 50 tower shields to a creature's inventory. It'll hold 30, but 20 will drop on the ground because it couldn't hold anymore.
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    nwnx.org Forum Index -> Windows technical support 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