Detecting passing time (aka how do inn rooms work?)

Moderator: Event DM

Post Reply
User avatar
Calzier
CCC
CCC
Posts: 2284
Joined: Mon May 02, 2005 10:39 am
Timezone: GMT/BST
Location: UK

Detecting passing time (aka how do inn rooms work?)

Post by Calzier » Wed Aug 01, 2007 10:50 am

Hi -

I need, for another project, to have an object determine how muh time has passed when a PC interacts with it, much as the Avlis inn rooms seem to when extorting rent.

Can someone point me in the direction of the appropriate functions, or paste a code snipet?

Thanks

Cal.
User avatar
Jonezie
Team Member; Retired with Honors
Posts: 3905
Joined: Wed Jun 09, 2004 7:05 am
Location: Melbourne (GMT +10)

Post by Jonezie » Wed Aug 01, 2007 11:21 am

There's a few ways you can do this, depending if you're working with persistency functions and an external database.

For short-term timers, the easiest method is to set a local int, and then delete it after a set time using a DelayCommand(). Then you add a check to the start of your script that aborts it if the int is set to TRUE. For example...

Code: Select all

    int iTest = GetLocalInt(OBJECT_SELF, "Stored_Value");

    if (iTest)
        return;

    // Your Code Here

    SetLocalInt(OBJECT_SELF, "Stored_Value", TRUE);
    DelayCommand(10.0, DeleteLocalInt(OBJECT_SELF, "Stored_Value"));
That only works within the time-frame of a server reset, however. If you want a Persistent timer, a-la inn rooms, you've got two options.

1. SetPersistentInt(object oObject, string sVarName, int iValue, int iExpiration = 0, string sTable = "pwdata")
If you pass a value for iExpiration into SetPersistentInt(), the variable will be deleted from the database in that many days. This is accomplished by an external program that runs once per day, and decrements the value in this field, deleting any that would drop to zero. I believe that it only works on the pwdata table, and has to be set up specially.

2. Datetimes. Yay.

Code: Select all

    SQLExecDirect("select (unix_timestamp(now())");
    SQLFetch();
    iReturn = StringToInt(SQLGetData(1));
That snippet of code will return the current time, presented as the number of seconds since some reference value. Not sure what the reference value is, but it doesn't really matter. What you can do, though, is record this value either as a normal or persistent int, and then compare it to the current time later on to determine how much time has passed. For example...

Code: Select all

    SQLExecDirect("select (unix_timestamp(now())");
    SQLFetch();
    int iTime = StringToInt(SQLGetData(1));

    int iStoredTime = GetLocalInt(OBJECT_SELF, "Stored_Time");

    if (iTime < (iStoredTime + 10))
        return;

    //Your Code Here

    SetLocalInt(OBJECT_SELF, "Stored_Time", iTime);
Note that in both examples, the delay is set for 10 seconds. Note also, that the last two options require some for of persistent data storage and NWNX.
User avatar
PlasmaJohn
CCC / Release Admin
CCC / Release Admin
Posts: 9010
Joined: Fri Dec 26, 2003 10:37 pm
Timezone: US/Eastern
Location: Negaria
Contact:

Post by PlasmaJohn » Wed Aug 01, 2007 12:56 pm

Jonezie wrote:1. SetPersistentInt(object oObject, string sVarName, int iValue, int iExpiration = 0, string sTable = "pwdata")
If you pass a value for iExpiration into SetPersistentInt(), the variable will be deleted from the database in that many days. This is accomplished by an external program that runs once per day, and decrements the value in this field, deleting any that would drop to zero. I believe that it only works on the pwdata table, and has to be set up specially.
The pwdata table stores a datestamp of the last time the value was modified. The daily purge script deletes any rows where expiration>0 and timestamp+expiration < now. There are several tables that are maintained this way, the default persistent variable table (pwdata) and the one managing innroom 90 day evictions.

A very common mistake to make with the persistent variable functions is to continually update them and assume that the original expiration time still holds: every change resets the clock.

Another common mistake is to assume that persistent variables enforce a period. The rent collection code uses something very similar to Jonezie's second method.

So, for one-shots, p-vars are suitable, for periodic usage, you'll need something else.
Calvin: This is so cool!
Hobbes: This is so stupid.
Post Reply