Converting game time code to hours/min [SOLVED]

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Message
Author
JinzouTamashii
Eileen-Class Veteran
Posts: 1686
Joined: Mon Sep 21, 2009 8:03 pm
Projects: E-mail me if you wanna rock the planet
Location: USA
Contact:

Converting game time code to hours/min [SOLVED]

#1 Post by JinzouTamashii »

I have the example code

Code: Select all

$ minutes, seconds = divmod(int(renpy.get_game_runtime()), 60)
"%(minutes)d minutes and %(seconds)d seconds to
 finish this demo."
I would like to convert that into hours and minutes, with hours possibly given as a decimal if > a value of 1. I can write that first part, but I'm not sure how to parse the rest, syntax-wise.
Last edited by JinzouTamashii on Sat Oct 31, 2009 2:49 am, edited 1 time in total.
Don't worry, we can get through it together. I didn't forget about you! I just got overwhelmed.
https://cherylitou.wordpress.com

JQuartz
Eileen-Class Veteran
Posts: 1265
Joined: Fri Aug 31, 2007 7:02 am
Projects: 0 completed game. Still haven't made any meaningfully completed games...
Contact:

Re: Converting minutes and seconds gametime code to hours/min

#2 Post by JQuartz »

JinzouTamashii wrote:I would like to convert that into hours and minutes, with hours possibly given as a decimal if > a value of 1.
Here a way to do it, it's unefficient though:

Code: Select all

label start:
    $ hours=0
    $ minutes, seconds = divmod(int(renpy.get_game_runtime()), 60)
    $ minute=minutes
    while minute>=60:
        $ minute-=60
        $ hours+=1
    if hours>0:
        "%(hours)d hours and %(minute)d minutes to finish this demo."
    elif hours==0:
        "%(minutes)d minutes and %(seconds)d seconds to finish this demo."
I suspect somebody is stealing my internet identity so don't believe everything I tell you via messages. I don't post or send messages anymore so don't believe anything I tell you via messages or posts.

delta
Epitome of Generic
Posts: 525
Joined: Sat Dec 22, 2007 12:59 pm
Projects: yes
Contact:

Re: Converting minutes and seconds gametime code to hours/min

#3 Post by delta »

Come on.

Code: Select all

$ allminutes, seconds = divmod(int(renpy.get_game_runtime()), 60)
$ hours, minutes = divmod(allminutes, 60)
The rest is left as an exercise for the reader.

JinzouTamashii
Eileen-Class Veteran
Posts: 1686
Joined: Mon Sep 21, 2009 8:03 pm
Projects: E-mail me if you wanna rock the planet
Location: USA
Contact:

Re: Converting minutes and seconds gametime code to hours/min

#4 Post by JinzouTamashii »

