Rust Wiki

Revision Difference

Hooks#526800

<cat>Dev.Modding</cat> <title>Hooks</title> # What Is It A `Hook` is a way for modding frameworks to call your mod's code every time a certain action happens. Some Hooks Are: * If an Entity takes damage * After a Player picks up an item * If a locked door can be opened * After a player pushes a boat * After a player wakes up * Before a player respawns Some hooks control if an action should happen, some trigger after an action happens and some will do both. # How Does It Work A modding framework such as <page>Oxide</page> will insert their special `CallHook` functions throughout the Rust Server code. A `CallHook` function will run any code in your plugin that: * Matches the name of the hook * Has the same parameters as the hook # How Do I Use Them Go to the [Oxide Documentation](https://umod.org/documentation/games/rust) page to see a list of every single hook. ##First figure out what you want to do. ___ For this example we are going to prevent players from damaging boats. ##Next we find a hook that relates to what we are trying to do. ___ We chose the hook `OnEntityTakeDamage` occurs when damage is dealt and can block it. We chose the hook `OnEntityTakeDamage` as it occurs when damage is dealt but we can block it! ## Now We Write The Code ___ First we copy the hook found in the documentation into our plugin. ```csharp object OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info) { Puts("OnEntityTakeDamage works!"); return null; } ``` Now all we do is remove the default code and put in our code. Now all we need to do is remove the default code and put in our code. ```csharp object OnEntityTakeDamage(BaseCombatEntity hitEntity, HitInfo info) { //We only want to block damage when players attack the boats //Skip if the attacking player doesn't exist if ( info.InitiatorPlayer == null ) { //Allow damage return null; } //We only want to prevent damage if the entity attacked is a boat if ( hitEntity is MotorRowboat ) { //Prevent damage return false; } else { //Allow damage to anything other than boats return null; } } ```