Observe
Observers are builtin CET functions that allow developers to detect when a function/method is executed by the game. They must be registered inside the onInit event, in the init.lua file.
There are two kinds observers:
ObserveBefore()which is triggered right at the moment the function/method is calledObserveAfter()which is triggered once the game finished to execute the function/method
The provided callback is always filled as follow:
The first argument is the current object that execute the function/method. This argument is not present when function/method is static.
Others arguments passed to the targeted function/method, if any.
Definition
Observe(className, method, callback) -- alias of ObserveBefore()ObserveBefore(className, method, callback)ObserveAfter(className, method, callback)--
-- ObserveBefore()
--
-- @param string className The parent class name
-- @param string method The method name to target
-- @param function callback The callback function
--
ObserveBefore('className', 'method', function(self [, arg1, arg2, ...])
-- method() has just been called
end)Representation
Here is a visual representation showing where observers are executed in the game's script:
In this representation, the observer callback would be filled with the following arguments:
In the code above, the observer listens to AimingStateEvents:OnEnter(), which is triggered everytime the player enters in ADS state.
Additionally, the OnEnter() method is responsible to apply different effects, like the camera zoom, the initial stamina drain etc... Following this logic, this means:
Using
ObserveBefore()allows to hook in before these effects are appliedUsing
ObserveAfter()guarantees the method finished to apply all the effects
Usage Examples
Give money each time the player crouches:
Give money as long as the player is crouched:
Observe event send to an AnimationControllerComponent (click on function's name in NativeDB to copy the full name in your clipboard):
Advanced Example
Give money when the player is crouched and in ADS:
Last updated