You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GM:OnNPCKilled()失效:如何将NPC击杀者设为玩家而非子弹

解决GM:OnNPCKilled()中击杀者为子弹而非玩家的问题

问题核心是创建SWEP子弹时未将子弹的攻击者关联到开枪玩家,导致OnNPCKilled触发时attacker参数指向子弹实体而非预期的玩家。

步骤1:修改子弹创建代码,关联玩家攻击者

在子弹参数表中添加Bullet.Attacker = player,让子弹将开枪的玩家标记为攻击者:

local Bullet = {}
Bullet.Num = self.Primary.NumShots
Bullet.Src = player:GetShootPos()
Bullet.Dir = player:GetAimVector()
Bullet.Spread = Vector(self.Primary.Spread, self.Primary.Spread, 0)
Bullet.Tracer = 0
Bullet.Damage = self.Primary.Damage
Bullet.AmmoType = self.Primary.Ammo
Bullet.Attacker = player -- 添加该行,将玩家设为子弹的攻击者

步骤2:修正OnNPCKilled逻辑,使用正确的玩家对象

原代码用inflictor(子弹实体)执行给弹药操作,现在改为用attacker(关联的玩家),同时增加类型判断避免非玩家实体触发错误:

function GM:OnNPCKilled(npc, attacker, inflictor)
    -- 先验证攻击者是否为玩家实体
    if attacker:IsPlayer() then
        attacker:GiveAmmo(1, 13, true)
        print(attacker:GetAmmoCount(13))
    end
end

设置完成后,子弹击杀NPC时,OnNPCKilled的attacker参数会正确指向开枪玩家,即可正常实现击杀NPC给玩家发弹药的功能。

内容的提问来源于stack exchange,提问作者Vladimir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 13:07:46