#"Failed to call function 'hasTag' at <anonymous>"

1 messages · Page 1 of 1 (latest)

dusky magnet
#

To explain the error in full detail, I have 2 scripts.

  1. Detects an entity being hurt which checks for the damageSource tag using 'hasTag' for player attacks and checks for the damageSource family using 'hasTypeFamily'
world.afterEvents.entityHurt.subscribe(({ hurtEntity, damageSource: { damagingEntity, damagingProjectile, cause } }) => {

  const playerFamily = damagingEntity?.getComponent('minecraft:type_family');
  const projectileFamily = damagingProjectile?.getComponent('minecraft:type_family');

  if(damagingEntity?.hasTag('player_zero') && playerFamily.hasTypeFamily("player") & cause === "entityAttack" && hurtEntity.typeId == 'test:dummy'){
        hurtEntity.runCommand("say I am hit by an ATTACK!");
        
    }

  if(projectileFamily?.hasTypeFamily('custom_projectile') && cause === "projectile" && hurtEntity.typeId == 'test:dummy'){
        hurtEntity.runCommand("say I am hit by a PROJECTILE!");
    }

});
#
  1. A script that fires a projectile using 'itemUse'
world.afterEvents.itemUse.subscribe((event) => {
  const {itemStack,source} = event
  const {x,y,z} = source.getHeadLocation();
  const headLoc = {x:x, y:y+0.3, z:z}
  const viewDir = source.getViewDirection()

if(itemStack.typeId === 'test:custom_pistol'){
        const bullet = source.dimension.spawnEntity("test:bullet", headLoc);
        const projectile = bullet.getComponent("projectile");

        projectile.owner = source;
        projectile.shoot({x: viewDir.x*5, y: viewDir.y*5, z: viewDir.z*5})
      }
});

When I attack the entity while having the correct tag, I trigger the first command.

if(damagingEntity?.hasTag('player_zero') && playerFamily.hasTypeFamily("player") & cause === "entityAttack" && hurtEntity.typeId == 'test:dummy'){
        hurtEntity.runCommand("say I am hit by an ATTACK!");
        
    }

When I throw a projectile at the entity that has the correct familyType, I trigger the other command.

if(projectileFamily?.hasTypeFamily('custom_projectile') && cause === "projectile" && hurtEntity.typeId == 'test:dummy'){
        hurtEntity.runCommand("say I am hit by a PROJECTILE!");
    }

BUT, when I throw an exploding projectile or trigger any explosion that damages the entity. It triggers the error.

When I "fire" a projectile using the custom pistol item (using script 2), even with the projectile having the correct family type. It still triggers the error. Does it have anything to do with myself being the owner?

rapid wraith
dusky magnet
chrome mantle
#

damagingEntity is the projectile...

dusky magnet
#

Hmm..

#

It works well until I actually use the custom gun.

#

I need to test for entity attack and a projectile hit.

#

If I use the custom gun, it registers as an attack instead of a projectile hit I believe.

dusky magnet
chrome mantle
#

display their typeId

#

Gotta watch which is which

dusky magnet
chrome mantle
dusky magnet
#

Perhaps that could cause it?

chrome mantle
#

Hurt event prob triggered 2 times
projectile and explosion
Explosion as the cause and the projectile being the damagingEntity

#

Tricky eh

dusky magnet
#

Truly.

chrome mantle
#

Well, did u see it's typeId?

#

console.warn(damagingEntity?.typeId) somewhere there

dusky magnet
dusky magnet
dusky magnet
chrome mantle
#

Put the cause statement 1st

if (cause === "entityAttack" && ...
#

Same with other if statements, put the cause first

dusky magnet
chrome mantle
#

**?.**hasTypeFamily

#

With that error, just add '**?. **' before the property/method

dusky magnet
chrome mantle
#

Hmmmm

dusky magnet
#

I added a timer within the bullet entity that despawns it after 2 seconds. That way, it gets detected. and it does, so cool.

And gets rid of it.

chrome mantle
#

Nice one

#

I'd actually try map

const projs = new Map()

projs.set(projectile, player)

const source = projs.get(damagingProjectile ?? damagingEntity)
```idk ;-;
dusky magnet
chrome mantle
#

Entity.prototype here is useable as a key cus entity === source can be used

#

Well, that's how objects in js behaves btw

dusky magnet
#

Hmm..