Frictional Games Forum (read-only)
Tracking playing time - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: Tracking playing time (/thread-9669.html)



Tracking playing time - Akasu - 08-09-2011

I was wondering if there is a smart way to track the time someone has played a certain custom story and display it when the player interacts with, for example, a clock entity in game.

The tracking could probably happen like this:
Code:
void OnStart()
{
AddGlobalVarInt("time", 0);
AddTimer("timer", 60, "tracker");
}

void tracker (string& asName)
{
SetGlobalVarInt("time", GetGlobalVarInt("time")+1; // don't know if it works like this. gotta test
AddTimer("timer", 60, "tracker");
}

This would track the game time every minute but the problem is how to display it... One way to do this is trough a debug message:
Code:
AddDebugMessage("Time played: "+GetGlobalVarInt("time"), false);
but the player would need to have debug mode on for it to show.

If it was done trough SetMessage I'd need to make lots of entries in the lang file which would be pretty time consuming.
So what would be the smartest way to do this?


RE: Tracking playing time - Your Computer - 08-09-2011

I know the .lang file supports calling commands by using the $ character. However, i haven't done anything with it, so i'm not sure how it works.


RE: Tracking playing time - Apjjm - 08-09-2011

As far as I am aware, the $ sign is only used for variables relating to keys. It isn't too hard to generate some code that will create some entries automatically for you though, so the brute force approach isn't off the table at all here; I've hastily slapped the following together in python to prove the point:
Code:
# --- Initial Setup --- #
#File name
fname = "output1.txt"
#Lang File details
lfCatagory = "gametime" #The catagory of the time entries
lfPfx = "gt"            #Prefix of each entry
#Message String
msPrefix = "You have played for" #Prefix of the entry
msSuffix = "in this session"     #Suffix of the entry
#Limits
lmLower = 1    #Smallest possible time spent playing (mins)
lmUpper = 6001 #Upper bound on time spent playing (mins)
#Mode      #1: HH hours MM minutes
mode = 1   #2: HH:MM
          
# --- Precalculation --- #
lfPfx = "    <Entry Name=\"" + lfPfx
msPrefix = "\">" + msPrefix
msSuffix = " " + msSuffix + "</Entry>\n"

# --- Generation --- #
#Catagory line
output = "  <CATEGORY Name=\"" + lfCatagory + "\">\n"

     #Generate entries (mode 1)
if mode == 1:
    for m in range(lmLower,lmUpper):
        ihrs = m / 60
        imns = m % 60
        hrs = ""
        mns = ""
        if ihrs > 0:
            hrs = " " + `ihrs` + " hour"
            if ihrs != 1:
                hrs += "s"
        if imns > 0:
            mns = " " + `imns` + " minute"
            if imns != 1:
                mns += "s"
        output += lfPfx + `m` + msPrefix + hrs + mns + msSuffix
else: #Generate entries (mode 2)
    for m in range(lmLower,lmUpper):
        ihrs = m / 60
        imns = m % 60
        output += lfPfx + `m` + msPrefix + " " + `ihrs` + ":" + `imns` + msSuffix
#Add in closing catagory tag
output += "  </CATEGORY>"


# --- Write the output --- #
try:
    fout = open(fname,"w")
    fout.write(output)
    fout.close()
except IOError:
    print "Error writing to file!"

This will generate all the entries up to 100 hours of playtime. You can change the initial settings and stuff to suit. Just copy-paste the output into the relevant spot in the relevant lang file. I generated a file of approx 524kb using this script, entries looking like:
Code:
<Entry Name="gt2">You have played for 2 minutes in this session</Entry>
  <Entry Name="gt3">You have played for 3 minutes in this session</Entry>
  <Entry Name="gt4">You have played for 4 minutes in this session</Entry>
  <Entry Name="gt5">You have played for 5 minutes in this session</Entry>
  <Entry Name="gt6">You have played for 6 minutes in this session</Entry>
  <Entry Name="gt7">You have played for 7 minutes in this session</Entry>
  <Entry Name="gt8">You have played for 8 minutes in this session</Entry>
...
  <Entry Name="gt5996">You have played for 99 hours 56 minutes in this session</Entry>
  <Entry Name="gt5997">You have played for 99 hours 57 minutes in this session</Entry>
  <Entry Name="gt5998">You have played for 99 hours 58 minutes in this session</Entry>
  <Entry Name="gt5999">You have played for 99 hours 59 minutes in this session</Entry>
  <Entry Name="gt6000">You have played for 100 hours in this session</Entry>
You can then use the entries as follows:
Code:
void OnStart()
{
SetGlobalVarInt("time", 1); //Init time var
AddTimer("timer", 2* 60, "tracker"); //Not really bothered with 0th minute.
}

void tracker(string& asName)
{
AddGlobalVarInt("time",1); //Increment time var
AddTimer("timer", 60, "tracker"); //Increment again in 1 min
}

void showTime()
{
//It's show time, for showing the time.
//*badum* *tish*
SetMessage("gametime","gt"+GetGlobalVarInt("time"),-1);
}
This is very much a inelegant approach, but 500kb is nothing - it is like adding 1 custom decal, and you can probably relax the time constraint down to under half the size of that. It would be nice if there was a virtual "temp" entry, that we could read/write to using scripting, and then use as if it was in the language file though!

This method assumes you want to track the total time the player spent playing the game. Obviously, if you want it to tell the time based on how long the player has been playing you would take a similar approach but have a greatly decreased time interval, as only 24 hours would need to be covered and the modulo function would handle the rest.


RE: Tracking playing time - Phoroneus - 08-09-2011

If you want to go purely by scripts, you could theoretically set a 60-second timer that refreshes itself and increments a variable (e.g. "ClockTime"). Then, when you interact with the clock it could call for the variable to determine what time to display. This might end up becoming a really long script though, unless you managed to get the display time right from the variable instead of running it through extra_english.lang, since you'd need 60 minutes (times roughly two lines) per hour, and at least twelve if not twenty-four hours' worth of minutes to display the time via extra_english.

If you wanted to be really devious, you could set one timer to increment a variable (i.e. "ClockMinutes") for 0-59 and then reset, incrementing a second variable (i.e. "ClockHours") and setting "ClockMinutes" to zero. Once "ClockHours" reached 12 (or 24) it could also reset back to zero, effectively creating a clock out of .hps script.

Of course, how you'd implement the clock in a recognizable fashion into the game is another matter.