Day 6 - Progress

Ok, so I'm looking back at my challenge system script and it feels messy and unclear. Eventhough the largest part was for testing purposes, I need it optimized before I start implementing the challenges. My new approach in using game states. This will make my code much neater and clearer.

Game Manager & State Manager
I've created a singleton Game Manager (empty GameObject + GameManager script). This Game Manager gets instantiated at run time and can not be destroyed during run time. (see image below)














For the State Manager I've also created a similar script. The StateManager will contain all states used within the game, e.g.: GameState, ChallengeState, TimerState. Both singletons get instantiated by the LoadGame script.

Challenge System
The hierarchy of the challenge system is: ChallengeManager, Challenge, Challenge1(2,3,...). The last class in the hierarchy inherits from the challenge class, and overrides specific functions per challenge. The Challenge Manager keeps track of the CountDownTimer and the Attemptcounter. 

At first I'd skipped the Challenge class in the hierarchy and made the last class inherit from the ChallengeManager, which at that time also contained the base class funtions for a challenge. I wanted to create a dynamic list to easily add more challenges (i <= 3 still needs to be changed into a dynamic variable though). But the instantiated derived classes (see image below) gave me some issues.







E.g. the timer.text for the CountDownTimer gave me a NullReferenceException because the object reference was never set to the instance of an object. After trying all sorts of possible solution and asking help from a fellow programmer, I decided to add the Challenge class in between to solve this issue for me. 

Now the ChallengeManager takes care of the timer and counter and the Challenge class takes care of the functions that will be used by the derived classes.

Comments