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 protobuf message struct.

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

§Parameters

The handler can receive:

  • ctx: &Context (optional) - Access to current replay state
  • Message parameter - The decoded protobuf message (inferred from parameter type)
  • Message can be taken by value or reference

§Supported Message Types

  • CDotaUserMsgChatMessage and other Dota 2 messages
  • CCitadelUserMsgChatMsg and other Deadlock messages
  • CCSUserMessage_* and other CS2 messages
  • Any protobuf message type with a decode method

§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(())
}