CS2335
Master 1.0.0
Master 1.0.0
  • Introduction
  • Introduction
    • Introduction
      • Game Design
    • Unity - Download
    • Visual Studio - IDE
    • Unity Manual, Scripting API
  • Unity Basics
    • Unity Editor Windows
    • Behavior Components
    • 2D Project, Scenes
      • Create 2D Sprite GameObject
      • Create UI-Text GameObject
    • MonoBehavior - Base-Class
    • Create a Custom C# Script
  • Game Coding Structures
    • Games Overview
    • Unity Engine - Event Functions
    • Finite State Machines
    • UML Class Diagram
  • Animation
    • Animator Controller
    • Animation Steps
      • Optional: Dead Animation
    • PlayerController.cs V0
  • Project 1 - Player
    • Player GameObject v1
      • C# Generics, Statics
    • Player GameObject - Jump
    • PlayerController.cs V2-Jump
    • PickUp PreFabs
      • Sorting Layers
    • PlayerController.cs V3-Collide
    • GameData Version1
    • GameData Version2
    • PlayerController V4-Score
  • Project 1 Details
    • Project1 GameObjects
    • PlayerStats Version1
      • UI-Canvas
    • Utility Class
    • Simple Spawner
    • MiniGameManager
      • Logic Diagrams
      • StartButton
      • ResultsPanel
  • Project1 Enhancements
    • PickUp - SelfDestruct
    • Spawn from List of Prefabs
  • Project 2 - StateManager
    • Project 2 - Learning Objectives
      • Inspiration
        • Branching Story Structures
        • Branching Structures
        • Hero's Journey
        • Visual Novel in Unity-Links
    • Project 2 - Starter Assets
    • State Machine Framework
    • StateManager - Singleton Design Pattern
    • Interface IStateBase
    • Create SceneXState.cs
    • OptionPanel Prefab
      • UI Images: Sprite Sheets
      • Button Image-Transitions
    • Project 2 - List of Steps
    • Project 2 - Starter Code
  • Project 2 -Dialogue
    • Hide_Show_Panel Script
    • Edit OptionPanel
    • Simple DialogPrefab
    • Conversation Entry
    • SimpleDialog.cs
    • ScriptableObjects
      • Scriptable Object Factory
    • Conversation Scriptable Objects
    • DialogManager_ConvList
      • DialogManager V2
      • Coroutines: Dynamic Text
      • DialogPrefab wImage
  • Overview: Branching Logic
    • DialogTrigger
    • Dictionary Data-Structure
      • Unity PlayerPrefs Dictionary
    • GameData Version3
    • Dictionary: choiceData
      • SaveChoice, ChoicePanel
        • choiceData - examples
          • Dictionary Value to Disable Options
    • Configure UI-Button Listeners
      • NPC Animation
      • NPC Activation
    • UI-Triggered Animations
    • Simple Inventory
    • EndState Conditions
    • Script ExecutionOrder
    • Custom UnityEvents
    • PlayerStats v2
      • ModifyPlayerData
      • BuyItem
    • Text Input
  • UI Components
    • Finding Game Objects
    • Game Objects: UI vs. 2D Sprite
    • UI Elements
      • Canvas: Screen-Space Render-Mode
      • UI-Buttons To Change Scene
  • Proj4: Inventory System
    • Inventory-System
      • GameData version4
      • Install and Configure
      • Diagrams, Resources
        • Item, Gem, Potion Classes
        • Inventory Class
      • InventoryDisplay, Slot UI
        • InventoryDisplay Class
        • Slot Class
        • Hazard Class
        • Layout Groups
      • Customization Steps
        • Configure Animation
        • AddItem Button
        • Concrete Class: Food
        • MiniGame Mods
          • PlayerController Mods
      • Code: InventorySystem
        • GameData, PickUp Mods
      • Resources: Data Structures
  • Custom Unity Events
    • Event Publishing Patterns
    • Custom Event Messaging
  • Proj4: Mini-Game
    • Simplified Mini-Game
      • PlayerController_v2 Mods
        • PlayerController_v2_final
      • MiniGameManager_v2
    • MiniGame-Overview-Proj4
    • LevelManager
      • LevelManager Logic Diagram
      • LevelManager FSM
      • LoadLevel, StartLevel Logic
      • Code Framework
    • Timer
  • Project 4 - Code Mods
    • Project 4 - Steps
    • Project 4 - Code
      • Code: Final Versions
        • PlayerController Mods
          • PlayerController_v2 Mods
        • GameData - Final
        • LevelManager
        • PlayerStats - Final
        • PickUp, Hazard, ScorePickUp
        • Spawner - Final
        • CameraFollow
        • ScreenFader
        • MiniGameState
        • Example: EndState
      • MiniGameWin Logic
  • Optional, Supplemental Content
    • Optional Content
      • Particle Systems
      • Adding Audio
      • Screen Fading and Reloading
      • ScriptableObjects
      • Disable Debug Logging
      • Events and Actions
      • Saving Data - Serialization
      • Parallax Scrolling
      • Change Sprites
    • XR - Extended Reality
  • Computing Concepts
    • Programming Patterns
      • State - FSM
      • Singleton Pattern
    • C# Language
      • Variables
      • Delegates
      • Dictionary
      • Enum
      • Encapsulation
        • C# Properties
        • Access Modifiers
      • Generics < T >
      • Inheritance
      • Interface
      • List< T >
      • Polymorphism
      • Queue< T >
      • Switch-Case
      • Foreach
      • Static
      • Ternary Operator: ?
      • this
    • Diagrams
      • State Machine Framework
      • UML Class Diagrams
      • Level Manager Logic Diagram
      • Flow-Chart: NumberGame
      • FSM: NumberGame
    • Tools
    • Glossary
    • References and Resources
    • Random Thoughts
