[solved]Detecting real-world date and time for use in-game

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.
Post Reply
Message
Author
User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

[solved]Detecting real-world date and time for use in-game

#1 Post by Imperf3kt »

I don't seem to be turning up anything by googling and my Python knowledge is minimal.
So I was wondering if this is possible?

In the most basic sense, I want to add a different main menu background if the date matches a certain day.
A usage example would be I want to give my game a christmas theme if the day == December 24-27
This may include character sprites or just menu imagebuttons (possibly a parralax effect, etc)

Or on April 1st, my game flips upside down.

Is there any way to get the real world date into a variable of some sort from which I can use to create these events?

E: hmm, google turns up the 'datetime' function. Could this be used?
https://docs.python.org/2/library/datetime.html
Last edited by Imperf3kt on Mon Nov 27, 2017 4:49 pm, edited 1 time in total.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Detecting real-world date and time for use in-game

#2 Post by trooper6 »

If you look at the Clock recipe in my signature and do a search for "datetime" you can see an example of importing and using date time...the clock uses it for time, not date, but it might give you an idea.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
Donmai
Eileen-Class Veteran
Posts: 1958
Joined: Sun Jun 10, 2012 1:45 am
Completed: Toire No Hanako, Li'l Red [NaNoRenO 2013], The One in LOVE [NaNoRenO 2014], Running Blade [NaNoRenO 2016], The Other Question, To The Girl With Sunflowers
Projects: Slumberland
Location: Brazil
Contact:

Re: Detecting real-world date and time for use in-game

#3 Post by Donmai »

Image
No, sorry! You must be mistaking me for someone else.
TOIRE NO HANAKO (A Story About Fear)

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Detecting real-world date and time for use in-game

#4 Post by Imperf3kt »

Thanks Trooper6, I had looked at your clock code previously but it was too hard for me to understand how I could manipulate it how I wanted. (Or rather, there was so much code that I gave up before I started)
Donmai wrote: Mon Nov 27, 2017 3:52 pm viewtopic.php?f=8&t=37939
Truly awesome! As always this community is really helpful, thanks!

Now lets see, there's halloween, easter, christmas, new years eve/day, april 1st... Any other significant dates I should include? International, of course, so no 4th of July. Unless there's also a way to detect system region?
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

DragoonHP
Miko-Class Veteran
Posts: 758
Joined: Tue Jun 22, 2010 12:54 am
Completed: Christmas
IRC Nick: DragoonHP
Location: Zion Island, Solario
Contact:

Re: [solved]Detecting real-world date and time for use in-game

#5 Post by DragoonHP »

Well you can detect the timezone.

Code: Select all

# If you use the code Donmai linked to, you don't need to import time here
import time

current_tz = time.tzname

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: [solved]Detecting real-world date and time for use in-game

#6 Post by Imperf3kt »

Sorry to drag this up again, but I finally got around to giving this a go tonight.

At first, I got it to work great, but after some more editing of my game, I now cannot get it to NOT show a Christmas theme.

Here is my main menu in it's entirety

Code: Select all

## Main Menu screen ############################################################


screen main_menu():

    tag menu

    style_prefix "main_menu"
    
    if month == 12 and day == 24 or 25 or 26 or 27:
        $ m_music = "music/We Wish You.mp3"
        add gui.xmas_main_menu_background
    else:
        add gui.main_menu_background
    on "show" action Play("music", m_music)

    frame:
        pass

    use navigation

    if gui.show_name:

        vbox:
            text "[config.name!t]":
                style "main_menu_title"

            text "[config.version]":
                style "main_menu_version"


style main_menu_frame is empty
style main_menu_vbox is vbox
style main_menu_text is gui_text
style main_menu_title is main_menu_text
style main_menu_version is main_menu_text

style main_menu_frame:
    xsize 280
    yfill True

    background "gui/overlay/main_menu.png"

style main_menu_vbox:
    xalign 1.0
    xoffset -20
    xmaximum 800
    yalign 1.0
    yoffset -20

style main_menu_text:
    properties gui.text_properties("main_menu", accent=True)

style main_menu_title:
    properties gui.text_properties("title")

style main_menu_version:
    properties gui.text_properties("version")
    
init python:
    import time

    year, month, day, hour, minute, second, dow, doy, dst = time.localtime()
I have not edited options.rpy and the only thing I added to gui.rpy was

Code: Select all

define gui.xmas_main_menu_background = "gui/xmas_main_menu.png"
Which I placed at line 90.

If I remove

Code: Select all

if month == 12 and day == 24 or 25 or 26 or 27:
        $ m_music = "music/We Wish You.mp3"
        add gui.xmas_main_menu_background
    else:
        
Then the original background and music are used, but obviously thats not very useful.

I've tried various things, even changing datetime from an init and using $, to init python.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: [solved]Detecting real-world date and time for use in-game

#7 Post by Remix »

and day in [24,25,26,27]:

using or does not work like it works in speech, your 'or 25' basically just says 'or 25 == True' ...
Frameworks & Scriptlets:

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: [solved]Detecting real-world date and time for use in-game

#8 Post by Imperf3kt »

Remix wrote: Tue Jan 16, 2018 7:31 pm and day in [24,25,26,27]:

using or does not work like it works in speech, your 'or 25' basically just says 'or 25 == True' ...
Thank you.

I guess I better go study Python lists.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: [solved]Detecting real-world date and time for use in-game

#9 Post by trooper6 »

The code Remix gave is the best way to do it.
The long way of doing what you wanted to so...which I hope illustrates what you did wrong...would be:

Code: Select all

if month == 12 and (day == 24 or day == 25 or day ==26 or day == 27):
but

Code: Select all

if month ==  12 and day in [24, 25, 26,27]: 
is much better!
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot]