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
- This topic has 2 replies, 3 voices, and was last updated 4 weeks, 1 day ago by A_DONUT.
-
AuthorPosts
-
June 5, 2024 at 2:48 pm #14952::
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
June 5, 2024 at 5:24 pm #14957::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.
November 27, 2024 at 8:25 pm #16577::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?-
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 aHashSet
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. -
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. -
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.
-
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 likeSceneManager.GetSceneByName()
are slower when you have to search through a list, aHashSet
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:-
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 }
-
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 }
-
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. -
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: