- Home /
How to implement a Game Manager using Switch and Case in C#
Hi, I'm looking for some help in setting up a Finite State Machine using the Switch and Case statements for a Game Manager to switch between Scenes.
I'm NOT looking to make a FSM for a character (There are a lot of good links out there for this), but I'm looking to build the Game Manager and how I would switch from scene to scene and use the new scene's update function without having to revert back to the Game Master class's update function.
This is what I have working so far: 1. In Game Master Class I have a enum list with all the difference scenes/levels (eg. Splash, MainMenu, Level01, Leve02...) 2. I set the enum case to SPLASH (for Splash Page) in the GameManager's Start Method. 3. Within Case:SPLASH implement the SPLASH class through an interface called ILevelBase. 4. In the SPLASH class I call applicationLoadLevel and load the SPLASH scene file.
This is where I am stuck. I want the game loop to reside within the SPLASH class update function, and not jump back to the GameManager class after constructing the SPLASH scene. How do I make the game loop reside in the SPLASH Scene/Class?
Thanks for your help. A.
Here is my code for my GameManager Class:
using UnityEngine;
using Assets.Scripts.Levels;
using Assets.Scripts.Interfaces;
using System.Collections;
public class GameManager : MonoBehaviour
{
private static GameManager gameManagerInstanceReference;
public ILevelBase activeLevel;
// All the Levels in the game.
public enum LEVEL
{
NULLLEVEL,
SPLASH,
MAINMENU,
LEVEL01,
LEVEL02,
HIGHSCORE,
CREDITS
};
public LEVEL level; // Use this to check the current level and set the current level.
void Awake ()
{
// Check to see if the current Game Manager object exists. If it does then destroy.
if (gameManagerInstanceReference == null)
{
gameManagerInstanceReference = this;
DontDestroyOnLoad (gameObject);
}
else
{
DestroyImmediate(gameObject);
}
}
// Use this for initialization
void Start ()
{
//We are in the START level. We now load the SPLASH Level for Logo and animation, and a countdown.
LevelChange(LEVEL.SPLASH);
}
void Update()
{
// Runs the Level's Update Method
activeLevel.LevelUpdate();
}
void LevelChange(LEVEL newLevel)
{
level = newLevel;
// LEVELS FSM
switch (level)
{
case LEVEL.SPLASH:
// load the Splash Level
activeLevel = new Splash(this);
break;
case LEVEL.MAINMENU:
// The currentLevel variable will be switched to MainMenu from the Splash Level Script.
break;
case LEVEL.LEVEL01:
// Load leevl 01 - After the Player Selects "Start" from the main Menu, the currentLevel variable will be assigned Level01
break;
case LEVEL.LEVEL02:
// Load leevl 02 - After the Player Selects "Start" from winning Level 01
break;
case LEVEL.HIGHSCORE:
// Play the High Score. Set the currentLevel variable to Credits
break;
case LEVEL.CREDITS:
// Roll Credits and then set the currentLevel variable to Main Menu.
break;
}
}
}
Your answer
Follow this Question
Related Questions
How to check if an enum’s case has changed 3 Answers
What is a more efficient way to write this Switch Statement? 3 Answers
Broken FSM - It only runs once, ignores switch 1 Answer
Getting dynamic indicies from multiple dropdown menus 0 Answers
checking if 2 out of 4 is true no matter which one 2 Answers