knowing how long i've played?

Started by scourge, Sun 27/11/2005 22:59:18

Previous topic - Next topic

scourge

hey. I have searched the forums and have found some topics on this, but not what i need. If i've missed something please re-direct me :)

I want to know how long i've played my game, right? so i use DateTime.RawTime. I have a global long start_time, when i first enter the game (new game) it does the following:

DateTime* dt = DateTime.Now
start_time = dt.RawTime

Then, just before i SAVE, i compute end_time (local) in the same manner and then del_time (global) by end_time-start_time.

BUT when i load again there is no way of recovering start_time and computing it again cause its saved with the previous value. Is there a way of NOT saving start_time AND is there then a way of computing start_time right after i loaded the game? What would be ideal is having access to the load function (RestoreGameSlot()) and directly compute the start_time then after game is loaded.  :P

Is there some way of holding the game time like this or otherwise?

Scummbuddy

I would suggest having a seperate text file that keeps track of your statistics.

Take a look at the following:
---------------------
Open
(Formerly known as FileOpen, which is now obsolete)

static File* File.Open(string filename, FileMode)

Opens a disk file for reading or writing. These disk I/O functions are only intended for simple tasks like the way the QFG series export the character when you complete it.
MODE is either eFileRead, eFileWrite or eFileAppend, depending on whether you want to write to or read from the file. If you pass eFileWrite and a file called FILENAME already exists, it will be overwritten.

eFileAppend opens an existing file for writing and starts adding information at the end (ie. the existing contents are not deleted).

This function returns a File object, which you use to perform operations on the file. null is returned if there was a problem (eg. file not existing when MODE is eFileRead).

NOTE: You MUST close the file with the Close function when you have finished using it. There are only a limited number of file handles, and forgetting to close the file can lead to problems later on.

IMPORTANT: If you open the file for writing, then you can ONLY work with files in the game directory. You CANNOT use a path, so any filename with "\" or "/" in it will automatically be rejected, for security reasons.

NOTE: Open file pointers are not persisted across save games. That is, if you open a file, then save the game; then when you restore the game, the File will not be usable and you'll have to open it again to continue any I/O. The safest practice is not to declare any global File variables.

Example:

File *output = File.Open("temp.tmp", eFileWrite);
if (output == null)
  Display("Error opening file.");
else {
  output.WriteString("test string");
  output.Close();
}

will open the file temp.tmp for writing. An error message is displayed if the file could not be created. Otherwise, it will write the string "test string" to the file and close it.
See Also: File.Close, File.ReadString, File.WriteString
- Oh great, I'm stuck in colonial times, tentacles are taking over the world, and now the toilets backing up.
- No, I mean it's really STUCK. Like adventure-game stuck.
-Hoagie from DOTT

scourge

i have conscidered keeping it a file, however i will reconscider it again :)

When would i then load the file, should i keep one? that is how do i know i have just loaded a game?

Ashen

You could use the eEventRestoreGame event, in the on-event function.
If there's just this one thing you need to track, it might be easier to just have another variable to store total time (e.g. total_time += (end_time-start_time)), and use eEventRestoreGame to reset start_time at the start of a new 'session'.
I know what you're thinking ... Don't think that.

scourge

#4
thanks ashen, i'll go try it out. I did successfully do it with files using these two small functions:

Code: ags

function saveStartTime() {
Ã,  File* output = File.Open("store.dat", eFileWrite);
Ã,  if (output != null) {
Ã,  DateTime* dt = DateTime.Now;
	int starting_time = dt.RawTime;
	output.WriteInt(starting_time);
	output.Close();
Ã,  }	
}
function readStartTime() {
Ã,  int starting_time = 0;
Ã,  File* output = File.Open("store.dat", eFileRead);
Ã,  if (output != null) {
Ã,  Ã,  starting_time = output.ReadInt();
Ã,  Ã,  output.Close();
Ã,  }
Ã,  return starting_time;
}


When loading or starting a new game ->saveStartTime()
When saving save the variable total_time += dt.RawTime - readStartTime()

:) your approach sounds soooo much simpler, i'll check it out.

scourge

thanks ashen, that was exactly what i was looking for :)

SMF spam blocked by CleanTalk