It is generally advisable to useObserve
whenever possible, and only use Override
when absolutely necessary
Override is a built-in CET function allowing developers to rewrite a game's class method. It must be registered using the Override()
function inside the onInit event, in the init.lua
file.
The provided callback
is always filled as follows:
The first argument is the current object that execute the method. This argument is not present when function/method is static.
Others arguments passed to the targeted method, if any.
The last argument is the original callable method.
You can now generate Override using NativeDB. You need to configure option Clipboard syntax to Lua. You can click on the "copy" button of a function, pick Copy Override and it will copy the code in your clipboard.
Considering the following class and method:
When applying Override()
, the callback would be filled with the following arguments:
The self
and wrappedMethod
parameters can be renamed at your convenience:
_
this
class
method
whatever
The last argument of the Override()
function, wrappedMethod
, contains the original method as a callable function. We can use it to execute the original code and/or retrieve its result (if it returns something).
It is highly recommended to know the overriden method return statement to avoid breaking the script, and use wrappedMethod
accordingly.
Any method declared void doesn't return a value. Overriding it follow the same logic:
The same logic is applied to methods with a specific return statement. Overriding it must return a compatible type:
It is not required to execute the original code using wrappedMethod
. This can be omitted, but it exposes the script to malfunctions if not handled properly.
Do not allow the player to crouch:
Do not allow the player to crouch if in ADS:
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 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.
Observe()
is an alias of ObserveBefore()
. They both work the same.
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
Cyber Engine Tweaks allow mods to listen or overwrite game's script functions/methods. There are three kinds of functions:
Observe allows to listen for function/method execution
Override allows to overwrite a function/method
NewProxy allows to trigger a function/method callback using game's script system
NewProxy is a built-in CET feature you can use to declare callback functions. You can use it to bind a Lua function as a callback with game's script system.
CallbackName is an arbitrary name you define. A callback name can be formatted as follow On[Action]
(e.g. OnDamaged
or also OnPlayerDamaged
).
CallbackDefinition is the signature of the script function (args) and the Lua function (callback) you want to be executed when the callback is triggered.
The list of arguments must indicate the name of the types to expect. For example with a callback function which receives a String
, an Int32
and a reference to a GameObject
, it should be defined like this:
In this case, you can define your function callback like this:
The signature of the function depends on the game's script function you want to register a callback for.
Lets create a proxy:
After creating the proxy, you can use it to pass the target and function you want to callback. Lets say a game's script is defined as:
We can call the function RegisterHit
to register our callback with our proxy like this:
Note that the value, when calling listener:Function("OnHit")
, is the same we declared in the proxy.
This way, you can create multiple callback in a proxy and you just need to call listener:Function
with the name of the callback you want to use. For example:
You can now generate NewProxy using . You need to configure option Clipboard syntax to Lua. You can click on the "copy" button of a function, pick Copy NewProxy and it will copy the code in your clipboard. It only works for classes with their names ending with Listener.
You can use to know the types of arguments to declare. By default, the syntax will be written in Redscript. You can change the option Code syntax
in the settings and select Pseudocode · Legacy
instead. Basically, it will show you handle:GameObject
instead of ref<GameObject>
(among other things).
This example will be using and its system to listen for . It will listen for the event Session/Ready
and print a message in CET logs.