Timed Events in Evennia
In the Evennia game engine, there is a wide assortment of ways to time an event in the game world, and at first I was a bit overwhelmed and confused by the various options. In this thought, I'll be going through each of them.
Specific in-game Time
This one is a bit annoying to actually get started with, as there is no API which can hook into the settings, or in-game. This one can only be created through pure Python code. The uses for being able to schedule a function to run at a very specific in-game time, as every Evennia world has an internal calendar and clock. This can be completely ignored by the game, or it can be used for various purposes, including the scheduling of something to happen in-game at a very specific in-game time.
There is a few examples which can help better understand how to use this in code, it creates a timed event which will show during an in-game sunrise as the example. In the backend, this will create a new Script object with an attached interval.
Script with Interval
One thing that should be noted about these Script objects, is that they do not require any sort of timed events attached to them to work, as they are otherwise completely stand-alone pieces of game code. The Script object provides a lot of control for how and then a repeating function should be called, probably more control than the other options here. Furthermore, besides working as a flexible timed event, they function like a traditional in-game object which has it's own persistent set of attributes. The best uses for a Script object I can see, is when you need some isolated bit of code to run at specific intervals, which also contain it's own set of internal data parameters. These parameters can be accessed by other objects as well.
Script objects can also be attached to other objects, and even accounts. This enables the use of a self.obj and self.account to perform actions from the script on the attached object. This can then provide that object with a unique set of characteristics which might not be a part of their main typeclass. This can allow for certain game mechanics for example, to be applied to already existing objects. Although a subclass of a typeclass should always be preferred, this could be a nice alternative, that also includes the ability to perform a function at a given interval. One example could be to say give each in-game NPC a job or task via their attached Script. More than one Script object can also be attached I do believe, so an NPC can be given multiple. This could be used to really add layers to a game world. I'll be researching this some more.
One last thing about this Script objects I only briefly mentioned, is it's flexibility, in that it not only has a interval, and can repeat. It also has the ability to not repeat, and to only repeat a very specific amount of times. The other timed options here do not allow for those without some extra bit of work.
The TickerHandler
This is by far one of the most simplest at first glance repeating timed event... At first glance. Unlike the other options mentioned here, tickers cannot be managed at the moment in-game, they can only be viewed. This means, that in order to remove one, or stop one, you will need to use Python code. This can be a bit annoying, depending on what you are using tickers for, but keep this in mind. Otherwise, it seems that tickers are the most recommended due to their very low performance impact. For events that might occur once every minute, this is probably the timed event to use, if you only need to directly attach it to specific in-game objects. These tickers don't need to be placed onto an object, but by doing so, they become much easier to manage. You should always be sure to use the at_object_delete() event on the object to remove the ticker, if it's placed onto an object. While I've seen an older ticker just disappear after it's object was deleted, I'm not sure what not removing it properly might otherwise do within the game engine code itself, so best remove it before the object is deleted.
The TaskHandler
The TaskHandler is generally used through it's utility function delay. This sets up a one-time event after a specific delay to run. This one seems the least documented, but the included tutorial world does use it for the light source. When the light source is lit, a new task is created to trigger in 3 minutes, which then unlights the light source and deletes it from the game world. I wrote a very basic test object which should be easier to read and understand an example:
from evennia.utils import delayfrom .objects import Objectclass TaskObject(Object): def at_object_creation(self): self.db.desc = "A Task Object" delay(60, self.at_delay) def at_delay(self): self.db.desc = "A Used up Task Object" self.location.msg_contents("Object Task has been run.")
This object gets right to the point, and it's really that easy to use. One last thing to note about Tasks, is that they are not normally persistent by default, but you can pass an option to it to enable it to be persistent. It is also of course possible, or at least I think, to use the delay function from the event to schedule another task. I haven't tested this yet, as for creating a repeat event, it is probably best to use the ticker responsibly.
Conclusion
Each of these can be viewed in-game with the appropriate permissions, and all of them except for the tickers can be managed in-game as well. In terms of in-game control of each, the one with the most flexible control goes to the Script object. Scripts don't need to have an interval, but when they do, have several options for repeating. Furthermore, in-game with the script objects, you can start/stop/pause them at any point, and even run associated event code during those events. Tasks have some similar options, except they cannot be started or stopped, only paused and unpaused. Tickers cannot be managed in-game, you can only view which tickers are currently running.
