[Tutorial] Using a real Datetime to control flow of your game

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
enrohk
Newbie
Posts: 11
Joined: Tue Jun 19, 2018 6:32 pm
Location: Spain
Contact:

[Tutorial] Using a real Datetime to control flow of your game

#1 Post by enrohk »

Hi !
I'm already know so much content from this forum and I find myself bad for not sharing myself also what I'm discovering
At the start of my project it was a mesh when I put the factor "time" in the game. It was a mesh, Ints that counts for days , that read from a list of days for names , moments that pass from Morning to evening etc... Read a book ? pass a moment ,cut a flower? pass a moment? ... no

After reading about python datetime and some answers/threads in this forum , I end with a solution that is very simple , compact , easy to translate and allow to control every aspect of your game simply from the date.
First off , I'm learning python myself so probably this code is the most rude and dumb thing you can read. So don't laugh please and try to improve if you have a better method ;) I'm trying to improve here.

First in our script.rpy we put the next :

Code: Select all

init -3 python:
    #required imports
    import datetime
    import locale
    from datetime import timedelta
    
    #We create the variable holding the game time and create not from NI , but with manual data , i hate that .now thing.
    game_date = datetime.datetime(2018,7,31, hour=12, minute=47, second=0)
    #we get the locale from your system, so no need to translate day names , and we work with day numbers , not strings.
    locale.setlocale(locale.LC_ALL, '')
    
    #Methods for change time
    def change_hour(hour):
        global game_date 
        #timedelta only handles seconds or days so we must convert
        segundos = hour * 3600
        game_date = game_date + timedelta(seconds=segundos)
        renpy.restart_interaction() 
        
    def change_minute(minute):
        global game_date 
        segundos = minute * 60
        game_date = game_date + timedelta(seconds=segundos)
        renpy.restart_interaction() 
With this now we have a fully working datetime system with hour included and the methods for change the time (and restart the interaction of the GUI, if you have the time in the guy you want that) and with a date fixed to the time I was writing this.

Now we can create a new screen and add this :

Code: Select all

text(game_date .strftime("%H : %M"))
text(game_date .strftime("%A, %d/%m"))
Those two lines show hour and minutes
The second says the Name of the day , number of day and number of month this is easy customizable.
For customize what the clock or day format says , you have the reference in this page :
https://docs.python.org/3.5/library/dat ... e-behavior

For change the time we can use anywhere :

Code: Select all

	$ change_hour(5)
	$ change_minute(23)
The new days are added automatically , so you don't need to check or change nothing more. simple add time when something happens.

How we can control what kind of backgrounds or things appear at determined hour ?

Code: Select all

        if game_date .hour >= 1:
            background Frame("bg example_midnight",0,0)
        elif game_date .hour >= 8:
            background Frame("bg example_day",0,0)
One important question related to this kind of games is .... Day number , i want to know if is Monday or Saturday . Gladly python already has this method.

Code: Select all

	#monday
        if game_date .weekday == 0:
            do_something_in_monday()
        #sunday
        elif game_date .weekday == 6:
            do_something_in_sunday()
For me it was a very lifesaver because it add the option to customize very well your game , add determined events at determined days or determined hours.
It's also works very well with the garden game I implemented (it uses the same datetime to determine when the plant is ready). And also you don't need to put events that happen when a day ends , simply can put "timestamps" to those events and react when compare the timestamp with the game date.

I hope people find this useful. I can upload a demo if people don't get clear this.
Also , I made a modification of the choice system to allow show the usage of time a determined option can have (or not) but also it shows a energy usage , so i need to adapt to show here . Told if people are interested .

Sorry for my bad english and code ;)
Last edited by enrohk on Tue Jul 31, 2018 7:16 am, edited 1 time in total.
Do I live in a friendly or a hostile universe? Which is it? Is it a universe that is filled with hostility and anger and people wanting to hate each other and people wanting to kill each other, is that what you see?Because when you see the world that way that’s exactly what you will create for yourself in your life.

SuzuAyaGT
Newbie
Posts: 9
Joined: Fri Jun 22, 2018 4:30 am
Contact:

Re: [Tutorial] Using a real Datetime to control flow of your game

#2 Post by SuzuAyaGT »

thank you so much for this!

User avatar
enrohk
Newbie
Posts: 11
Joined: Tue Jun 19, 2018 6:32 pm
Location: Spain
Contact:

Re: [Tutorial] Using a real Datetime to control flow of your game

#3 Post by enrohk »

Hi !
I Just found how RenPy works saving variables hahaha
Probably this code WON'T save the date if you save the game. To solve this simply put the "game_date" variable at "start" label

Code: Select all

label start:
	$ game_date = datetime.datetime(2018,7,31, hour=12, minute=47, second=0)
I think with this approach should be saved correctly , I gonna test myself because I need that :lol:
Maybe is better put a demo when is all finished and working good haha

EDITED Yes. it get saved if you set in the start label , I do some testings and when you modify the date or time and save the game it get save/loaded correctly
Btw I'm thinking of making it a class with more methods for compare dates , return time in days , etc...
Do I live in a friendly or a hostile universe? Which is it? Is it a universe that is filled with hostility and anger and people wanting to hate each other and people wanting to kill each other, is that what you see?Because when you see the world that way that’s exactly what you will create for yourself in your life.

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: [Tutorial] Using a real Datetime to control flow of your game

#4 Post by trooper6 »

That way will also give you problems. The current best practices is to declare your variables outside of any block using the “default” keyword.
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
enrohk
Newbie
Posts: 11
Joined: Tue Jun 19, 2018 6:32 pm
Location: Spain
Contact:

Re: [Tutorial] Using a real Datetime to control flow of your game

#5 Post by enrohk »

trooper6 wrote: Sun Aug 05, 2018 2:46 am That way will also give you problems. The current best practices is to declare your variables outside of any block using the “default” keyword.
U mean this part of documentation ?
https://www.renpy.org/doc/html/python.h ... -statement
Sorry for this noobish question , but that means the method I was using before of putting all the variables and classes constructor in a file outside a label was a good pratique but only need to add "default" to those classes ?
Gonna test this off . Thank you very much for that info :)
Do I live in a friendly or a hostile universe? Which is it? Is it a universe that is filled with hostility and anger and people wanting to hate each other and people wanting to kill each other, is that what you see?Because when you see the world that way that’s exactly what you will create for yourself in your life.

User avatar
Trafagal
Regular
Posts: 100
Joined: Mon Apr 29, 2019 9:32 am
Contact:

Re: [Tutorial] Using a real Datetime to control flow of your game

#6 Post by Trafagal »

enrohk wrote: Tue Jul 31, 2018 7:10 am Hi !
I'm already know so much content from this forum and I find myself bad for not sharing myself also what I'm discovering
At the start of my project it was a mesh when I put the factor "time" in the game. It was a mesh, Ints that counts for days , that read from a list of days for names , moments that pass from Morning to evening etc... Read a book ? pass a moment ,cut a flower? pass a moment? ... no

After reading about python datetime and some answers/threads in this forum , I end with a solution that is very simple , compact , easy to translate and allow to control every aspect of your game simply from the date.
First off , I'm learning python myself so probably this code is the most rude and dumb thing you can read. So don't laugh please and try to improve if you have a better method ;) I'm trying to improve here.
This looks like very useful. I wonder if you will still update the code. Thanks for sharing this!
Check out some of my stuffs here:https://linktr.ee/theartofjoshlab

Art Portfolio: https://www.instagram.com/theartofjoshlab/

Working on a personal Visual Novel~

Post Reply

Who is online

Users browsing this forum: No registered users