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 
 
struct usage?

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



Joined: 20 Feb 2005
Posts: 3
Location: Canada

PostPosted: Fri Mar 04, 2005 6:51    Post subject: struct usage? Reply with quote

Hello, i have made a system that tracks and stores player info in a row that contains fields such as cdkey, ip, kills, deaths, and various flagging options like if they are a vampire, if they are a player DM, if they are AFK etc etc.

I use a struct to keep track of these variables when im playing with them in the module. Basically what i have is my struct declaration with all my variables. So i have struct PlayerData { string blah; etc };

Then i have


PlayerData readdata(object oPC)
{
struct read;
read.Kills = 3;
return read;
}

in the above i define each variable based on DATA i get from the PC (pulling it from the DB)

So all that would be in an include, and i would do

struct PlayerData readdata(oPC);

in my mod onenter script, and then i would change some variables...

read.Kills = 5;

Then i would execute another function that takes in the read struct and inputs each one of those variables back into the DB, updating them. The only problem is they don't seem to be passing into that function as my NWNX logs show no values being read.

I haven't used structures much, what would be the best way for me to load variables into a struct from a PC, modify each one at will in my script, and then update them all back in the DB ?

The reason i did this is so i can make multiple changes and execute one big statement instead of doing individal functions for minor changes.
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Asmodae



Joined: 07 Jan 2005
Posts: 55

PostPosted: Sun Mar 06, 2005 8:33    Post subject: Reply with quote

If I read this correctly, I used something similar to help manage different parts of our complex quest system. I wrote the SQL parts, and another person wrote all the conversations and events. We used structs to pass data back and forth, kinda like this:

Code:

struct PCData
{
   string Name; //pc name, used in database column to id character row
   int HP;
   int Kills;
   int MoreData; // random int variable
   string SomeString; //random string variable
}


void sqlWritePCData(struct PCData charData)
{
 
   string sValues;
   
   if(charData.HP!=0)
   {
       sValues +="HP = " + IntToString(charData.HP);
   }

   if(charData.Kills!=0)
   {
       if(sValues!="")
          sValues += ",";

       sValues +="Kills = " + IntToString(charData.Kills);
   }

   if(charData.MoreData!=0)
   {
       if(sValues!="")
          sValues += ",";

       sValues +="MoreData= " + IntToString(charData.MoreData);
   }

   if(charData.SomeString!="")
   {
       if(sValues!="")
          sValues += ",";

       sValues +="SomeString= " + charData.SomeString);
   }

   SQLExecDirect("UPDATE TableName SET " + sValues + " WHERE TableName.Name = '" + charData.Name + "'");
   
}



This is a gross over simplification, but it builds the SQL Update query based on the values in the struct. You set the values in the struct that you want to change, and the function ignores empty values. You'll have to work something out if you want to be able to use empty values in your table.

Also, if the table row isn't created yet, you would do somethine similar except constructing the SQL Insert query instead. I do a parent function to check if the table entry exists, then branch to update or insert as necessary.

Reading data out of the table is a simple select and then fetch each piece you need.
_________________
Nepenthe - An NWN2 Persistant World, coming to a planet near you. http://www.nepentheonline.com
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
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