Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Put a scene on standby while entering a building or cave?

Discussion in 'Scripting' started by sarynth, Jan 12, 2021.

  1. sarynth

    sarynth

    Joined:
    May 16, 2017
    Posts:
    98
    I have an "outside world" scene, and I'd like to switch scenes when the player goes into a building or cave.

    Then I'd like to re-activate the outside world quickly, without destroying the position and living/dead state of all of the creatures outside.

    I was hoping there would be an easy "UnloadScene" without destroying everything so I could "ReloadScene" and have it exactly as I left it.

    Doesn't seem to be a thing -- is there a good approach for this? I just watched the How to work with multiple scenes in Unity which is nice, and gives me some general ideas, but as you can see in that video, when the author goes to the northern end of the game, and the middle path is unloaded, then re-loaded -- those creatures show up again as the author traverses back to the entrance room.

    Ideas? Approaches? Thanks.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    There's really no general case solution.

    Loading new scenes in Unity is pretty cool: it gives you a nice fresh slate.

    Perhaps your game in the future will want a load/save functionality, so this might be an excuse to make something that can "save and restore" your creatures for a given level.

    Therefore when you enter the cave you would store all the creatures that were in the previous scene, plus the position of the player and anything else you care about, the go in with a regular scene load.

    When you come back out, load the scene but restore the state of the creatures rather than make a set of fresh creatures.

    Again though, none of this is super-easy, but take it step by step. It's totally doable.
     
    sarynth likes this.
  3. sarynth

    sarynth

    Joined:
    May 16, 2017
    Posts:
    98
    Good ideas. The main thing I'm left wondering is how to track randomly generated monsters.

    Outside of the cave, my statically placed creatures I can create a GUID and attach it to the creatures. So then as the creatures are killed, we record that GUID. And upon reloading the scene, remove all dead GUIDs.

    But, in a randomly generated cave scenario that becomes a little more tricky. Got any ideas?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    I would use two possible ways of populating the scene:

    1. allow emplaced monsters and random generation to occur

    OR

    2. create them from the serialized list you made when you left the level.

    Keeping a kill list is counter to how I would naturally think about it. I'd rather keep track of what is here than what is gone.
     
    sarynth likes this.
  5. sarynth

    sarynth

    Joined:
    May 16, 2017
    Posts:
    98
    I like your idea of remembering the creatures when leaving.

    So, I think I'll work on implementing this approach:
    - The first time visiting a cave during a game session, a random seed for the cave is generated, that generates the map.
    - Random creatures are spawned. Saved to an isAlive list.
    - As the randomly spawned creatures are killed, remove them from the list.

    - If leaving, and then returning to the cave in the same play session, since the cave has already generated, load creatures from the isAlive list.
    - Also, the saved seed will allow the scene to regenerate the same layout.

    I do want the player to be able to exit the game session, and start a new game session in order to get entirely fresh creatures. So I think this approach will work. Thanks for the ideas.
     
    Kurt-Dekker likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    Without trying to start any flame wars, this is how the computer science guys would do it. :)

    Personally I hate writing list management code, especially when Unity is already tracking it ALL for you! Not only that but that list code has to be touched by EVERYTHING that goes in there. Ugh, yuck...

    I would create a blank MonoBehaviour called SaveableEntity (no methods, no functions, no nothing) and attach that to every entity (of any kind) that you want to save and respawn.

    When you hit that doorway do a FindObjectsOfType<SaveableEntity> and BAM, you can find your GUID, see what kind of enemy it is, look up transforms, etc. and serialize.

    Just think of all the bugs in list management code that you will NEVER have to think about with this approach.
     
    sarynth likes this.
  7. sarynth

    sarynth

    Joined:
    May 16, 2017
    Posts:
    98
    Oh, you mean when you hit the doorway leaving, instantly grab everything. Completely agree, great point. Thank you. I will definitely work that into my solution.
     
    Kurt-Dekker likes this.
  8. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Are you doing this for performance reasons, or because you want to pause the state of the outside world while your in a cave?
     
  9. sarynth

    sarynth

    Joined:
    May 16, 2017
    Posts:
    98
    Primary desire is to return to the same state as before. So, the player is blasting through creatures, they come upon a cave, they enter the cave, complete some small thing in the cave, leave the cave, all of the enemies should not be reloaded behind them.

    Secondary reason is there are several caves/dungeons/buildings I want to enter and return back to the main play area without all of the creatures showing up again. I am using a different scene so that I can control the lighting and create a new environment within the cave.

    The initial desire did include performance reasons. Taking a reference -- Diablo 3 -- when you checkpoint warp to an area, it takes a second or two to load (depending on your machine performance). Then when you immediately warp back to town, it is fast. Then warp back to checkpoint instant load. Warp to different checkpoint, takes a few seconds to load. Point being -- it seems like they cache the area or loaded game objects. In my game when teleporting between scenes that 1-2 second load time never goes away. So I was hoping there was a better way to only partially unload the scene or something. But, I'm not too worried about this, it was only a nice to have. The more important feature is not respawning killed enemies when returning to the main scene from the cave.
     
    Kurt-Dekker likes this.