Thank you. (´ ▽ `)ノ

I'm getting better with Python and learning to think like a programmer would, but sometimes these things defeat me. Does Python have a general documentation wiki too for operations like this?
Don't worry, we can get through it together. I didn't forget about you! I just got overwhelmed.
https://cherylitou.wordpress.com

JQuartz
Eileen-Class Veteran
Posts: 1265
Joined: Fri Aug 31, 2007 7:02 am
Projects: 0 completed game. Still haven't made any meaningfully completed games...
Contact:

Re: Converting minutes and seconds gametime code to hours/min

#5 Post by JQuartz »

JinzouTamashii wrote:Does Python have a general documentation wiki too for operations like this?
Probably this:http://docs.python.org/
I suspect somebody is stealing my internet identity so don't believe everything I tell you via messages. I don't post or send messages anymore so don't believe anything I tell you via messages or posts.

JinzouTamashii
Eileen-Class Veteran
Posts: 1686
Joined: Mon Sep 21, 2009 8:03 pm
Projects: E-mail me if you wanna rock the planet
Location: USA
Contact:

Re: Converting minutes and seconds gametime code to hours/min

#6 Post by JinzouTamashii »

Is Ren'Py still using 2.6? I thought we were up to 3.1...
Don't worry, we can get through it together. I didn't forget about you! I just got overwhelmed.
https://cherylitou.wordpress.com

delta
Epitome of Generic
Posts: 525
Joined: Sat Dec 22, 2007 12:59 pm
Projects: yes
Contact:

Re: Converting minutes and seconds gametime code to hours/min

#7 Post by delta »

I'd love to see Python 3 (mostly because it makes Unicode the default string type, which solves a lot of headaches), but since P3 is not backwards compatible and Ren'Py's love for backwards compatibility is only second to the Internet Explorer team's, I would not expect it anytime soon.

Also, just for laughs:
http://docs.python.org/dev/3.0/whatsnew/3.0.html#integers wrote:An expression like 1/2 returns a float.
Well if that isn't just amazing.
The rest is left as an exercise for the reader.

Guest

Re: Converting minutes and seconds gametime code to hours/min

#8 Post by Guest »

Somewhat related, is it possible to have a total running time when the Ren'Py game is first started up and creates the persistent data.

Though not really applying to visual novels, the game time that is recorded is usually the 'best game' time that is usually your shortest game played without mistakes and restarts / reloads. As some developers state (often in response to criticism that their game is too short), the game time recorded in game saves tend to be understated, and do not account for exploration or total time spent on a game.

Would it be possible to save two types of time information... the 'best game' time which is the saved state of your most successful play, as well as 'total running time' which starts counting the first time a new Ren'Py game installation is launched, and only pauses when you close the game.

JinzouTamashii
Eileen-Class Veteran
Posts: 1686
Joined: Mon Sep 21, 2009 8:03 pm
Projects: E-mail me if you wanna rock the planet
Location: USA
Contact:

Re: Converting minutes and seconds gametime code to hours/min

#9 Post by JinzouTamashii »

Back to the original question...

Odd. It returns this error:
I'm sorry, but an exception occured while executing your Ren'Py
script.

TypeError: an integer is required

While running game code:
- script at line 41 of O:\Renpy\SumoftheParts/game/endings.rpy

-- Full Traceback ------------------------------------------------------------

File "E:\Renpy\renpy\bootstrap.py", line 260, in bootstrap
File "E:\Renpy\renpy\main.py", line 308, in main
File "E:\Renpy\renpy\main.py", line 92, in run
File "E:\Renpy\renpy\execution.py", line 230, in run
File "E:\Renpy\renpy\ast.py", line 341, in execute
File "E:\Renpy\renpy\exports.py", line 499, in say
TypeError: an integer is required

While running game code:
- script at line 41 of O:\Renpy\SumoftheParts/game/endings.rpy

Ren'Py Version: Ren'Py 6.9.3c
This is the code I used and it starts on line 41.

Code: Select all

$ hours= divmod(int(renpy.get_game_runtime()), 3600)
$ minutes= divmod(int(renpy.get_game_runtime()), 60)
"You finished the game in %(hours)d and %(minutes)d minutes."
I commented it out now so I can debug other things until I can come back to it and figure out which integer is missing. Even if I haven't played for an hour, it shouldn't give me this! I was hoping to get something like "0 hours and 3 minutes".
Don't worry, we can get through it together. I didn't forget about you! I just got overwhelmed.
https://cherylitou.wordpress.com

Alex

Re: Converting minutes and seconds gametime code to hours/min

#10 Post by Alex »

May be this do the trick

Code: Select all

$ hours= int(divmod(int(renpy.get_game_runtime()), 3600))
$ minutes= int(divmod(int(renpy.get_game_runtime()), 60))

JinzouTamashii
Eileen-Class Veteran
Posts: 1686
Joined: Mon Sep 21, 2009 8:03 pm
Projects: E-mail me if you wanna rock the planet
Location: USA
Contact:

Re: Converting minutes and seconds gametime code to hours/min

#11 Post by JinzouTamashii »

What am I missing here? I used your code, Alex. Ren'Py didn't care for it...
I'm sorry, but an exception occured while executing your Ren'Py
script.

TypeError: int() argument must be a string or a number, not 'tuple'

While running game code:
- script at line 42 of E:\Renpy\SumoftheParts/game/endings.rpy
- python at line 42 of E:\Renpy\SumoftheParts/game/endings.rpy.

-- Full Traceback ------------------------------------------------------------

File "E:\Renpy\renpy\bootstrap.py", line 260, in bootstrap
File "E:\Renpy\renpy\main.py", line 308, in main
File "E:\Renpy\renpy\main.py", line 92, in run
File "E:\Renpy\renpy\execution.py", line 230, in run
File "E:\Renpy\renpy\ast.py", line 558, in execute
File "E:\Renpy\renpy\python.py", line 921, in py_exec_bytecode
File "E:\Renpy\SumoftheParts/game/endings.rpy", line 42, in <module>
TypeError: int() argument must be a string or a number, not 'tuple'

While running game code:
- script at line 42 of E:\Renpy\SumoftheParts/game/endings.rpy
- python at line 42 of E:\Renpy\SumoftheParts/game/endings.rpy.

Ren'Py Version: Ren'Py 6.9.3c
Don't worry, we can get through it together. I didn't forget about you! I just got overwhelmed.
https://cherylitou.wordpress.com

delta
Epitome of Generic
Posts: 525
Joined: Sat Dec 22, 2007 12:59 pm
Projects: yes
Contact:

Re: Converting minutes and seconds gametime code to hours/min

#12 Post by delta »

divmod() only accepts two integers as arguments and returns a tuple of two integers, and you can't simply coerce a tuple into an integer. If you're going to copypasta people's code without understanding what it does, you would be better off not changing it.
The rest is left as an exercise for the reader.

Jake
Support Hero
Posts: 3826
Joined: Sat Jun 17, 2006 7:28 pm
Contact:

Re: Converting minutes and seconds gametime code to hours/min

#13 Post by Jake »

Specifically, divmod(a, b) returns a tuple of (x,y) where (generally) x is the integer (floor) result of the division a/b, and y is the remainder (the modulus of a%b).

Alex's code tries to convert it to an int by calling the int function with that tuple result as a parameter, the error you're seeing is saying that the int method can't take a tuple parameter.

If you look at delta's example way up the page, he's assigning the result of divmod to two separate variables. There's no need to int() them, since both the inputs are integers, the outputs will always already be integers.



Honestly, what's wrong with delta's code? Assuming renpy.get_game_runtime() works as described, delta's code should do what you want...

At least, it does what you want ignoring the idea of a decimal number of hours, because it would produce weird notions like "0.5 hours and 30 minutes", where in fact only 30 minutes had passed. If you really actually want something like that, then it's fairly simple:

Code: Select all

    if (hours < 1):        
        hours = renpy.get_game_runtime() / 3600
Server error: user 'Jake' not found

delta
Epitome of Generic
Posts: 525
Joined: Sat Dec 22, 2007 12:59 pm
Projects: yes
Contact:

Re: Converting minutes and seconds gametime code to hours/min

#14 Post by delta »

Code: Select all

    if (hours < 1):       
        hours = renpy.get_game_runtime() / 3600
better hope get_game_runtime() does not return an integer, or you're going to have a lot of fun with that one.
The rest is left as an exercise for the reader.

Jake
Support Hero
Posts: 3826
Joined: Sat Jun 17, 2006 7:28 pm
Contact:

Re: Converting minutes and seconds gametime code to hours/min

#15 Post by Jake »

delta wrote:

Code: Select all

    if (hours < 1):       
        hours = renpy.get_game_runtime() / 3600
better hope get_game_runtime() does not return an integer, or you're going to have a lot of fun with that one.
Yeah, that's true (albeit incredibly unlikely). Try:

Code: Select all

    if (hours < 1):       
        hours = float(renpy.get_game_runtime()) / 3600
(I expect changing the '3600' to '3600.0' would work too, but I couldn't say for sure off the top of my head.)

Nevertheless, I still reckon it's unlikely to be a desirable way to present the time to the user. And realistically you'd want to round it off to only a couple of decimal places anyway.
Server error: user 'Jake' not found

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]