AdamsLair/duality

Check if gameobjects going to be removed in OnShutdown-Method

Aperta

#381 aperta il 28 ago 2016

 (4 commenti) (0 reazioni) (0 assegnatari)C# (287 fork)auto 404
CoreHelp WantedNice2HaveTaskUnit Tests

Metriche repository

Star
 (1425 stelle)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

Summary

The engine throws an exception when you remove gameobjects in the OnShutdown-Method.

How to reproduce [Bugs]

Here is my code:

private List<GameObject> enemies = new List<GameObject>();

public void OnInit(InitContext context)
{
    if (context == InitContext.Activate)
    {
        // add some gameobjects and put them into the list.
    }
}

public void OnShutdown(ShutdownContext context)
{
    if (context == ShutdownContext.Saving)
    {
        foreach (var enemy in enemies)
        {
            Scene.Current.RemoveObject(enemy);
        }
    }
}

What's my plan? I like to keep track of "enemies" and I want to remove them after exiting the sandbox-mode.

Attachments

The Exception:

[Core] Error:   OnSaving() of Scene "Data\Scenes\TestLevel" failed: InvalidOperationException: Collection was modified; enumeration operation may not execute.
CallStack:
   at System.Collections.Generic.HashSet`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at Duality.Resources.Scene.OnSaving(String saveAsPath) in c:\projects\duality\Duality\Resources\Scene.cs:line 942
   at Duality.Resource.CheckedOnSaving(String saveAsPath) in c:\projects\duality\Duality\Resource.cs:line 215

Jeah, I know that I try to remove gameobjects when the engine wants to save them.

Guida contributor