sinbad / SPUD

Steve's Persistent Unreal Data library
MIT License
300 stars 44 forks source link

Cannot iterate actors after loading a game #72

Closed krojew closed 3 months ago

krojew commented 3 months ago

Seems like TActorIterator is not working after loading a game. When iterating actors with a given class after PostLoadGame fires, the iterator rejects many of the actors due to them belonging to a different world - TActorIteratorBase::operator++() has a check which checks every actor found if its world is the same as the world its level belongs to. This check fails for some reason thus breaking iteration.

sinbad commented 3 months ago

Are you storing an actor iterator in your own code or something? You can't do that, actor iterators are only valid in a short window of time. Yes, loading a game will re-create the world so will definitely kill you stored actor iterators among many other things.

krojew commented 3 months ago

No - in my game mode I listen to PostLoadGame and then run a for loop with TActorIterator on the world returned by GetWorld(). I am expecting to find around 10 actors of given class, but I only find 2. After debugging, the rest are rejected due to being in a different world. Can't tell why those random 2 are in the same world.

krojew commented 3 months ago

Found the problem. Turns out the iterator iterates over the actors from both the previous and newly loaded world, and I simply didn't notice that. The actual error was in different place.

sinbad commented 3 months ago

Possibly the previous world still exists but has yet to be garbage collected. You should probably be using the EActorIteratorFlags::OnlyActiveLevels flag in constructing your iterator. Also note that at PostLoadGame, if you have any streaming levels, those won't have been loaded yet, only the persistent level.

sinbad commented 3 months ago

Ah we crossed replies, glad you figured it out.