Rust Wiki

Hooks

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 Oxide 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 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 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.

object OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info) { Puts("OnEntityTakeDamage works!"); return null; }

Now all we need to do is remove the default code and put in our code.

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; } }