Hi!
I am looking to get when a helicopter has despawned or "retired" when leaving the map so I can send information to console or a player.
So far I cannot figure anything out from looking at Assembly-CSharp. Any ideas?
Cheers!
Solved Detecting when helicopter despawns/retires?
Discussion in 'Rust Development' started by JoeSheep, Mar 11, 2017.
-
Code:
private void OnEntityDeath(BaseCombatEntity entity, HitInfo info) { if (entity is BaseHelicopter) { Puts("Heli death msg to console"); rust.BroadcastChat("Heli death msg to players"); } }
-
Unfortunately with the way the helicopter is disposed of it doesn't trigger that hook:
Code:public void Retire() { if (this.isRetiring) return; this.isRetiring = true; this.Invoke("DestroyMe", 240f); float num1 = TerrainMeta.Size.x; float num2 = 200f; Vector3 newPos = Vector3Ex.Range(-1f, 1f); newPos.y = 0.0f; newPos.Normalize(); newPos *= num1 * 20f; newPos.y = num2; this.ExitCurrentState(); this.State_Move_Enter(newPos); } public void DestroyMe() { this.helicopterBase.Kill(BaseNetworkable.DestroyMode.None); }
-
Yes it does, it just takes awhile. It takes about 2 to 5 mins for it to despawn after it starts flying away.
-
Code:void OnEntityDeath(BaseCombatEntity entity, HitInfo info) { if (helicopterDestroyedAnnouncement && entity is BaseHelicopter) { if (helicopterDestroyedAnnouncementWithDestroyer) { CreateAnnouncement(helicopterDestroyedAnnouncementWithDestroyerText.Replace("{playername}", LastHitPlayer), BannerTintRed, TextWhite); LastHitPlayer = String.Empty; } else { CreateAnnouncement(helicopterDestroyedAnnouncementText, BannerTintRed, TextWhite); } .... } }
Last edited by a moderator: Mar 11, 2017 -
Hey I am really sorry, you are absolutely right. I just graded some code out of the heli plugin I wrote awhile ago and didn't remember I ran into the same problem. Looks like my work around was to store the heli BaseHelicopter into a var and check the var with a timer every second until it equals null.
-
I was just looking at InfoPanel and it seems to do it a rather complicated way with extra classes and events. I don't understand yet exactly how it works, so I can't tell which way would be the most efficient yet (in terms of performance). -
My plugin already has to have the one second timer so it was a ok solution for me. There probably is a more efficient way, you should post what you find.
-
Couldn't you use OnEntityKill?
-
EDIT: Yep just looked through the Oxide Git repo and found that. Worth testing. -
It's not in the docs (they're out of date), but it gets called when a BaseNetworkable has Kill called. It should work for both heli being shot down and heli despawning
[DOUBLEPOST=1489273324][/DOUBLEPOST]Code:void OnEntityKill(BaseNetworkable entity) { if (entity is BaseHelicopter) Puts("Heli is gone :("); }
-
EDIT: I can't think of anything... Maybe a timer from the last time it was damaged? If it despawns and wasn't damaged after say one minute we can safely assume it retired? Unless you use HeliControl to make it keep targeting you even when it is supposed to be leaving, that way it can just despawn in front of you. Concerned of the performance of constantly destroying and recreating a timer everytime the helicopter takes damage.Last edited by a moderator: Mar 12, 2017 -
-
-
I was just being factual OnEntityKill does get called for both, but is not necessarily the best to use for both events. Like Shady said, you could use OnEntityDeath to handle shot down, then you could just grab the net ID, toss it in a list, and ignore it on OnEntityKill... But there are always alternatives
-
-
I just tested it, I had both OnEntityDeath and OnEntityKill checking if the entity is BaseHelicopter then output to console when that method gets called. I shot down a helicopter and only OnEntityDeath was called. I let one despawn and only OnEntityKill gets called. But on the odd occasion probably due to processing time I get both. So ignoring the entity ID in OnEntityKill with a timer delay could work before checking.
Code:private List<uint> HeliNetIDs = new List<uint>(); void OnEntityDeath(BaseCombatEntity entity, HitInfo info) { if (helicopterDestroyedAnnouncement && entity is BaseHelicopter) { var entityNetID = entity.net.ID; if (helicopterDespawnAnnouncement) HeliNetIDs.Add(entityNetID); if (helicopterDestroyedAnnouncementWithDestroyer) { CreateAnnouncement(helicopterDestroyedAnnouncementWithDestroyerText.Replace("{playername}", LastHitPlayer), helicopterDestroyedAnnouncementBannerColor, helicopterDestroyedAnnouncementTextColor); LastHitPlayer = String.Empty; } else { CreateAnnouncement(helicopterDestroyedAnnouncementText, helicopterDestroyedAnnouncementBannerColor, helicopterDestroyedAnnouncementTextColor); } } } void OnEntityKill(BaseNetworkable entity) { if (entity is BaseHelicopter) { var entityNetID = entity.net.ID; timer.Once(2, () => { if (HeliNetIDs.Contains(entityNetID)) HeliNetIDs.Remove(entityNetID); else if (helicopterDespawnAnnouncement) CreateAnnouncement(helicopterDespawnAnnouncementText, helicopterDespawnAnnouncementBannerColor, helicopterDespawnAnnouncementTextColor); }); } }
Last edited by a moderator: Mar 13, 2017