Remove all buildings in mapSolved

Hi!

Im trying to develop a plugin, I need to create a method that cleanup the entire map starting by destroy all buildings created by the players.

This is my first try:

void resetMap()
        {
            Puts("test");
            foreach (var wall in GameObject.FindObjectsOfType<BuildingBlock>())
            {
                Puts(wall.ToString());
                GameObject.Destroy(wall);
            }
        }​

First I placed a foundation, and next called resetMap(), the foundation apparently is not destroyed but after I placed another foundation next to the first one, the server kicked me for "RPC Error for DoInPlace".

After reconnecting, the first foundation disappeared, but apparently there is a ghost of it (?).
I mean if I walk where the foundation was I cannot walk through like the foundation is still there, and the server console is responding with "Server Exception: BasePlayer.BudgetedLifeStoryUpdate NullReferenceException"

How does it work? How can I remove that ghost foundation?

 

First, I would avoid using FindObjectsOfType or at least be careful when using it frequently because it's an expensive and slow method.

Alternatively, I would suggest looping BaseNetworkable.serverEntities and checking the type of each entity individually. This way is more performant. Otherwise, you could also use BaseNetworkable.serverEntities.OfType<BuildingBlock>()

Ok thanks, really, but how do I destroy it?

Something similar to this:

            foreach (BuildingBlock entity in BaseNetworkable.serverEntities.OfType<BuildingBlock>())
                entity.Kill();

Thank you Dana, it actually work and now I understend better what is going on. The problem is that not all the entities are returned, resulting, for example with 30 buildings, to have like 15 or 20 entities removed. Why?

There are difference between Kill() and DieInstantly()? I noticed that Kill() simply set HP to 0, resulting in the buildings to make to many particles if there are so many construction to destory.

Merged post

I was wrong, Kill() is perfect, and code solved with this:

foreach(var ent in BaseNetworkable.serverEntities)
            {
                if(typeof(BuildingBlock) == ent.GetType())
                {
                    Puts(ent.GetType().ToString());
                    ent.Kill();
                }

            }​
Seraph

There are difference between Kill() and DieInstantly()?

BaseCombatEntity.Die() and BaseCombatEntity.DieInstantly() are the two methods that are typically called when an entity dies, these methods handle things that would happen after they die (like a player dropping his weapon). The Kill() method is called after Die() and is for cleaning up the entity (terminating on clients, adding back to pools, etc.)

Locked automatically