Solved hurtevent.entity.destroy > metal window bar (solved)

hunternope3

Plugin Developer
Plugin Developer
Mar 22, 2015
36
46
8
35
I noticed that the Entity.Destroy() function from class Entity doesn't destroy a metal window bar that is attached to a wooden/metal window. Is there anyone els that experienced the same problem? I have only noticed this problem with the Gameobject metal window bar.

for my remover tool i use:

C#:
hurtevent.Entity.Destroy();
to remove the structure/deployable.

I have tried the following code aswell, but i won't remove the metal window bar

C#:
if (hurtEvent.Entity.IsDeployableObject())
  {
  DeployableObject deployableObject = (DeployableObject)hurtEvent.Entity.Object;
  TakeDamage.KillSelf(deployableObject.GetComponent<IDMain>());
  }

  if (hurtEvent.Entity.IsStructure())
  {
  StructureComponent structureObject = (StructureComponent)hurtEvent.Entity.Object;
  TakeDamage.KillSelf(structureObject.GetComponent<IDMain>());
  }
Solved - See the comment section below
 
Last edited:

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,065
4,486
113
At your house.
github.com
I noticed that the Entity.Destroy() function from class Entity doesn't destroy a metal window bar that is attached to a wooden/metal window. Is there anyone els that experienced the same problem? I have only noticed this problem with the Gameobject metal window bar.

for my remover tool i use:

C#:
hurtevent.Entity.Destroy();
to remove the structure/deployable.

I have tried the following code aswell, but i won't remove the metal window bar

C#:
if (hurtEvent.Entity.IsDeployableObject())
  {
  DeployableObject deployableObject = (DeployableObject)hurtEvent.Entity.Object;
  TakeDamage.KillSelf(deployableObject.GetComponent<IDMain>());
  }

  if (hurtEvent.Entity.IsStructure())
  {
  StructureComponent structureObject = (StructureComponent)hurtEvent.Entity.Object;
  TakeDamage.KillSelf(structureObject.GetComponent<IDMain>());
  }
Try:

Util.DestroyObject(Entity.Object.gameObject);
 

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,065
4,486
113
At your house.
github.com
(UnityEngine GameObject.Destroy)
GameObject.Destroy((GameObject)hurtEvent.Entity.Object);
Does not destroy anything.

(Fougerite.Entity)
public void UpdateHealth() in class Entity
Does not take any parameter to actually update the health.

(Manual cast DeployableObject)
DeployableObject deployableObject = (DeployableObject)hurtEvent.Entity.Object;
deployableObject.ClientHealthUpdate(0f);
Does not destroy any object.

(Method Util.DestroyObject is not static)
Util util = Fougerite.Util.GetUtil();
util.DestroyObject((GameObject)hurtEvent.Entity.Object);
Does not destroy any object.

(property Health from Fougerite.Entity)
public float Health
Set does destroy entitys EXCEPT the metal window bar

i tried deployableObject.OnDestroy() and OnKilled(), still the metal window bar remained.

before removing:



after removing:

Does It even trigger the hurtevent?
 

hunternope3

Plugin Developer
Plugin Developer
Mar 22, 2015
36
46
8
35
yes, even put log statements after almsot every step. in the end i get the metal window bar in my inventory yet it still remains ingame.
 

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,065
4,486
113
At your house.
github.com
yes, even put log statements after almsot every step. in the end i get the metal window bar in my inventory yet it still remains ingame.
Wait.

util.DestroyObject((GameObject)hurtEvent.Entity.Object); that's bad

Should be

util.DestroyObject(hurtEvent.Entity.Object.gameObject);
 

hunternope3

Plugin Developer
Plugin Developer
Mar 22, 2015
36
46
8
35
Wait.

util.DestroyObject((GameObject)hurtEvent.Entity.Object); that's bad

Should be

util.DestroyObject(hurtEvent.Entity.Object.gameObject);
you can't do Object.gameObject since Object doesn't have the property gameObject
(you have to cast it first)
 

hunternope3

Plugin Developer
Plugin Developer
Mar 22, 2015
36
46
8
35
The explanation of the problem:
You have a metal window bar attached to a wooden or metal window.
You want to remove the metal window bar using any remove option (granted you know it will work).
Yet after removing the object there still remains a window bar ingame.

2 solutions found:

1. using Fougerite.Uitl.DestroyObject

C#:
util.DestroyObject(hurtEvent.Entity.Object.GetProperty("gameObject") as GameObject);
2.

