Skip to main content

on_message

Attribute Macro on_message 

Source
#[on_message]
Expand description

Marks a method as a protobuf message handler.

Use this to handle specific protobuf message types. The message type is inferred from the method’s parameter type, which should be a mapped protobuf message struct.

The method will automatically decode binary message data and call your handler with the decoded message object.

§Parameters

A decoded handler can receive:

  • ctx: &Context (optional)
  • One protobuf message by value or shared reference

A raw handler can instead receive:

  • ctx: &Context (optional)
  • One supported message enum by value or shared reference
  • payload: &[u8]

Parameters can appear in any order. Return ObserverResult.

§Supported Message Types

  • CDotaUserMsgChatMessage and other Dota 2 messages
  • CCitadelUserMsgChatMsg and other Deadlock messages
  • CCsUsrMsgVguiMenu and other CS2 messages
  • Other mapped protobuf message types

§Examples

§Handle Dota 2 chat messages

#[on_message]
fn on_chat(&mut self, ctx: &Context, msg: CDotaUserMsgChatMessage) -> ObserverResult {
    println!("[{}] {}", ctx.tick(), msg.message_text());
    Ok(())
}

§Handle without context

#[on_message]
fn on_chat(&mut self, msg: CDotaUserMsgChatMessage) -> ObserverResult {
    println!("Message: {}", msg.message_text());
    Ok(())
}

§Handle by reference

#[on_message]
fn on_chat(&mut self, msg: &CDotaUserMsgChatMessage) -> ObserverResult {
    println!("Message: {}", msg.message_text());
    Ok(())
}

§Handle raw messages

#[on_message]
fn on_raw(&mut self, msg_type: SvcMessages, payload: &[u8]) -> ObserverResult {
    println!("{msg_type:?}: {} bytes", payload.len());
    Ok(())
}