Page 1 of 1

Total time in a level - displayed?

PostPosted: Fri Dec 16, 2011 4:13 pm
by CrimsonTheDarkBat
Basically, I want to have a timer in the HUD which shows the current playtime for a particular stage in minutes (play Sonic and you'll know what I mean xD)

How do I go about this? Thanks! :D

Re: Total time in a level - displayed?

PostPosted: Fri Dec 16, 2011 4:27 pm
by Game A Gogo
Using the pre-defined variable frame, you can get the numbers of frame you have had the program running.
Knowing this, if you divide it by you FPS, for example, 30, you will get the numbers of second, if you divide this by 60 you get the numbers of minutes.

Code: Select all
textNumber=round((float)frame/1800);

because 30*60 equals 1800.
if you had a FPS of 60, then the number would be 3600.

Why use this instead of a timer? Because this will take in account for lag. If there is lag then the player can't really do what he should be able to do, thus it doesn't handicap him if he encounters lag.

Re: Total time in a level - displayed?

PostPosted: Fri Dec 16, 2011 5:10 pm
by CrimsonTheDarkBat
Awesome, that works thanks! :) +1 rep

But tell me, how can I make it so that after 60 seconds, the seconds will reset to 0 - and then add 1 to the minute actor? ^^

Re: Total time in a level - displayed?

PostPosted: Fri Dec 16, 2011 6:35 pm
by Game A Gogo
pretty simple, create two variables. One named tmin and one named tsec. We will use these variable to display time with:
Code: Select all
int tmin,tsec;
sprintf(text,"%d:%d",tmin,tsec);

sprintf allows us to use formatting when displaying a string, including variables.

but how to display the seconds and minutes?
by using the code I've already provided you, you can easily display the minutes.
And by using the modulus operator %, you can display the seconds too!

Code: Select all
int tmin,tsec;
tmin=(float)frame/1800;//1800=30*60, 30 being the fps
tsec=(int)((float)frame/30)%60;//30 being the fps
sprintf(text, "%d:%02d",tmin,tsec);


%d will display integers normally
%02d will display integers while always filling with 2 0's if there's not enough digits.
the 0 means the filling kind, 2 means how many zeros it should be filled with.

http://www.cplusplus.com/reference/clib ... o/sprintf/

what (float) does is convert a variable into a float, so when you perform an operation (In this case divide by 1800), you will have more precision. Because in theory, if you divide an integer directly, there will be less precision than if you divide it as a float and then convert it into an integer.

in tsec, you can see we are using (int) because % cannot use any other form of type. % gives you the remainder of a division, but I prefer to think of it as a sort of warping function.

Re: Total time in a level - displayed?

PostPosted: Fri Dec 16, 2011 6:49 pm
by CrimsonTheDarkBat
100% working dude, thanks a bunch! =D

Re: Total time in a level - displayed?

PostPosted: Sat Dec 17, 2011 5:31 pm
by Game A Gogo
No problem! I also hope you understand the process of the code, I tried explaining it the best I could! :)

Re: Total time in a level - displayed?

PostPosted: Sat Feb 04, 2012 10:27 am
by mcavalcanti
I'm using this method to calc a level gameplay time, but I'm trying to reset tmin and tsec when the game is over, and I can't. :-(
I just put 'tmin=0; tsec=0;' when the player object is created, but nothing is happening. Is there a way?

Re: Total time in a level - displayed?

PostPosted: Sat Feb 04, 2012 2:58 pm
by Game A Gogo
Because frame variable is a read only variable and tsec and tmin are dependent on frame. So you'll have to create a new variable and manually increase it.

Use the variable dialog to create this variable as you'll need it to be global, or you can create it in the global script editor.

Code: Select all
int tmin,tsec;
tmin=(float)time/1800;//1800=30*60, 30 being the fps
tsec=(int)((float)time/30)%60;//30 being the fps
sprintf(text, "%d:%02d",tmin,tsec);
time++;//Increase the time manually


when you want to reset the timer, just set time to 0
Code: Select all
time=0;


Edit: I'd recommend using the Global script, as normal integers are limited to 32767 frames, which will make it loop to -32768 if you try to add to it too much, which is something you probably want to avoid.

Code: Select all
long unsigned int time;


in the global code will make the integer accept values from 0 to 4294967295, which would mean more frames can be accounted for.

long means to use double the bytes a normal integer uses. unsigned means to use only positive numbers, doubling the length.