A way to keep track of play time?

Started by Lufia, Sat 14/11/2009 22:16:25

Previous topic - Next topic

Lufia

Is there a built-in way to keep track of the time that was spent playing per game save? I can't find anything anywhere.

Matti

I don't know if there's a built-in way to to so, but you could just update a variable (every 40 gameloops for counting the seconds).

suicidal pencil

#2
You'd have to script it in yourself.

Code: ags

function repeatedly_execute() 
{
  Loops++; //Global Variable
  if(Loops >= 40)
  {
    Seconds++; //Global Variable
  }
}

function TellTime()
{
  while(Seconds >= 60)
  {
    Minutes++;
  }
  while(Minutes >= 60)
  {
    Hours++;
  }
  while(Hours >= 24)
  {
    Days++;
  }
}


For save games without closing the game in between, this would do. But if the game is closed over save games, you'd need to get the computer time from before it's closed, then when it's played again, and then compare the numbers.

Lufia

Having the play time is a pretty standard thing so I tought there would be a built-in way. Isn't it best to count game loops and convert when making the game save, rather than counting seconds?

I'd also place the counter in repeatedly_execute_always(), otherwise it'll get screwed up.

Snake

#4
How about:
Note: I'm taking in account that you do what Suicidal Pencil has suggested...

Every time the player saves the game a file can be created in the game's folder, let's call it, Game_Time.txt. In Game_Time.txt the variables; seconds, minutes, hours and days, are written and stored here (initially these variables would of course equal zero until the game is saved for the first time).

When the player loads their saved game, Game_Time.txt is read. Now, all the variables, in the game's script, equal what they do in that file. From there it is added to with the code that Suicidal Pencil has given (or similar at least).

This is only a concept of course, but I'm thinking it might be the way to go with what you need.
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

RickJ

#5
Here is a module that does what you want.   Elapsed game time is stored in a data file whose name is of your choosing.  Documentation is in the source file.

GamePlayTimer Module
[edit]
fixed broken link

suicidal pencil

#6
Quote from: RickJ on Sun 15/11/2009 02:31:31
Here is a module that does what you want...

There's always a module of what you want. If you cant find it, you're not looking hard enough

edit: That link is broken. Very, very, broken.

edit2: http://demo.agspace.ws/project/archive/GamePlayTimer-M0101-000.zip

Lufia

How feasible is it to put all the data I want to keep track of in game saves in a single data file? I guess I'd have to rewrite the whole file each time and use WriteRawLine() to give some kind of fixed structure to the file.

RickJ

I'm not sure I understand the question. 

1) Static variables (those declared outside the bounds of a function) are persistent between game saves. 

2) If I were going to save data to a file I would probably use WriteInt(..)/readInt(..), WriteString(..)/ReadStringback(..).

Lufia

Quote1) Static variables (those declared outside the bounds of a function) are persistent between game saves.
But they are reset when you quit the game, right? Otherwise, why would you use an external data file to store the play time?

Quote2) If I were going to save data to a file I would probably use WriteInt(..)/readInt(..), WriteString(..)/ReadStringback(..).
I want to provide additional info for each saved game (play time and keep track of some collectibles and such), that would mean 3 or four different variables to keep track of per saved game. Now, in your module, you use one data file per timer started. Say I want to keep everything in a single file ; I don't see a way to do that using WriteInt()/WriteString(), rather I need some kind of way to delimit different data, which is why I thought WriteRawLine() would be the way to go.

Khris

I'm not sure I understand what exactly the problem seems to be, but as long as any data is stored during the game in room or global variables, it's saved and restored with save games.

There's no need for an additional file that can be tampered with.

RickJ

But they are reset when you quit the game, right? Otherwise, why would you use an external data file to store the play time?

No they are not reset.  The play time is kept in a separate file because the game may have been played but not saved at the end of the session.

Lufia

How do you display the play time when loading a save from the title screen? Variables are restored after you've loaded a game, not before, no?

That's actually the main reason why I thought just storing everything in an array wouldn't work.

RickJ

My module saves/restores the game play time from a data file when GamePlayTime.Start() is executed, which is recommended to be done in the game_start() event handler in the global script.

Quote
How do you display the play time when loading a save from the title screen? Variables are restored after you've loaded a game, not before, no?
Yes a game restore will overwrite the current game play time to what it was when the game was last saved. I don't know that this is necessarily incorrect. I suppose it would depend upon the game and the game designers intent.  However,   this behaviour can be easily changed by stopping the timer before restoring the game and then restarting it again after it is restored, as illustrated in the code below.  This will cause the game play time to be written to the data file before the restore operation is executed and retrieved after it is completed.

Code: ags

*** Global Script ***
function MyCustomRestorGameSlot(int slot) {
   // This function save the current game play time before restoring the game.
   GamePlayTimer.Stop();
   RestoreGameSlot(slot);

   // Use on_event() handler to restart timer
   // GamePlayTimer.Start();
}

function on_event(EventType event, int data) {
   // Restart game play timer after a game restore
   if (event==eEventRestoreGame) GamePlayTimer.Start();
}


Lufia

I think I'm confusing you. Let's start over.

I want to keep track of some stuff per game save. That is, if I call my load game GUI, when I click on an item in the list of saved games, the screenshot of that save will be displayed, along with some info relative to that saved game, like play time, number of lemmings collected, money...

I click on the save named "blah", it displays that at the moment of that save, I had 2 lemmings, which might not be the number of lemmings I have now, but is the number of lemmings that I will have if I load that save.

So I need to keep track of an array that stores 3 or 4 variables per saved game (hello struct definition) and which actually woudln't change if I load a game, and which would contain all the right values before any game is loaded in case I'm loading a game from the title screen.

Is it possible to keep such an array in AGS or would I need to manage an external file? In either case, how would I go about implementing that?

GarageGothic

You could store the info as part of the save game name, possibly using some special character (like "|") that lets you identify and display the user given name and your game state info separately. Unfortunately there's currently a character limit for save game names (I've been lobbying to have this limit removed), so the more info you need to store, the less characters are available for the player to use in naming his save.

RickJ

Well, you will need to keep that data in an external file. Probably the easiest way to manage it is to have a struct array where each element corresponds to a save slot number.  Before doing a game restore you will have to write this data to  a file.  After the game restore is complete you will need to read the file back in to the struct using the eEventRestoreGame event illustrated ion my previous example.

SMF spam blocked by CleanTalk