1. Hello!

    I have a weird issue, according to the documentation returning false will still allow the attack to happen. In my case it doesn't. Here is my code
    Code:
    object OnPlayerAttack(BasePlayer attacker, HitInfo info)
            {
                if (!hasgamestarted)
                {
                    //if the game hasn't started block attack
                    rust.SendChatMessage(attacker, "[Embankment]", " Haha you can't hurt anything because the game hasn't started!");
                    return true;
                }
                else
                {
                    if (info.HitEntity is BasePlayer)
                    {
                        BasePlayer victim = (BasePlayer)info.HitEntity;
                        if((team1.Contains(victim) && team1.Contains(attacker)) || (team2.Contains(victim) && team2.Contains(attacker)))
                        {
                            rust.SendChatMessage(attacker, "[Embankment]", " You can't hurt anyone on the same team as you");
                            return true;
                        }else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
    Anyone have any idea on why this might be?
     
    Last edited by a moderator: Jun 4, 2016
  2. Are you trying to cancel damage between team mates, and before the game has started?
     
  3. Before yes, the game starts and it still does 0 damage. You can't even gather resources. I tested it by putting puts("test") right before the last return false and it shows up.
     
  4. I usually just modify the HitInfo to reflect what ever changes I need
    Code:
    // Nullify damage completely
    info.damageTypes = new DamageTypeList();
    info.DoHitEffects = false;// Scale damage
    info.damageTypes.ScaleAll(0f); // 1.0f is normal, 0 is nothing
    Also use OnPlayerAttack as void. Here is a example using your code

    Code:
    void OnPlayerAttack(BasePlayer attacker, HitInfo info)
            {
                if (!hasgamestarted)
                {
                    //if the game hasn't started block attack
                    hitinfo.damageTypes.ScaleAll(0); // Scale damage to nothing
                    rust.SendChatMessage(attacker, "[Embankment]", " Haha you can't hurt anything because the game hasn't started!");
                    return;
                }
                else
                {
                    if (info.HitEntity is BasePlayer)
                    {
                        BasePlayer victim = (BasePlayer)info.HitEntity;
                        if ((team1.Contains(victim) && team1.Contains(attacker)) || (team2.Contains(victim) && team2.Contains(attacker)))
                        {
                            hitinfo.damageTypes.ScaleAll(0); // Scale damage to nothing
                            rust.SendChatMessage(attacker, "[Embankment]", " You can't hurt anyone on the same team as you");
                            return;
                        }
                    }
                }   
                // Didn't meet any requirements so continue with dealing damage
            }
     
  5. That does seem like a much better solution. I'll give it a shot tomorrow morning, I'm in bed right now (I didn't expect to get such a fast response).

    I appreciate your help and will confirm that works.

    Any idea on why my way might not work? Documentation might just be out of date?

    Thanks!
     
  6. I just happened to be browsing at the right time :p Failing that you could also use OnEntityTakeDamage. Unsure about the OnPlayerAttack hook with out looking at the assembly, I have never tried to return anything to it before
     
  7. Wulf

    Wulf Community Admin

    Returning anything other than null in the hook cancels it. The Docs say true just to set a standard.
     
  8. Gotcha, It looks like I am going to need to change this up as hitinfo.damageTypes(ScaleAll(0) doesn't stop it from doing damage :/
     
  9. OnPlayerAttack is triggered before the damage is calculated so you need to cancel the normal behavior or use a different hook (OnEntityTakeDamage) to scale the damage to 0.
    Code:
    void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info)
     
  10. I use OnEntityTakeDamage as object and return true to cancel and null to let it pass through
     
  11. You need to do your OnPlayerAttack like this

    Code:
    bool? OnPlayerAttack(BasePlayer attacker, HitInfo info)
    {
       // Can return null, true, or false
    }
     
  12. I should probably have updated this thread by now. I fixed the issue a while ago by using
    Code:
    object OnPlayerAttack(BasePlayer attacker, HitInfo info)
    {
       // Can return null, true, or false
    }
    @Dyceman Would there be any benefits to using bool? instead of object? Just curious.
     
  13. Wulf

    Wulf Community Admin

    It's an object hook, so not really. You can use a bool, but that would require always returning true/false.
     
  14. @Wulf Thanks for the quick reply!
     
  15. bool? allows you to use true, false, and null as returns
     
  16. Wulf

    Wulf Community Admin

    That'd be the same as object then. ;)