Powered by GitBook
On this page
  • GameData - Version 1
  • Singleton Pattern:
  • GameObject Persistence: DontDestroyOnLoad( )
  • Static Variable:
  • Event-Driven Component Communication via public Methods
  • GameData Class Definition - Version1

Was this helpful?

  1. Project 1 - Player

GameData Version1

PreviousPlayerController.cs V3-CollideNextGameData Version2

Last updated 4 years ago

Was this helpful?

GameData - Version 1

Important Concepts: 1. Singleton Pattern - One and only one component instance exists - Globally accessible variable - Component Persistence between Scene-change 2. DontDestroyOnLoad( ) - Persistence 3. Static Variables - Globally accessible variable 4. Component Communication via Methods

In the game we'll create a custom component to store game-data that is persisted throughout the gameplay session so that we can keep track of score, lives, health, etc. This is the first version for class: GameData, we'll add more complexity to this class definition throughout the course.

Singleton Pattern:

GameData will use the , this means that we'll add code to insure that there is always one and only one of this gameObject in existence throughout the lifetime a gamePlay session.

Awake( ) - GameData will be initialized in the Awake( ) event-handler method, so that it's created before other gameObjects that may need to access the object during their initialization in the Start( ) event-handler.

GameObject Persistence: DontDestroyOnLoad( )

In order to insure that the GameData Component exists throughout the entire gameplay session, it's necessary to insure that the gameObject is not destroyed when changing scenes. So, the code below shows the use of the method: DontDestroyOnLoad( someGameObject ).

Test to determine existence of any other GameObject Instances: When initializing GameData in Awake( ) we need logic to test to determine if a GameData object already exits in a scene: The code below checks to see if the instanceRef variable already points to an active GameData object, if not, then this variable should point to: this (the object currently executing the code ). Otherwise a GameData object already exists and the one executing the code should destroy itself!

if( instanceRef == null)  //this code hasn't been executed before
        {
            instanceRef = this; //point to object instance currently executing this code
            DontDestroyOnLoad(this.gameObject); //don't destroy the gameObject this is attached to
        }

