Rust Wiki

Revision Difference

Hooks#526552

<cat>Dev</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: * When an Entity takes damage * When a Player picks up an item * When a door is opened me⤶ * 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 ⤶ # 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.⤶ ⤶ ## 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.⤶ ⤶ ```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;⤶ }⤶ }⤶ ```⤶ ⤶