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 
 
Have NWNX2 and MySQL on Ubuntu 6.0 Error: DB went away!
Goto page Previous  1, 2
 
Post new topic   Reply to topic    nwnx.org Forum Index -> Linux technical support
View previous topic :: View next topic  
Author Message
Jesterb769



Joined: 25 Aug 2006
Posts: 22

PostPosted: Mon Dec 18, 2006 3:09    Post subject: Reply with quote

im not much for scripting, but if I understood how to write to MySQL right, what I've got looks like this in my module heartbeat
Code:

int sSQLConnectHB = GetLocalInt(GetModule(), "SQLSelect1");
if( sSQLConnectHB != 1)
{
SetLocalInt(GetModule(),"SQLSelect1",1);
DelayCommand(120.0, SetLocalInt(GetModule(),"SQLSelect1",0));
    string sSQL;
        sSQL = "SELECT 1;";
        SQLExecDirect(sSQL);
}

If i got that right, it should fire roughly every two minutes +/- a heartbeat
_________________
Jester the Occasionally Merciful
World of Raathi
Creative Director/Admin
Back to top
View user's profile Send private message
Papillon
x-man


Joined: 28 Dec 2004
Posts: 1060
Location: Germany

PostPosted: Tue Dec 19, 2006 0:12    Post subject: Reply with quote

It looks a bit... unusual, but yes, this should work.
_________________
Papillon
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Jesterb769



Joined: 25 Aug 2006
Posts: 22

PostPosted: Tue Dec 19, 2006 4:35    Post subject: Reply with quote

I was never very good with setting timers and such, so that could be the reason Rolling Eyes
_________________
Jester the Occasionally Merciful
World of Raathi
Creative Director/Admin
Back to top
View user's profile Send private message
FunkySwerve



Joined: 02 Jun 2005
Posts: 377

PostPosted: Tue Dec 19, 2006 4:57    Post subject: Reply with quote

It's worth noting that delaycommands eat up more cpu than equivalent heartbeats, around five times more in nwn1. I haven't profiled your script, but generally speaking its better to use heartbeats for things that you always want running.
Funky
Back to top
View user's profile Send private message
Jesterb769



Joined: 25 Aug 2006
Posts: 22

PostPosted: Tue Dec 19, 2006 10:46    Post subject: Reply with quote

in the interest of expanding my own knowledge then... could I ask how you would suggest doing it instead? I looked at your script and (as embarassing as it is), I didn't quite understand it. I admit I don't quite have a grasp on how the % sign is used yet... Embarassed
_________________
Jester the Occasionally Merciful
World of Raathi
Creative Director/Admin
Back to top
View user's profile Send private message
FunkySwerve



Joined: 02 Jun 2005
Posts: 377

PostPosted: Tue Dec 19, 2006 20:20    Post subject: Reply with quote

The % is 'remainder'. In normal nwn int division, the remainder is discarded (you can also think of this as always rounding down). With this function, it calculates the remainder as an int, rather than splitting it into a decimal. The quotient is disregarded, only the remainder is output. Here are a few examples:

6%5 = 1, because 5 goes into 6 once, with a remainder of 1.
50%10 = 0, because 10 goes into 50 5 times, with no remainder.
58%5 = 3, because 5 goes into 58 11 times, with a remainder of three.
46%12 = 10, because 12 goes into 46 3 times, with a remainder of 10.

I find it helps to think of the division brace we were taught in grade school:

__42__<----quotient
7/300
28
---
20
14
---
6 <----remainder

With that in mind, have a look at the if clause again:

if (!(nTime%60))

The ! simply reverses the truthhood of the statement it abuts, so that if it is FALSE(0) it becomes TRUE(nonzero), and if TRUE(nonzero) it becomes FALSE.

nTime is being incremented by 5, every 5 seconds. Most of the time, it will have a remainder, but when a multiple of 60 rolls around, the remainder will nonzero, or TRUE. The ! switches it so that its FALSE, except when a multiple of 60 rolls around every minute, when it's TRUE, and the conditional fires sending the SELECT 1 to the database:

