Triggering an event at a specific time

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: 3792
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Triggering an event at a specific time

#1 Post by Imperf3kt »

Recently, I received a private message requesting help to send a message to the player at 8pm, Christmas eve in their local time.
I came up with the following

Code: Select all

init python:
    import time

    year, month, day, hour, minute, second, dow, doy, dst = time.localtime()


screen xmas_check():
    modal False
    
    if month is 12 and day is 24 and hour is 20:
        textbutton("It's Christmas!") action NullAction
        


label start:
    e "Put this where you want the menu."
    show screen xmas_check
    e "Did it work?"

    return
    
It works, but its not perfect; it runs at game launch, so it only shows if the player started the game _after_ 8pm, it won't show the message if the player started playing _before_ 8pm. I assume this is because it is run at init?

Does anybody know what I should do to. Get the.desired behaviour?
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: Triggering an event at a specific time

#2 Post by trooper6 »

They could use my clock as a basis. It has a realtime mode and an alarm can be created. Clearly the clock is more than they would need, but looking at the code could give inspiration.
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
Imperf3kt
Lemma-Class Veteran
Posts: 3792
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Triggering an event at a specific time

#3 Post by Imperf3kt »

I tried borrowing bits of your clock code, specifically the alarm. Unfortunately, it is too complex for me to. Understand., so I failed.
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
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Triggering an event at a specific time

#4 Post by Per K Grok »

Imperf3kt wrote: Mon Oct 01, 2018 10:08 pm ---

It works, but its not perfect; it runs at game launch, so it only shows if the player started the game _after_ 8pm, it won't show the message if the player started playing _before_ 8pm. I assume this is because it is run at init?

---
A quick thought, if you put

year, month, day, hour, minute, second, dow, doy, dst = time.localtime()

in the screen, would that not solve that problem?

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

Re: Triggering an event at a specific time

#5 Post by Imperf3kt »

Sorry, I don't know what you mean.
Are you suggesting putting the python inside the screen instead of an init block?
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: Triggering an event at a specific time

#6 Post by Remix »

In Ren'Py there are several ways to set up automatic running of code:

config.periodic_callback This runs many times a second, so is not really suitable here.
config.python_callbacks This might be suitable, it runs whenever python code runs during game context (e.g. if you had $ e.hair_style = "messy" within your script it would run then)
Putting the code somewhere that is repeatedly accessed, such as a screen, a loop/interim label or as part of the menu system. If your game has no label loop, perhaps integrate it into the save screen... if the players loads or saves the test runs and ...
Are you suggesting putting the python inside the screen instead of an init block?
Yes, just make sure it doesn't cause side effects or try python_callbacks if you have enough python code in your game.
Last edited by Remix on Tue Oct 02, 2018 5:45 am, edited 1 time in total.
Frameworks & Scriptlets:

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

Re: Triggering an event at a specific time

#7 Post by Imperf3kt »

Thanks for the help, that appears to have mostly done the trick.

Code: Select all

init python:
    import time
screen xmas_check():
    modal False
    python:
        year, month, day, hour, minute, second, dow, doy, dst = time.localtime()
I'll tidy things up and pass them along as well as make use of this for my own uses.
Thanks everyone.
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

Post Reply

Who is online

Users browsing this forum: No registered users