< Return to Video

2D Roguelike 5 of 14 : Game Manager

  • 0:03 - 0:05
    In this video we're going to start
  • 0:05 - 0:07
    setting up our GameManager so that we can
  • 0:07 - 0:10
    test the BoardManager script that we just wrote.
  • 0:11 - 0:13
    Let's select our two scripts and add them
  • 0:13 - 0:15
    to the game manager.
  • 0:18 - 0:22
    Next let's open our game manager script in MonoDevelop.
  • 0:24 - 0:26
    In GameManager we're going to add
  • 0:26 - 0:28
    a public variable of the type BoardManager
  • 0:28 - 0:30
    called BoardScript.
  • 0:30 - 0:33
    We're also going to add a private variable
  • 0:33 - 0:37
    of the type int called Level and initialise it to 3.
  • 0:37 - 0:40
    We're using 3 because we're going to want to test
  • 0:40 - 0:43
    level 3 because that's where the enemies appear.
  • 0:43 - 0:45
    We'll change Start to Awake.
  • 0:45 - 0:47
    In Awake we're going to do two things.
  • 0:47 - 0:50
    We're going to get and store a component reference
  • 0:50 - 0:52
    to our BoardManager script.
  • 0:52 - 0:55
    And we're also going to call the InItGame function.
  • 0:55 - 0:57
    Let's declare InItGame.
  • 0:57 - 1:01
    In InItGame we're going to call the SetupScene function
  • 1:01 - 1:02
    of boardScript.
  • 1:02 - 1:05
    When we do this we're parse in the
  • 1:05 - 1:08
    parameter level so that we can tell boardScript
  • 1:09 - 1:11
    what level the scene that we're setting up
  • 1:11 - 1:14
    is so that it can determine the number of enemies.
  • 1:15 - 1:18
    With that done let's save our script and return to the editor.
  • 1:22 - 1:24
    Back in the editor it's time to assign
  • 1:24 - 1:27
    the variables of our BoardManager script.
  • 1:27 - 1:29
    Let's highlight the GameManager.
  • 1:31 - 1:33
    And what we're going to do is we're going to lock
  • 1:33 - 1:35
    the inspector so that we can then
  • 1:35 - 1:37
    click on multiple prefabs
  • 1:37 - 1:41
    and drag them in to our arrays here.
  • 1:41 - 1:45
    Let's lock the inspector by clicking the lock button up here.
  • 1:46 - 1:48
    And now what we can do, we'll start by dragging in
  • 1:48 - 1:50
    the exit, because that's first.
  • 1:53 - 1:55
    Next we're going to shift-click
  • 1:55 - 1:58
    to select all eight of our floor tile prefabs
  • 1:58 - 2:00
    and we're going to drag those directly
  • 2:00 - 2:03
    on to the floor tiles array.
  • 2:07 - 2:10
    This is going to allow us to add all of them at once
  • 2:10 - 2:12
    without losing focus.
  • 2:13 - 2:15
    Next we'll do the wall tiles.
  • 2:15 - 2:18
    Select Wall1, shift-click on Wall8.
  • 2:21 - 2:23
    Drag them to the Wall Tiles array.
  • 2:25 - 2:28
    We'll do the food tiles, in this case I'm going to
  • 2:28 - 2:32
    click on the food prefab and then command-click
  • 2:32 - 2:34
    on the soda prefab because I'm on a Mac.
  • 2:34 - 2:37
    If we were on a PC that would be control-click
  • 2:37 - 2:39
    to add to the selection.
  • 2:39 - 2:41
    We'll drag those to our Food Tiles array.
  • 2:42 - 2:44
    Let's grab our two enemy tiles.
  • 2:47 - 2:50
    And finally our outer wall tiles.
  • 2:54 - 2:57
    With those set let's unlock the inspector,
  • 2:59 - 3:00
    and give it a test.
  • 3:03 - 3:05
    Great, so we can see
  • 3:05 - 3:07
    that our levels are being spawned,
  • 3:07 - 3:09
    we can see that the camera is a little out of position,
  • 3:09 - 3:11
    so let's just fix that quickly.
  • 3:11 - 3:15
    We'll set the X to 3.5 and the Y to 3.5.
  • 3:15 - 3:17
    We'll also set the background to black.
  • 3:20 - 3:22
    With that done let's play the scene
  • 3:22 - 3:24
    and give it a test.
  • 3:28 - 3:29
    And so there we have it,
  • 3:29 - 3:32
    our exit, our floor background,
  • 3:32 - 3:34
    food items being spawned,
  • 3:34 - 3:37
    inner and outer walls being spawned,
  • 3:37 - 3:39
    and our enemies being spawned as well.
  • 3:44 - 3:48
    We're going to add some functionality to our GameManager script.
  • 3:48 - 3:51
    To make our GameManager what's called a singleton.
  • 3:51 - 3:54
    A singleton is an object for which there can
  • 3:54 - 3:57
    only be one instance in the game
  • 3:57 - 3:59
    at any given time.
  • 3:59 - 4:01
    Since the GameManager is going to do things like
  • 4:01 - 4:05
    loading levels, managing the player's score,
  • 4:05 - 4:07
    we wouldn't want more than one of
  • 4:07 - 4:09
    these object to exist,
  • 4:09 - 4:11
    so in our code we're going to make sure
  • 4:11 - 4:13
    that that's not possible.
  • 4:13 - 4:15
    Let's open the GameManager in MonoDevelop.
  • 4:17 - 4:20
    To setup our GameManager as a singleton
  • 4:20 - 4:23
    we're going to add a public static variable
  • 4:23 - 4:26
    of the type GameManager called Instance
  • 4:26 - 4:28
    and initialise it to null.
  • 4:28 - 4:30
    Declaring instances public means
  • 4:30 - 4:33
    that it will be accessible from outside the class.
  • 4:33 - 4:37
    Declaring it as static means that the variable will belong
  • 4:37 - 4:40
    to the class itself as opposed to
  • 4:40 - 4:42
    an instance of the class.
  • 4:42 - 4:44
    This means that we can now access the public functions
  • 4:44 - 4:46
    and variables of our game manager
  • 4:46 - 4:48
    from any script in our game.
  • 4:49 - 4:51
    In Awake we're going to check if
  • 4:51 - 4:54
    instance is equal to null, and if it is
  • 4:54 - 4:57
    we're going to assign it to this.
  • 4:57 - 5:00
    If that's not the case, and instance is not
  • 5:00 - 5:03
    equal to this we're going to destroy this
  • 5:03 - 5:06
    so that we don't accidentally end up
  • 5:06 - 5:09
    with two instances of our GameManager.
  • 5:09 - 5:11
    We're also going to set our GameManager
  • 5:11 - 5:14
    to DontDestroyOnLoad.
  • 5:14 - 5:16
    DontDestroyOnLoad means that when we
  • 5:16 - 5:19
    load a new scene, normally all of the
  • 5:19 - 5:22
    game objects in the hierarchy will be destroyed.
  • 5:22 - 5:24
    Because we want to use the GameManager to
  • 5:24 - 5:27
    keep track of the score between scenes
  • 5:27 - 5:30
    we don't want it to be destroyed at that point.
  • 5:30 - 5:32
    And so we're going to use DontDestroyOnLoad
  • 5:32 - 5:35
    to make sure that it will persist between scenes.
  • 5:35 - 5:38
    Let's save our script and return to the editor.
  • 5:40 - 5:42
    Now that we've added the scripts that we need to
  • 5:42 - 5:44
    our GameManager let's drag it down to our
  • 5:44 - 5:46
    Prefabs folder to create a prefab.
  • 5:51 - 5:53
    Before we're finished we're going to need one more
  • 5:53 - 5:55
    sample script which is going to load the
  • 5:55 - 5:57
    GameManager when the game starts.
  • 5:59 - 6:01
    We're going to go to Scripts and create
  • 6:01 - 6:03
    a new script called Loader.
  • 6:08 - 6:10
    Loader is a very simple script that's
  • 6:10 - 6:12
    just going to check if a GameManager has
  • 6:12 - 6:14
    been instantiated, and if not
  • 6:14 - 6:16
    instantiate one from our prefabs.
  • 6:16 - 6:19
    We're going to add a public game object,
  • 6:19 - 6:21
    called GameManager.
  • 6:21 - 6:23
    We're going to change our Start to an Awake.
  • 6:23 - 6:27
    In Awake we're going to check if GameManager.Instance
  • 6:27 - 6:29
    is equal to null.
  • 6:29 - 6:32
    Here we're using the static variable that we created
  • 6:32 - 6:35
    in the GameManager script
  • 6:35 - 6:37
    and accessing it from our Loader script.
  • 6:38 - 6:40
    If it is equal to null we're going to instantiate
  • 6:40 - 6:42
    our GameManager prefab.
  • 6:42 - 6:45
    We can delete the update function because we're not going to use it.
  • 6:46 - 6:48
    And that's all we need for now.
  • 6:50 - 6:54
    Now in the editor we can delete our GameManager from the hierarchy.
  • 6:55 - 6:58
    And add our Loader script to the main camera.
  • 6:59 - 7:01
    With our Loader script added we'll drag
  • 7:01 - 7:03
    in our GameManager prefab.
  • 7:07 - 7:09
    Now we can test our scene.
  • 7:12 - 7:15
    Now that we've got our GameManager started
  • 7:15 - 7:16
    and we can lay out some levels
  • 7:16 - 7:19
    it's time to get our units moving around.
  • 7:19 - 7:22
    In the next video we're going to write a script
  • 7:22 - 7:24
    called MovingObject to do that.
Title:
2D Roguelike 5 of 14 : Game Manager
Description:

more » « less
Video Language:
English
Duration:
07:31

English subtitles

Revisions