5 - 5%60 = 5 (TRUE) so (!5%60) = FALSE
10 - 10%60 = 10 (TRUE) so (!10%60) = FALSE
15 - 15%60 = 15 (TRUE) so (!15%60) = FALSE
20 - 20%60 = 20 (TRUE) so (!20%60) = FALSE
25 - 25%60 = 25 (TRUE) so (!25%60) = FALSE
30 - 30%60 = 30 (TRUE) so (!30%60) = FALSE
35 - 35%60 = 35 (TRUE) so (!35%60) = FALSE
40 - 40%60 = 40 (TRUE) so (!40%60) = FALSE
45 - 45%60 = 45 (TRUE) so (!45%60) = FALSE
50 - 50%60 = 50 (TRUE) so (!50%60) = FALSE
55 - 55%60 = 55 (TRUE) so (!55%60) = FALSE
60 - 60%60 = 0 (FALSE) so (!60%60) = TRUE <-- conditional fires
65 - 65%60 = 5 (TRUE) so (!65%60) = FALSE
70 - 70%60 = 10 (TRUE) so (!70%60) = FALSE
75 - 75%60 = 15 (TRUE) so (!75%60) = FALSE
...
115 - 115%60 = 55 (TRUE) so (!75%60) = FALSE
120 - 120%60 = 0 (FALSE) so (!120%60) = TRUE <-- conditional fires

As you can see, the conditional will fired once per minute. The rest of the heartbeat is negligible overheadwise (and I'm a real stickler in that regard). We use a similar one to check for interserver messages in ourr database:



Code:

void main()
{
    int nTime = GetLocalInt(OBJECT_SELF,"uptime");
    nTime = nTime + 5;//changed from 6 because server firing hb every 5.012 seconds
    SetLocalInt(OBJECT_SELF,"uptime", nTime);
    if (!(nTime%60))
    {
        string sServer = GetLocalString(OBJECT_SELF, "ServerNumber");
        string sSQL = "SELECT name, sender, message FROM messages WHERE server = '" + sServer + "'";
        string sMessage, sName, sLocalString;
        object oAddressee, oPC;
        string sSender;
        int nCount = 1;
        int nX;
        object oMod = GetModule();
        SQLExecDirect(sSQL);
        while(SQLFetch() != SQL_ERROR)
        {
            sMessage = SQLDecodeSpecialChars(SQLGetData(3));
            sName = SQLDecodeSpecialChars(SQLGetData(1));
            sSender = SQLDecodeSpecialChars(SQLGetData(2));
            PrintString(sMessage+sName+sSender);
            if (sName == "None") AssignCommand(oMod, SpeakString(sMessage, TALKVOLUME_SHOUT));
            else
            {
                oAddressee = FindAddressee(sName);
                if (GetIsObjectValid(oAddressee))
                {
                    FloatingTextStringOnCreature("<c þ >Interserver message received from " + sSender + "!</c>", oAddressee, FALSE);
                    //SendMessageToPC(oAddressee, "<c þ >" + sMessage + "</c>");
                    SendChatLogMessage(oAddressee, "<c þ >" + sMessage + "</c>", GetMessenger());
                }
            }
        }
        sSQL = "DELETE FROM messages WHERE server = '" + sServer + "'";
        SQLExecDirect(sSQL);
    }
}


Another common use of % is to break up large switches in order to reduce the number of comparisons:

Code:

int GetMaxItemCostUseableAtLevel(int nPCLevel)
{
    int nReturn;
    if (nPCLevel < 31)
    {
        switch (nPCLevel/6)
        {
            case 0:
            switch (nPCLevel%6)
            {
                case 1: nReturn = 1000; break;
                case 2: nReturn = 1500; break;
                case 3: nReturn = 2500; break;
                case 4: nReturn = 3500; break;
                case 5: nReturn = 5000; break;
            }
            break;
            case 1:
            switch (nPCLevel%6)
            {
                case 0: nReturn = 6500; break;
                case 1: nReturn = 9000; break;
                case 2: nReturn = 12000; break;
                case 3: nReturn = 15000; break;
                case 4: nReturn = 19500; break;
                case 5: nReturn = 25000; break;
            }
            break;
            case 2:
            switch (nPCLevel%6)
            {
                case 0: nReturn = 30000; break;
                case 1: nReturn = 35000; break;
. . .


Hope that helps,
Funky
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    nwnx.org Forum Index -> Linux technical support All times are GMT + 2 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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