From e84203b1b4e2b7d48de557320b30663d15a51eea Mon Sep 17 00:00:00 2001
From: erheine <erheine@nps.edu>
Date: Fri, 6 Mar 2020 14:05:39 -0800
Subject: [PATCH] Added a component for firing game events on Unity Start and
 added the ability to delay firing Unity Events when a Game Event is being
 listened to.

---
 .../Listeners/GameEventListener.cs            | 43 +++++++++++++++++++
 .../Senders/StartEventSender.cs               | 13 ++++++
 .../Senders/StartEventSender.cs.meta          | 11 +++++
 3 files changed, 67 insertions(+)
 create mode 100644 Scriptable Variables/Senders/StartEventSender.cs
 create mode 100644 Scriptable Variables/Senders/StartEventSender.cs.meta

diff --git a/Scriptable Variables/Listeners/GameEventListener.cs b/Scriptable Variables/Listeners/GameEventListener.cs
index 7c89bdf..6f01632 100644
--- a/Scriptable Variables/Listeners/GameEventListener.cs	
+++ b/Scriptable Variables/Listeners/GameEventListener.cs	
@@ -1,4 +1,5 @@
 using System;
+using System.Collections;
 using UnityEngine;
 using UnityEngine.Events;
 
@@ -11,6 +12,9 @@ namespace Shared.ScriptableVariables {
     [Tooltip("The UnityEvent to raise in response to the game event being raised")]
     public UnityEvent response;
 
+    [Tooltip("The delay to have before firing the Unity Events")]
+    public float delay = 0.0f;
+
     //---------------------------------------------------------------------------
     void OnEnable() {
       eventToListenTo.OnRaised += OnEventRaised;
@@ -23,6 +27,24 @@ namespace Shared.ScriptableVariables {
 
     //---------------------------------------------------------------------------
     public virtual void OnEventRaised() {
+      // If there isn't a delay (or it's set wrong), just fire the Unity Event right away
+      if (delay <= 0.0f) {
+        InvokeUnityEvent();
+      }
+      // Otherwise, wait for the delay and THEN fire the Unity Event
+      else {
+        StartCoroutine(DelayInvokeUnityEvent());
+      }
+    }
+
+    //---------------------------------------------------------------------------
+    private IEnumerator DelayInvokeUnityEvent() {
+      yield return new WaitForSeconds(delay);
+      InvokeUnityEvent();
+    }
+
+    //---------------------------------------------------------------------------
+    private void InvokeUnityEvent() {
       if (response != null) {
         try {
           response.Invoke();
@@ -37,6 +59,9 @@ namespace Shared.ScriptableVariables {
 
   //---------------------------------------------------------------------------
   public abstract class GameEventListener<T> : MonoBehaviour {
+    [Tooltip("The delay to have before firing the Unity Events")]
+    public float delay = 0.0f;
+
     //---------------------------------------------------------------------------
     protected abstract GameEvent<T> GetGameEvent();
 
@@ -55,6 +80,24 @@ namespace Shared.ScriptableVariables {
 
     //---------------------------------------------------------------------------
     public virtual void OnEventRaised(T value) {
+      // If there isn't a delay (or it's set wrong), just fire the Unity Event right away
+      if (delay <= 0.0f) {
+        InvokeUnityEvent(value);
+      }
+      // Otherwise, wait for the delay and THEN fire the Unity Event
+      else {
+        StartCoroutine(DelayInvokeUnityEvent(value));
+      }
+    }
+
+    //---------------------------------------------------------------------------
+    private IEnumerator DelayInvokeUnityEvent(T value) {
+      yield return new WaitForSeconds(delay);
+      InvokeUnityEvent(value);
+    }
+
+    //---------------------------------------------------------------------------
+    private void InvokeUnityEvent(T value) {
       var unityEvent = GetUnityEvent();
       if (unityEvent != null) {
         try {
diff --git a/Scriptable Variables/Senders/StartEventSender.cs b/Scriptable Variables/Senders/StartEventSender.cs
new file mode 100644
index 0000000..c30d801
--- /dev/null
+++ b/Scriptable Variables/Senders/StartEventSender.cs	
@@ -0,0 +1,13 @@
+using UnityEngine;
+
+namespace Shared.ScriptableVariables {
+  // Fires the supplied GameEvent on Start()
+  public class StartEventSender : MonoBehaviour {
+    [Tooltip("Game Event to fire on start")]
+    public GameEvent startEvent;
+    
+    void Start() {
+      startEvent?.Raise();
+    }
+  }
+}
diff --git a/Scriptable Variables/Senders/StartEventSender.cs.meta b/Scriptable Variables/Senders/StartEventSender.cs.meta
new file mode 100644
index 0000000..6157217
--- /dev/null
+++ b/Scriptable Variables/Senders/StartEventSender.cs.meta	
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: cae2d5512c4785a41b7653c7de04f07a
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 
-- 
GitLab