Wizards of Lua

The Art of Spell Crafting


Time - Accessing the Time

The Time module provides access to time related properties of the active Spell’s world.

Here is an overview of the Time properties:

Property Type read / write
allowance number (long) r
autosleep boolean r/w
gametime number (long) r
luatime number (long) r
realtime number (long) r

Here is an overview of the Time functions:

Function Parameters Results
 getDate  pattern string
 sleep  ticks nil

Properties

Below you find short descriptions about each of the properties and some examples about how to use them in your spells.


allowance : number (long)

The allowance is the number of lua ticks that are left before the spell or event listener is broken or sent to sleep, depending on autosleep.


autosleep : boolean

The autosleep value defines whether the current spell should go to sleep automatically when its allowance is exceeded.

If this is set to false, the spell will never go to sleep automatically, but instead will be broken when its allowance reaches zero. Default is true normally, but in an event interceptor ‘autosleep’ is always false and can’t be changed.


gametime : number (long)

The gametime is the number of game ticks that have passed since the world has been created.


luatime : number (long)

The luatime is the number of lua ticks that the current spell has worked since it has been casted. This includes lua ticks of event listeners.


realtime : number (long)

The realtime is the number of milliseconds that have passed since January 1st, 1970.


Functions

Below you find short descriptions about each of the functions and some examples about how to use them in your spells.


getDate (pattern) -> string

Returns a string with the current real date and time. If you want you can change the format by providing an optional format string.

Example

Printing the current date and time in ISO date-time format, such as ‘2011-12-03T10:15:30’.

print(Time.getDate())

Example

Printing the current date and time with a custom format, such as ‘03 Oct, 2017 - 10:15:29’.

print(Time.getDate("dd MMM, yyyy - HH:mm:ss"))

sleep (ticks) -> nil

Forces the current spell to sleep for the given number of game ticks.

If the number is 0, the spell won’t sleep. If the number is negative, this function will issue an error. If the number is nil, the spell might go to sleep or not. This depends on the number of lua ticks that are already consumed by this spell.

The rule is as follows: the spell will be sent to sleep if the spell’s allowance falls below the half value of the spell’s initial allowance.

Example

Sending the current spell to sleep for 100 game ticks, which are approximately 5 seconds.

Time.sleep(100)

Since ‘sleep’ is a widely used function, there is a shortcut for it.

sleep(100)