Here’s another idea, which I have implemented recently as i came across this issue as well.
I didn’t want to destroy and respawn actors as some of mine have a lot of variables and states, and whilst I used to do this at one point with my inventory, it caused other headaches, as well as a lot of management code. There is also a performance impact of lots of spawns when you stream a level in.
Instead, I have done this:
- my entire world is made up of streaming levels below a persistent level, which get turned on and off on demand.
- all my pickups are created in the persistent level only - but in the location where they ought to be when that specific streaming level is loaded
- at start, all pickups are “despawned” which is a term i use for: simulate physics off, unticked, collision off, invisible
- all pickups have a local variable with its assigned initial level name (CurrentLevel), so when I loop through them I know which ones belong to each streaming level
- at each streaming level load, loop through and “respawn” them (i.e. physics on, collision on, visible, etc.)
- when leaving that level, again, loop through and “despawn” them again, and respawn the new level pickups (quicker than it sounds)
some things to take note of:
- when picking up something into inventory, change its CurrentLevel to something like “Inventory” so it gets ignored when moving between levels
- when dropping something out of inventory, change its CurrentLevel enum to the current streaming level, so it doesn’t get ignore next time you visit that level (and fall through the ground when its original streaming level is loaded)
- if your pickup needs to interact with any actors in the streaming level… you have a small problem - you cant reference them. So instead, I use tags to identify them in a loop of “getAllActorsofClass”… not ideal, but works ok.
- if the world allows you to throw an item from one streaming level into the next… what then? You could auto-assign the pickups CurrentLevel on its “hit event” I suppose, but i haven’t addressed this as it hasn’t come up as an actual real in game problem yet.
Overall the system works well, no noticeable performance implications.
Hope that helps?