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
Anyone have any idea on why this might be?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; } } }
Returning false still cancels OnPlayerAttack
Discussion in 'Rust Development' started by The FSM, Jun 4, 2016.
-
Are you trying to cancel damage between team mates, and before the game has started?
-
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.
-
I usually just modify the HitInfo to reflect what ever changes I need
Also use OnPlayerAttack as void. Here is a example using your codeCode:// Nullify damage completely info.damageTypes = new DamageTypeList(); info.DoHitEffects = false;// Scale damage info.damageTypes.ScaleAll(0f); // 1.0f is normal, 0 is nothing
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 } -
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! -
I just happened to be browsing at the right time
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 -
Wulf Community Admin
Returning anything other than null in the hook cancels it. The Docs say true just to set a standard.
-
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 :/
-
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)
-
I use OnEntityTakeDamage as object and return true to cancel and null to let it pass through
-
You need to do your OnPlayerAttack like this
Code:bool? OnPlayerAttack(BasePlayer attacker, HitInfo info) { // Can return null, true, or false } -
I should probably have updated this thread by now. I fixed the issue a while ago by using
@Dyceman Would there be any benefits to using bool? instead of object? Just curious.Code:object OnPlayerAttack(BasePlayer attacker, HitInfo info) { // Can return null, true, or false } -
Wulf Community Admin
It's an object hook, so not really. You can use a bool, but that would require always returning true/false. -
@Wulf Thanks for the quick reply!
-
bool? allows you to use true, false, and null as returns
-
Wulf Community Admin
That'd be the same as object then.
