Skip to main content

Context

Struct Context 

Source
pub struct Context { /* private fields */ }
Expand description

Current replay state accessible to observers.

The context is passed to all observer callbacks and provides access to:

  • Entity state and properties
  • String tables
  • Game event definitions
  • Current tick and timing information
  • Replay metadata

§Examples

§Accessing entities

use source2_demo::prelude::*;

#[derive(Default)]
struct HeroStats;

#[observer]
#[uses_entities]
impl HeroStats {
    #[on_tick_start]
    fn on_tick_start(&mut self, ctx: &Context) -> ObserverResult {
        for entity in ctx.entities().iter() {
            if entity.class().name().starts_with("CDOTA_Unit_Hero_") {
                let health: i32 = property!(entity, "m_iHealth");
                println!("{}: {}", entity.class().name(), health);
            }
        }
        Ok(())
    }
}

§Accessing string tables

use source2_demo::prelude::*;

#[derive(Default)]
struct TableReader;

#[observer]
#[uses_string_tables]
impl TableReader {
    #[on_tick_start]
    fn on_tick_start(&mut self, ctx: &Context) -> ObserverResult {
        if let Ok(table) = ctx.string_tables().get_by_name("ActiveModifiers") {
            println!("Active modifiers: {}", table.iter().count());
        }
        Ok(())
    }
}

Implementations§

Source§

impl Context

Source

pub fn classes(&self) -> &Classes

Returns a reference to the entity container.

Use this to access all entities in the game and query their properties.

§Examples
use source2_demo::prelude::*;

// Get entity by index
let entity = ctx.entities().get_by_index(0)?;

// Find entity by class name
let player_resource = ctx.entities().get_by_class_name("CDOTA_PlayerResource")?;

// Iterate all entities
for entity in ctx.entities().iter() {
    println!("{}", entity.class().name());
}
Source

pub fn entities(&self) -> &Entities

Returns a reference to the entity container.

Provides access to all game entities and their properties. Requires Interests::ENTITY_STATE to be populated.

Source

pub fn string_tables(&self) -> &StringTables

Returns a reference to the string tables.

String tables contain game data like hero names, item names, etc. Requires Interests::STRING_TABLE_STATE to be populated.

Source

pub fn game_events(&self) -> &GameEventList

Returns a reference to the game event list.

Contains definitions for all game events in the replay.

Source

pub fn tick(&self) -> u32

Returns the current tick number.

The tick represents the current game simulation step. Typically runs at 30 ticks per second.

Source

pub fn previous_tick(&self) -> u32

Returns the previous tick number.

This is the last tick observed before the current tick advanced.

Source

pub fn net_tick(&self) -> u32

Returns the current network tick.

The network tick from the last processed packet.

Source

pub fn game_build(&self) -> u32

Returns the game build number.

Identifies the specific version of the game that created this replay.

Source

pub fn replay_info(&self) -> &CDemoFileInfo

Returns replay file metadata.

Contains information about the replay including duration, map, and game info.

Source

pub fn baseline_entities(&self) -> Vec<BaselineEntity>

Returns the baseline entity classes currently known to the parser.

Baselines are keyed by entity class id rather than live entity index. This method is primarily useful for replay inspection and debugging tools.

Source

pub fn baseline_fields(&self, class_id: i32) -> Option<Vec<EntityField<'_>>>

Returns all fields for a baseline entity class id.

Returns None if the baseline or matching class serializer is not present in the current parser context.

Trait Implementations§

Source§

impl Default for Context

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.