Events

Modules and behaviours can receive events, such as when a unit is created, an enemy destroyed, or a command is finished/unit idle, etc. This allows them to listen for game changes.

A behaviour or module does not need to register for events, as long as it is loaded correctly. The game engine is responsible for generating the events, then passing them to the AI, which routes them to where they’re needed.

For example, lets say we have a behaviour with functionality that needs to happen when a unit is damaged. Here’s a cut down version of that behaviour

OnDamagedBehaviour = class(Behaviour)

... other code ...

function OnDamagedBehaviour:UnitDamaged(unit,attacker,damage)
    -- do something
end

If we give this behaviour to a unit, and that unit is damaged, the AI will be told by the engine that a unit has been damaged, and that message will be passed to the unit handler module, then to the behaviours that unit has. `OnDamagedBehaviour` does not have to call `UnitDamaged` or register itself, it happens automatically.

Game Updates

The update function in behaviours and modules is intended to run on every game frame. It should not be ran every time the world is drawn though, but only on every frame of the game simulation. Otherwise, the timings of the AI will become unpredictable, and the sleep module will not be able to guarantee the appropriate delays