To resolve the issue using hurtevent.entity.Destroy()
(Source: Fougerite.Entity (https://github.com/Notulp/Fougerite/blob/master/Fougerite/Fougerite/Entity.cs)

C#:
public void Destroy()
        {
            if (this.IsDeployableObject())
            {
                try
                {
                    this.GetObject<DeployableObject>().OnKilled();
                } catch
                {
                    TryNetCullDestroy();
                }
            } else if (this.IsStructure())
            {
                DestroyStructure(this.GetObject<StructureComponent>());         
            } else if (this.IsStructureMaster())
            {
                HashSet<StructureComponent> components = this.GetObject<StructureMaster>()._structureComponents;
                foreach (StructureComponent comp in components)
                    DestroyStructure(comp);

                try
                {
                    this.GetObject<StructureMaster>().OnDestroy();
                } catch
                {
                    TryNetCullDestroy();
                }
            }
        }
In this method we can see that the Method OnKilled() is being called from DeployAbleObject
(our metal window bar is a deployable object)

Now we are going to take a look what the method DeployableObject.OnKIlled() actually does:
(Source: Assembly-CSharp (from your legacy server located: \rust_server_Data\Managed\Assembly-CSharp.dll)

C#:
public void OnKilled()
    {
        if (this.handleDeathHere)
        {
            if (this.clientDeathEffect != null)
            {
                NetCull.RPC(NetEntityID.Get((MonoBehaviour) this), "Client_OnKilled", RPCMode.OthersBuffered);
            }
            NetCull.Destroy(base.gameObject);
            if (this.corpseObject != null)
            {
                NetCull.InstantiateStatic(this.corpseObject, base.transform.position, base.transform.rotation);
            }
        }
    }
This method starts of by a if statment (this.handleDeathHere) {}
If we look what this value is by default, we find that this value is by default ALWAYS false.
This means NO code of Onkilled() will actually be executed.
Leaving you with a metal window bar ingame (obviously since nothing happend).

So the solution to this problem is simple:

C#:
if (hurtEvent.Entity.IsDeployableObject() && itemName == "Metal Window Bars")
                            {                         
                                DeployableObject deployableObject = (DeployableObject)hurtEvent.Entity.Object;

deployableObject._carrier.RemoveObject(deployableObject);
deployableObject._carrier = null;
deployableObject.handleDeathHere = true;
}

hurtevent.Entity.Destroy();
We basically tell the DeployAbleObject that his attribute handleDeathHere should be: true.

After doing so we'll find out that the method DeployAbleObject.OnKilled() now actually executes all the code that we expect it should do.

NOTE:
To tell the wooden or metal window frame that there is no more metal window bar attached to it, we call the following lines:

C#:
deployableObject._carrier.RemoveObject(deployableObject);
deployableObject._carrier = null;
Otherwise the window still thinks it has a window bar attached to it.
 
Last edited:

hunternope3

Plugin Developer
Plugin Developer
Mar 22, 2015
36
46
8
35
are you checking on hurtevent.entity?
i can't find hurtevent.entity.gameobject (only object)
 

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,065
4,486
113
At your house.
github.com
are you checking on hurtevent.entity?
i can't find hurtevent.entity.gameobject (only object)
GameObject and Object is a different thing.

Object returns the original type (Rust class) of the entity.

It's not hurtevent.entity.gameobject.

As I said:

util.DestroyObject(hurtEvent.Entity.Object.gameObject);
 

hunternope3

Plugin Developer
Plugin Developer
Mar 22, 2015
36
46
8
35
GameObject and Object is a different thing.

Object returns the original type (Rust class) of the entity.

It's not hurtevent.entity.gameobject.

As I said:

util.DestroyObject(hurtEvent.Entity.Object.gameObject);
output on tostring (given the entity.object is a metal bar)

MetalBarsWindow<Clone> <DeployableeObject>
 

hunternope3

Plugin Developer
Plugin Developer
Mar 22, 2015
36
46
8
35
util.DestroyObject(hurtEvent.Entity.Object.GetProperty("gameObject") as GameObject);

seems to work.

But this doesn't seem logical to do
 
Last edited:

hunternope3

Plugin Developer
Plugin Developer
Mar 22, 2015
36
46
8
35
It's a bit weird that metal window bars has it's handleDeathHere set to false.
All the other structures/deployables seem to remove fine using Entity.Destory()
 

Snake

Moderator
Moderator
Jul 13, 2014
288
174
28
So much stuff to destroy a simple metal window bar...

C#:
NetCull.Destroy((entity.Object as DeployableObject).gameObject);

ez pz
 

hunternope3

Plugin Developer
Plugin Developer
Mar 22, 2015
36
46
8
35
So much stuff to destroy a simple metal window bar...

C#:
NetCull.Destroy((entity.Object as DeployableObject).gameObject);

ez pz
destroying it like that isn't correct though it works.

Try
C#:
NetCull.Destroy((entity.Object as DeployableObject).gameObject);
or Entity.Destory() on a wooden foundation with a furnace on it. you'll see how easy peasy the furnace doesn't get destroyed.

you will have floating items

C#:
NetCull.Destroy((entity.Object as DeployableObject).gameObject);
is only a cheap trick to destroy the metal window bar but i do not advise it to use.