Week 8 - Code improvements using "Managers" and Persistence using PlayerPrefs


With the addition of a singleton SoundManager class from the previous week, it became apparent that Managers made accessing actions that were needed across multiple scenes far easier. Because of this, the group decided to localise more functionality into "Manager" classes. These classes can all be found under the "Assets/Prefabs/Managers" folder in the github repository posted at the end of this blog.


The functionality that was moved into "Manager" classes include Sound (mentioned in last week's blog post), EndGameManager(The functionality behind when a game is over, such as number of lives reaching 0 or the player reaching the end of the game), LevelLoader (Functionality to progress the player from one level to another, or one scene to another), Button Manager(Handles the buttons on the Main Menu). Each of these classes are singletons which are initialized when the Main Menu is loaded and persist throughout gameplay using the "DontDestroyOnLoad" method. LevelLoader:
























(Ignore the copy paste comment for "SoundManager").

These manager classes provide a great way of being able to access functionality from any other scripts simply by attaching them to a GameObject that will be persisted across scene change. It also works by attaching one script to another, but this causes locking issues between scripts, making it harder to implement changes in future.

Another feature added this week was the addition of a"Hiscore" system using PlayerPrefs. The HiScore system uses a simple int value that is set and retreived from PlayerPrefs (full code can be found in the DataController.cs class in the GitHub repository). This class consists of some simple logic to check if the hiscore was beaten, then overwriting it if it was using the PlayerPrefs.GetInt("HiScore") and SetInt("HiScore", score). This class is attached to a GameObject and initialized on game load, loading in the value from PlayerPrefs if it exists (PlayerPrefs.HasKey("HiScore")).

As always, full code for this project can be found on GitHub through the following link: https://github.com/KAdams01/JazzJackrabbitComplete/tree/master/2D%20Game

Comments