saul / demofile

Node.js library for parsing Counter-Strike: Global Offensive demo files

Home Page:https://demofile.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Different entities when listening on `create` vs `postcreate`

thecatontheflat opened this issue · comments

There is a difference in entities when listening to create vs postcreate events. I've noticed it on various demos, but I have recorded a small one to isolate the case, in which I consequently throw 3 grenades:

  1. weapon_hegrenade
  2. weapon_flashbang
  3. weapon_smokegrenade

If I do this (listen to the create):

    this.demoFile.entities.on('create', (e) => {
      if (!('DT_BaseCSGrenadeProjectile' in e.entity.props)) return;

      const projectileEntity = (e.entity as unknown) as BaseEntity<CBaseCSGrenadeProjectile>;
      const thrower = projectileEntity.owner as Player;

      console.log('%s threw %s at:', thrower ? thrower.name : '(someone)', projectileEntity.modelName, projectileEntity.position);
    });

it outputs the following data, which is wrong:

d0h threw models/Weapons/w_eq_fraggrenade_dropped.mdl at: { x: 341.6875, y: 2492.34375, z: -53.21875 }
d0h threw models/Weapons/w_eq_fraggrenade_dropped.mdl at: { x: 341.6875, y: 2492.34375, z: -53.21875 }
d0h threw models/Weapons/w_eq_smokegrenade_thrown.mdl at: { x: 330.15625, y: 2484.28125, z: -52.78125 }

When I listen on postcreate with the same code

    this.demoFile.entities.on('postcreate', (e) => {
      if (!('DT_BaseCSGrenadeProjectile' in e.entity.props)) return;

      const projectileEntity = (e.entity as unknown) as BaseEntity<CBaseCSGrenadeProjectile>;
      const thrower = projectileEntity.owner as Player;

      console.log('%s threw %s at:', thrower ? thrower.name : '(someone)', projectileEntity.modelName, projectileEntity.position);
    });

the output is correct:

d0h threw models/Weapons/w_eq_fraggrenade_dropped.mdl at: { x: 341.6875, y: 2492.34375, z: -53.21875 }
d0h threw models/Weapons/w_eq_flashbang_dropped.mdl at: { x: 316.5, y: 2493.21875, z: -47.84375 }
d0h threw models/Weapons/w_eq_smokegrenade_thrown.mdl at: { x: 330.15625, y: 2484.28125, z: -52.78125 }

Could you please help me understand the underlying issue? Is it due to the nature of the sequence of the events being recorded in the demo, or is it a bug in the parser? If it's the latter, could you point me in the right direction so I could try to submit a fix for it?

nades.dem.zip

This isn't a bug, see the documentation for the create event: https://github.com/saul/demofile/blob/master/src/entities.ts#L228-L234. Specifically "Note no entity properties are available yet."

This is because when the entity is created, the props are all initially set to the 'baseline' (usually the same as an entity that used that index previously). postcreate is fired when the entity's actual props are read from the demo.

I may get rid of the create/postcreate split as it doesn't really serve any useful purpose. (I'd get rid of create and rename postcreate to create instead.)

Ah, understood.

Personally I think that switching them around would make more sense, as this is what I intuitively expected when hooking to create event.

Thanks for the clarification!

This will be released as part of v2.1.0