:

GameData instanceRef; is a the variable used to implement the Singleton Programming pattern for GameData.cs. A static variable belongs to the class, rather than to an object instance of a class, there can only be one instance of a static variable during a program's execution. Typical class-defined variables, each time an object is created from the class definition, each object gets it's own unique instance of each variable. Example: a class static variable can be used to keep track of the number of object instances currently instantiated in an executing program. public static numZombies; This variable could be incremented each time a zombie object is created and decremented each time a zombie object is destroyed during a program's execution, so that a count of the number of zombies could be displayed during the program. The data-type of the static variable instanceRef is GameData. So it's a variable that can 'point' to an instance of an object of the GameData class. While this seems a bit odd, think of it as you wearing a name-tag...it provides other gameObjects with easy access to the one GameData object in the game.

public static GameData instanceRef; //null //variable that can point to a GameData object

This script will be attached to the GameManager in the first game Scene and will be persisted throughout the game since it uses the singleton design pattern.

Add GameData script to an Empty GameObject: named: GameManager in your starting scene, so it will be executed as a singleton and shows up as 'DontDestroyOnLoad' in the Hierarchy when the game is played.

Event-Driven Component Communication via public Methods

Inter-Component Communication: When the Player gameObject has a collision with a gameObject that has an attached PickUp component, and a Collider2D Component with Trigger set to true: this can be considered the initializing event in a chain of events that need to be communicated with other components on other GameObjects, so that corresponding components are notified that the GameState has changed, so they can be updated to reflect that change. For example, the Score and Health displays should be updated. Requirement: PlayerController component must notify GameData component that Score or Health values have changed: Solution: When PlayerController logic has determined that a GameState changing event has occurred, it will execute a public GameData Method, so that GameData values for Score or Health are updated. We'll also discuss how other GameObjects can be notified to update, when a GameState changing-event occurs. See example below: How to use GameData Singleton To use the singleton reference in another class use the following syntax, for example, when calling the Add method from the PlayerController script:

Example of using GameData singleton in PlayerController.cs

// Inside PlayerController.cs
// Inside event-handler: OnTriggerEnter2D( )
// After PlayerController logic has deteremined 
//that the GameData.score must be updated 
GameData.instanceRef.Add( item.value );

GameData Class Definition - Version1

modified 4/10/2020

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// Singleton Object to store all GameData
/// Is not destroyed when changing scenes
/// </summary>
public class GameData : MonoBehaviour
{
    public static GameData instanceRef; //null //variable that can point to a GameData object

    private int score;
    private int health;
  
    //TODO - Add properties: Score, Health

    // Awake is called before Start() is called on any GameObject
    // Other objects have dependencies on this object so it must be created first
    void Awake()
    {
        if( instanceRef == null)  //this code hasn't been executed before
        {
            instanceRef = this; //point to object instance currently executing this code
            DontDestroyOnLoad(this.gameObject); //don't destroy the gameObject this is attached to
        }
        else  //this object is not the first, it's an imposter that must be destroyed
        {
            DestroyImmediate(this.gameObject);
            Debug.Log("Destroy GameData Imposter");
        }

        //initialize after destroying imposters
        score = 0;
        health = 100;
      

    } //end Awake

    //will be executed in PlayerController when colliding with a collectible
    public void Add( int value)
    {
        score += value;
        Debug.Log("Score updated: " + score); //display score in console
    }

    public void TakeDamage( int value)
    {
        health -= value;
        Debug.Log("health updated: " + health); //display health in console
        if( health <= 0)
        {
            Debug.Log("Health less than 0"); //display health in console

        }
    }

    //called when restarting the miniGame
    public void ResetGameData()
    {
        score = 0;
        health = 100;
    }


}//end class GameData

Resets Score and Health to correct initial values. Public, so it can be executed from MiniGameManager Script.

DontDestroyOnLoad - in Play-mode

Singleton Programming Pattern
Static Variable