Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fight.cs, doCleanup(), List modified in enumeration #1

Closed
MarianSijanin opened this issue Jul 31, 2021 · 1 comment
Closed

Fight.cs, doCleanup(), List modified in enumeration #1

MarianSijanin opened this issue Jul 31, 2021 · 1 comment

Comments

@MarianSijanin
Copy link

Lines 232-241. There are list modifications in enumerations here:

            foreach(Entity e in liberals)
            {
                if (e != null && !e.getComponent<Body>().Alive)
                    liberals[liberals.IndexOf(e)] = null;
            }
            foreach(Entity e in conservatives)
            {
                if (e != null && !e.getComponent<Body>().Alive)
                    conservatives[conservatives.IndexOf(e)] = null;
            }

It crashes for me when someone dies:

InvalidOperationException: Collection was modified; enumeration operation may not execute.
System.ThrowHelper.ThrowInvalidOperationException (System.ExceptionResource resource) (at :0)
System.Collections.Generic.List1+Enumerator[T].MoveNextRare () (at <c8d0d7b9135640958bff528a1e374758>:0) System.Collections.Generic.List1+Enumerator[T].MoveNext () (at :0)
LCS.Engine.Scenes.Fight.doCleanup (System.Collections.Generic.List1[T] liberals, System.Collections.Generic.List1[T] conservatives) (at Assets/Scripts/Engine/Scenes/Fight.cs:232)

It's more correct to use an enumeration of indexes or to store indices in a list, e.g.:

            foreach(Entity e in liberals)
                if (e != null && !e.getComponent<Body>().Alive)
                    indicesToClear.Add(liberals.IndexOf(e));
            foreach (int index in indicesToClear)
                liberals[index] = null;
            ...
@Alex-McClelland
Copy link
Owner

Hmm, weirdly I don't get this exception when I'm running it, but I can see why it would be thrown. I've put in a fix for it that stores the entities in a temporary list that is iterated instead of directly iterating the list that's being modified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants