RustTip

OnAIBrainStateSwitch

OxideCarbonNPC

Called when AI brain state switch occurs.

object OnAIBrainStateSwitch(BaseAIBrain baseAIBrain, BaseAIBrain.BasicAIState currentState, BaseAIBrain.BasicAIState newState)
Works — the game method in this build is the one the hook was written for.

Parameters

baseAIBrain
BaseAIBrain
currentState
BaseAIBrain.BasicAIState
newState
BaseAIBrain.BasicAIState

Return value

objectThe returned value replaces the game result; null lets the game decide.

Example

object — you can affect the game: return a value to change or cancel the action.

private object OnAIBrainStateSwitch(BaseAIBrain baseAIBrain, BaseAIBrain.BasicAIState currentState, BaseAIBrain.BasicAIState newState)
{
    Puts("OnAIBrainStateSwitch");

    // change nothing — the game continues as usual
    return null;

    // to affect the game:
    // return false; // your value instead of the game result
}

Works in both Oxide and Carbon: Carbon loads Oxide plugins as is.

Injection point

Rust.opj (Oxide)
bool BaseAIBrain.SwitchToState(BaseAIBrain.BasicAIState, int)

Passes: BaseAIBrain baseAIBrain, BaseAIBrain.BasicAIState currentState, BaseAIBrain.BasicAIState newState

WorksReturnBehavior: ExitWhenNonNullArgumentBehavior: UseArgumentString (this, this.CurrentState, a0)Assembly-CSharp.dll

Where it is called (this branch's game code)

// …
	if (!CurrentState.CanLeave())
	{
		return false;
	}
	if (CurrentState == newState && !UseAIDesign)
	{
		return false;
	}
	object returnvar = Interface.CallHook("OnAIBrainStateSwitch", this, this.CurrentState, newState);
	if (returnvar != null)
	{
		return returnvar is bool && (bool)returnvar;
	}
	CurrentState.StateLeave(this, GetBaseEntity());
}
AddEvents(stateContainerID);
CurrentState = newState;
// …

Sources: Rust.opj from OxideMod/Oxide.Rust (MIT) and hook metadata of the Carbon documentation (MIT). Method code is decompiled from this branch's server.