Scaling damage?

Discussion in 'Rust Development' started by Pyronix, Mar 2, 2017.

  1. I'm trying to figure out why I'm getting a NullReferenceException with this code.

    Code:
    void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo hitInfo)
            {
                DamageReceiver receiver = config.forName(hitInfo.HitEntity.ShortPrefabName);
                hitInfo.damageTypes.ScaleAll(receiver.mod);
            }
    Please assume receiver.mod is appropriately providing a float. Also, there's logic to prevent it from triggering on anything other than the intended entities.

    It works when I use weapons other than C4... I thought maybe it was because the item was destroyed instantly, so I used something that would survive (high wall) and I still get the NRE. Can someone help explain this?
     
    Last edited by a moderator: Mar 2, 2017
  2. Have you tried adding some debug output to verify it is finding your DamageReceiver? Might also be interesting to check the log file to get the complete error with the stacktrace
     
  3. Yes, that was my first guess. I had a plethora of read outs confirming intent, and did a lot of testing to make sure the problem only occurred with explosives.

    I was reading a thread earlier in which someone had a relative question and the answer was the HitInfo was not a component for Traps, and thus presented an NRE. Think it might be related?

    Also, I'm brand new to the oxide framework... where might I find that log file? That sounds extremely helpful.

    Edit:
    Found the log... I'm starting to run low on sleep, but it doesn't appear terribly helpful at first glance. Any idea why it shows <filename unknown>:0? I'm working through Visual Studio, but I push the .cs file to the server manually.

    I planned on making this plugin public to help someone in the plugin requests section ("Secured Loot" was the name of the thread I believe), so I'd be happy to share if you're intrigued.

    Code:
    01:01 [Error] Failed to call hook 'OnEntityTakeDamage' on plugin 'DCDeployables v1.0.0' (NullReferenceException: Object reference not set to an instance of an object)
    01:01 [Stacktrace]   at Oxide.Plugins.DCDeployables.OnEntityTakeDamage (.BaseCombatEntity entity, .HitInfo hitInfo) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.DCDeployables.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 
     
    Last edited by a moderator: Mar 2, 2017
  4. You need to add some null checks to your code. It's better to have a few "extra" null checks than too few. I would check if HitInfo, HitInfo.HitEntity, and HitInfo.damageTypes are all non-null before doing anything else. But from the context I suspect HitInfo.HitEntity is your culprit, since c4 does damage to all entities within in its blast radius as opposed to most other weapons which are focused on one entity.
     
  5. Also, getting the entity from HitInfo is unnecessary - the damaged entity is passed as the BaseCombatEntity entity parameter.