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.

You can find a list of things to observe in the RedScript wiki.

There are two kinds observers:

  • ObserveBefore() which is triggered right at the moment the function/method is called

  • ObserveAfter() 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.

You can now generate Observable and ObservableAfter using NativeDB. You need to configure option Clipboard syntax to Lua. You can click on the "copy" button of a function, pick Copy Observable or Copy ObservableAfter and it will copy the code in your clipboard.

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)

Observe() is an alias of ObserveBefore(). They both work the same.

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:

The self parameter can be renamed at your convenience:

  • _

  • this

  • class

  • whatever

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 applied

  • Using 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