Total time in a level - displayed?

Non-platform specific questions.

Total time in a level - displayed?

Postby CrimsonTheDarkBat » Fri Dec 16, 2011 4:13 pm

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
Sonic Velocity Development Thread --> http://game-editor.com/forum/viewtopic.php?f=4&t=11461

Completed Games:
Sonic: War of the Emeralds
Sonic: Empire of Nightmares
Sonic.EXE - The Game
Alice: Crimson Omen
Epsilon 27
User avatar
CrimsonTheDarkBat
 
Posts: 223
Joined: Fri Mar 21, 2008 10:54 am
Score: 20 Give a positive score

Re: Total time in a level - displayed?

Postby Game A Gogo » Fri Dec 16, 2011 4:27 pm

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.
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Total time in a level - displayed?

Postby CrimsonTheDarkBat » Fri Dec 16, 2011 5:10 pm

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? ^^
Sonic Velocity Development Thread --> http://game-editor.com/forum/viewtopic.php?f=4&t=11461

Completed Games:
Sonic: War of the Emeralds
Sonic: Empire of Nightmares
Sonic.EXE - The Game
Alice: Crimson Omen
Epsilon 27
User avatar
CrimsonTheDarkBat
 
Posts: 223
Joined: Fri Mar 21, 2008 10:54 am
Score: 20 Give a positive score

Re: Total time in a level - displayed?

Postby Game A Gogo » Fri Dec 16, 2011 6:35 pm

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.
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Total time in a level - displayed?

Postby CrimsonTheDarkBat » Fri Dec 16, 2011 6:49 pm

100% working dude, thanks a bunch! =D
Sonic Velocity Development Thread --> http://game-editor.com/forum/viewtopic.php?f=4&t=11461

Completed Games:
Sonic: War of the Emeralds
Sonic: Empire of Nightmares
Sonic.EXE - The Game
Alice: Crimson Omen
Epsilon 27
User avatar
CrimsonTheDarkBat
 
Posts: 223
Joined: Fri Mar 21, 2008 10:54 am
Score: 20 Give a positive score

Re: Total time in a level - displayed?

Postby Game A Gogo » Sat Dec 17, 2011 5:31 pm

No problem! I also hope you understand the process of the code, I tried explaining it the best I could! :)
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Total time in a level - displayed?

Postby mcavalcanti » Sat Feb 04, 2012 10:27 am

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?
Mario Cavalcanti
----------------------------
Code is poetry.
User avatar
mcavalcanti
 
Posts: 54
Joined: Fri Jul 22, 2011 1:09 am
Location: Rio de Janeiro, Brazil
Score: 1 Give a positive score

Re: Total time in a level - displayed?

Postby Game A Gogo » Sat Feb 04, 2012 2:58 pm

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.
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest