event_scheduler.add(600, say_ambient, "You hear the scurrying of rats deep within the walls.")
So now the message will play in exactly 10 minutes. To make it even better, let's do this:
def the_rats():
say_ambient("You hear the scurrying of rats deep within the walls.")
offset = random.randrange(600,1200)
event_scheduler.add(offset, the_rats)
We just made a function that, once called, continues to re-schedule itself forever in random intervals between 10 to 20 minutes apart -- and the best part is we can forget all about it.
Let's say we wanted to implement a four-hour game-day. We could write functions for noon(), dusk(), midnight(), and dawn() which did things like sent farmers to the fields and turned vampires to dust in the sunlight. Each one of these would re-schedule itself, just like the_rats() did, but with a four hour delay (14,400 seconds). So in our start up code we do something like:
# Call noon right nowAnd our world begins to spin.
noon()
# call dusk() in 1 hour
event_scheduler.add(3600, dusk)
# call midnight() in 2 hours
event_scheduler.add(7260, midnight)
# call dawn() in 3 hours
event_scheduler.add(10860, dawn)
An even more efficient version would have each daily event schedule the next one to fire in one hour, round-robin style.
No comments:
Post a Comment