Package openmw_aux.utilΒΆ

openmw_aux.util defines utility functions that are implemented in Lua rather than in C++.

Implementation can be found in resources/vfs/openmw_aux/util.lua.

Usage:

local aux_util = require('openmw_aux.util')

Type util

util.findNearestTo(point, objectList)

Finds the nearest object to the given point in the given list.

util.runEveryNHours(N, fn, initialDelay)

Runs given function every N game hours.

util.runEveryNSeconds(N, fn, initialDelay)

Runs given function every N game seconds (seconds when the game is not paused).

Type util

Field(s)

util.findNearestTo(point, objectList)

Finds the nearest object to the given point in the given list.

Ignores cells, uses only coordinates. Returns the nearest object, and the distance to it. If objectList is empty, returns nil.

Parameters

Return value

openmw.core#GameObject, #number: the nearest object and the distance

util.runEveryNHours(N, fn, initialDelay)

Runs given function every N game hours.

Note that loading a save stops the evaluation. If it should work always, call it in 2 places -- when a script starts and in the engine handler onLoad.

Parameters

  • #number N : interval in game hours

  • #function fn : the function that should be called every N game hours

  • #number initialDelay : optional argument -- delay in game hours before the first call. If missed then the delay is a random number in range [0, N]. Randomization is used for performance reasons -- to prevent all scripts from doing time consuming operations at the same time.

Return value

#function: a function without arguments that can be used to stop the periodical evaluation.

Usage:

local timeBeforeMidnight = 24 - math.fmod(core.getGameTimeInHours(), 24)
aux_util.runEveryNHours(24, doSomething, timeBeforeMidnight)  -- call `doSomething` at the end of every game day.
util.runEveryNSeconds(N, fn, initialDelay)

Runs given function every N game seconds (seconds when the game is not paused).

Note that loading a save stops the evaluation. If it should work always, call it in 2 places -- when a script starts and in the engine handler onLoad.

Parameters

  • #number N : interval in seconds

  • #function fn : the function that should be called every N seconds

  • #number initialDelay : optional argument -- delay in seconds before the first call. If missed then the delay is a random number in range [0, N]. Randomization is used for performance reasons -- to prevent all scripts from doing time consuming operations at the same time.

Return value

#function: a function without arguments that can be used to stop the periodical evaluation.

Usage:

local stopFn = aux_util.runEveryNSeconds(5, function() print('Test') end)  -- print 'Test' every 5 seconds
stopFn()  -- stop printing 'Test'
aux_util.runEveryNSeconds(5, function() print('Test2') end, 1)  -- print 'Test' every 5 seconds starting from the next second