Forum begins after the advertisement:


[Part 7] Question about the save system

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 7] Question about the save system

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #14952
    Aria
    Level 13
    Former Patron
    Helpful?
    Up
    0
    ::

    Hello, i was wondering if you could explain the purpose of the hashset of scene names and why we need it? I find that very unclear

    #14957
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    While i am not the creator of the series, sceneNames helps to maintain a record of player progress and updates the map by showing explored areas as well as where the player is at the time of a saved game.

    Although other functions can be used instead of hashset, like dictionary or list, it’s relatively simple to use for this purpose. It can also prevent duplicate names from occuring, however takes a bigger memory space because of how hashsets work.

    #16577
    A_DONUT
    Level 7
    Moderator
    Helpful?
    Up
    1
    ::

    A HashSet<string> of scene names is often used to manage or track scenes in a game, especially when you want to efficiently check if a particular scene has been loaded or if certain scenes need to be included in a group of scenes for a specific purpose (such as transitions, scene persistence, or exclusions).

    Here’s a breakdown of why you might use a HashSet<string> in Unity or other game engines, and how it works:

    Why Use a HashSet<string> for Scene Names?

    1. Efficient Lookup: A HashSet is a collection that allows you to quickly check if an item (in this case, a scene name) is present in the set. The lookup operation in a HashSet is very fast (O(1) time complexity), making it an ideal choice for checking scene names without iterating through a list.

      Example: If you have a large number of scenes and you need to frequently check if a scene is in the set (e.g., whether the current scene is part of a specific group), using a HashSet ensures that this check happens quickly.

    2. Scene Management: Sometimes you might want to track a set of specific scenes that you should treat differently (e.g., keeping certain scenes persistent across different parts of the game or ensuring that some scenes are excluded from transitions). A HashSet can help manage this by ensuring that only the scenes you need are included in the set.

    3. Avoiding Duplicates: Since HashSet automatically avoids duplicate entries, if you try to add the same scene name more than once, it will only appear once in the set. This is useful when you want to ensure that a scene is tracked without the risk of multiple identical entries.

      Example: If you’re keeping track of scenes that shouldn’t be reloaded, you don’t have to worry about adding a scene multiple times if it’s already in the set.

    4. Improved Performance for Scene Operations: If your game involves frequent checks for whether a scene is already loaded or needs to be loaded, using a HashSet can improve performance. Since Unity’s scene management functions like SceneManager.GetSceneByName() are slower when you have to search through a list, a HashSet gives you constant-time performance when checking for the presence of a scene.

    When Might You Use a HashSet<string> of Scene Names?

    Here are a few specific use cases for a HashSet of scene names:

    1. Persistent Scenes: If you have scenes that should persist across other scenes (e.g., a main menu or an inventory scene), you might use a HashSet to track these scenes. Before loading a new scene, you can check if the scene is in the persistent set and skip unloading it.

      Example:

      HashSet<string> persistentScenes = new HashSet<string> { "MainMenu", "Inventory", "Settings" };
      
      if (!persistentScenes.Contains(SceneManager.GetActiveScene().name)) {
          // Unload or change the scene if it's not persistent
      }
    2. Exclusion List for Scene Transitions: You may want to prevent certain scenes from being transitioned into directly or from being loaded under specific conditions. For instance, if the game is in a specific state (e.g., a pause menu), you may want to prevent players from loading certain scenes during that time.

      Example:

      HashSet<string> restrictedScenes = new HashSet<string> { "PauseMenu", "GameOver" };
      
      if (restrictedScenes.Contains(sceneName)) {
          // Prevent transitioning to the scene
      }
    3. Managing Loaded Scenes: If you need to keep track of which scenes are loaded or if a certain scene should be reloaded (for example, you want to check if the player has visited a particular area before loading a new level), you could use a HashSet to store the loaded scene names.

      Example:

      HashSet<string> loadedScenes = new HashSet<string>();
      
      void OnSceneLoaded(Scene scene, LoadSceneMode mode) {
          loadedScenes.Add(scene.name);
      }
      
      void OnSceneUnloaded(Scene scene) {
          loadedScenes.Remove(scene.name);
      }

    Summary of Benefits:

    • Fast lookups: Check if a scene is part of a group without looping through an array or list.
    • No duplicates: Automatically prevents duplicate entries, making management of scene sets cleaner.
    • Efficient performance: Scene checks (like whether to load or exclude a scene) are quick and non-blocking.

    In short, a HashSet<string> of scene names is a data structure that helps manage and track scenes more efficiently, especially in complex games where scene transitions, exclusions, or persistence need to be handled dynamically.

    Let me know if you have any more questions on this!

    has upvoted this post.
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: