AdamsLair/duality

Check if gameobjects going to be removed in OnShutdown-Method

開放

#381 建立於 2016年8月28日

 (4 則留言) (0 個反應) (0 位負責人)C# (287 個分叉)auto 404
CoreHelp WantedNice2HaveTaskUnit Tests

倉庫指標

星標
 (1,425 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

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.

貢獻者指南