Solved OnEntityTakeDamage NRE

Discussion in 'Rust Development' started by SwipoStyle, Sep 3, 2015.

  1. Code:
    OnEntityTakeDamage failed: Object reference not set to an instance of an object
    Code:
    void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info)
    {
     try {
      if(entity is BasePlayer && info.Initiator is BasePlayer) {
       OnAttackShared(info.Initiator as BasePlayer, entity as BasePlayer, info);
      }
     } catch(Exception ex) {
      PrintError("OnEntityTakeDamage failed: " + ex.Message);
     }
    }
    
    It is copied from FriendlyFire.cs (RustIO), problems shan't be, but they are.
     
  2. What about
    Code:
    void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info)
    {
        BasePlayer player = (BasePlayer) entity.ToPlayer();
        BasePlayer victim = (BasePlayer) info.Initiator;
    }
    
    ? :x
     
  3. Code:
    void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info)
     {
     try {
     if(entity is BasePlayer && info.Initiator is BasePlayer) {
     BasePlayer player = (BasePlayer) entity.ToPlayer();
     BasePlayer victim = (BasePlayer) info.Initiator;
     OnAttackShared(player, victim, info);
     }
     } catch(Exception ex) {
     PrintError("OnEntityTakeDamage failed: " + ex.Message);
     }
     }
    
    No :-(
     
  4. Replace the if line with
    Code:
    if(info.HitEntity != null && info.HitEntity.ToPlayer() != null)
    
     
  5. No change
     
  6. check for everything.
    Code:
    if(entity != null && entity.ToPlayer() != null && info != null && info.Initiator != null && info.Initiator.ToPlayer() != null)
    {
        BasePlayer victim = entity.ToPlayer();
        BasePlayer attacker = info.Initiator.ToPlayer();
    }
     
  7. Did not help
     
  8. Code:
    try
    {
        if(entity !=null&& entity.ToPlayer()!=null&& info !=null&& info.Initiator!=null&& info.Initiator.ToPlayer()!=null)
        {
            BasePlayer victim = entity.ToPlayer();
            BasePlayer attacker = info.Initiator.ToPlayer();
        }
    }
    catch(Exception ex)
    {Puts("Failed! Error: " + ex.ToString())}
     
  9. Code:
    OnEntityTakeDamage failed: System.NullReferenceException: Object reference not set to an instance of an object
      at Oxide.Plugins.Play4Free.OnAttackShared (.BasePlayer attacker, .BasePlayer victim, .HitInfo info) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.Play4Free.OnEntityTakeDamage (.BaseCombatEntity entity, .HitInfo info) [0x00000] in <filename unknown>:0
    
     
  10. The error is in your "OnAttackShared" method, maybe show me that one
     
  11. Code:
    private void OnAttackShared(BasePlayer attacker, BasePlayer victim, HitInfo info)
    {
    if(attacker == victim) return;
      string bodypart = StringPool.Get(info.HitBone);
    if(Config["Bodyparts", bodypart] != null)
      bodypart = Config["Bodyparts", bodypart].ToString();
    else {
      Config["Bodyparts", bodypart] = bodypart;
      SaveConfig();
    }
    int distance = Convert.ToInt32(Vector3.Distance(victim.transform.position, attacker.transform.position));
    PopupNotifications?.Call("CreatePopupNotification", "Попадание!\nИгрок: <color=#1e90ffff>"+victim.displayName+"</color>\nЧасть тела: <color=#1e90ffff>"+bodypart+"</color>\nРасстояние: <color=#1e90ffff>"+distance+"м</color>", attacker);
    }
    
    [DOUBLEPOST=1441290505][/DOUBLEPOST]I am sorry. Problem solved.
    Configuration file was not loaded.