1. Hello!,

    I've been trying to create logging for certain things on my own, by editing the plugin "Logger" made by Wulf. One of which includes logging when a player is downed, with information regarding who did it as well. But i seem to come into a problem, i don't know why it happens, i'm probably doing something wrong.

    Code:
            void OnPlayerWound(BasePlayer victim, BasePlayer attacker)
            {
                Puts(victim.displayName + " Downed by " + attacker.displayName);
            }
    Gives this error:

    Code:
    Failed to call hook 'OnPlayerWound' on plugin 'Logger v2.0.3' (NullReferenceException: Object reference not set to an instance of an object)
      at Oxide.Plugins.Logger.OnPlayerWound (.BasePlayer victim, .BasePlayer attacker) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.Logger.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.CSharpPlugin.InvokeMethod (HookMethod method, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Core.Plugins.Plugin.CallHook (System.String hook, System.Object[] args) [0x00000] in <filename unknown>:0
    I would appreciate any help i get :)
     
  2. Wulf

    Wulf Community Admin

    I wouldn't assume that the attacker is always a player, or that they always have a displayName. Null checks.
     
  3. So should i check with "BaseEntity" ?
     
  4. Wulf

    Wulf Community Admin

    No, the 2nd argument you are adding to the hook doesn't exist. See Oxide API for Rust.

    Basically the error is telling you that the "attacker" is null (hence the NullReferenceException), so you can't use that like that. If you want to log attacks, use OnEntityTakeDamage (also in the Docs) or OnPlayerAttack.
     
  5. Alright, so there is no way to get the player who wounds someone? Also where might i find these docs?
     
  6. The big “docs” button at the top of the website
     
  7. Wulf

    Wulf Community Admin

    The other hooks I mentioned would be what you're looking for.
     
  8. :| I'm blind, haha.

    Alright, yeah i figured that out moments ago. Thanks for the help guy's!
     
  9. Hello,

    As to not make a new thread as this is in regards of what i asked before, i will continue here.

    I have managed to log when people shot people, with damage and weapons, so thanks again for the help regarding that! Now i created logging for kills too, which works very well, but one bug and another "issue".

    This is for when a player dies

    Code:
            void OnPlayerDie(BaseEntity entity, HitInfo info)
            {
                var target = entity as BasePlayer;
                var attacker = info.Initiator as BasePlayer;
                if (target == null) return;
                if (attacker == null) return;            if (attacker == target)
                {
                    if (logDamage) Log("damage", Lang(target + " Killed himself"));
                    return;
                }            if (logDamage) Log("damage", Lang(attacker + "" + attacker.transform.position + "" + " has killed " + target + "" + target.transform.position + "" + " using " + info.Weapon));
               
            }
    So the problem is that when someone is downed and that player dies, it gives this error:

    Code:
    Failed to call hook 'OnPlayerDie' on plugin 'Logger v2.0.4' (NullReferenceException: Object reference not set to an instance of an object)
      at Oxide.Plugins.Logger.OnPlayerDie (.BaseEntity entity, .HitInfo info) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.Logger.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.CSharpPlugin.InvokeMethod (HookMethod method, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Core.Plugins.Plugin.CallHook (System.String hook, System.Object[] args) [0x00000] in <filename unknown>:0
    Now i know that it's not getting what it's needed to call something properly, but i have no idea how to fix it.


    Now to the other "issue", i want to log when an animal kills a player, or when a player kills and animal. I know i am calling things under "BasePlayer" I've tried other things but don't seem to get it to work.

    I would appreciate any help!
     
  10. Wulf

    Wulf Community Admin

    Null checks! Don't assume that info.Initiator is always there, so that's the first thing that needs a null check. You should also have one with *.transform.position, because they may not always have a position.

    An animal would be info.Initiator as BaseEntity.
     
  11. Your HitInfo is null, you can fix this via multiple ways.

    1 easy way is to add a ? to info.Initiator so info?.Initiator, the question mark basically makes the whole value null so it’d result as null in your check. What it’s doing now is trying to get a value from “info” but it’s null.

    2nd easy way is just to add a if(info == null) return; before anything at the top of the method.

    Presumably the player died from beig wounded too long. You could store the last attacker when the player is wounded in OnEntityTakeDamage. You can take a look into DeathNotes to see how this has been applied to work there.
     
  12. Thanks for the help guys! I'll make sure to look into deathnotes as tho how it's done there. I had put in "if (info == null)" but i hadn't put it first.